function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_without_holes(arr) {
    if (Array.isArray(arr)) return _array_like_to_array(arr);
}
function _iterable_to_array(iter) {
    if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _non_iterable_spread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _to_consumable_array(arr) {
    return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
import rehypeKatex from "rehype-katex";
import rehypeRaw from "rehype-raw";
import remarkFrontmatter from "remark-frontmatter";
import remarkGfm from "remark-gfm";
import remarkHtml from "remark-html";
import remarkMath from "remark-math";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import { unified } from "unified";
import { visit } from "unist-util-visit";
/**
 * Plugin to fix bold text containing special characters like **$9.698M**, **57%**, etc.
 *
 * @returns {(tree: any) => void}
 *   Transformer function.
 */ export function fixStrongWithSpecialChars() {
    // 返回转换器函数
    return function(tree) {
        // 先尝试处理段落中的原始文本
        visit(tree, 'paragraph', function(paragraphNode) {
            if (Array.isArray(paragraphNode.children)) {
                for(var i = 0; i < paragraphNode.children.length; i++){
                    var child = paragraphNode.children[i];
                    if (child.type === 'text' && child.value && typeof child.value === 'string') {
                        // 匹配包含特殊字符的加粗文本（美元符号、百分号、引号、书名号、其他数字相关符号）
                        var strongPattern = /\*\*([^*\n]*[$%#@&+\-=\w\d.，。、；：！？""''（）【】《》]+[^*\n]*?)\*\*/g;
                        if (strongPattern.test(child.value)) {
                            // 重置正则表达式
                            strongPattern.lastIndex = 0;
                            var newNodes = [];
                            var lastIndex = 0;
                            var match = void 0;
                            // 分割文本并创建新的节点结构
                            while((match = strongPattern.exec(child.value)) !== null){
                                // 添加匹配前的普通文本
                                if (match.index > lastIndex) {
                                    var beforeText = child.value.slice(lastIndex, match.index);
                                    if (beforeText) {
                                        newNodes.push({
                                            type: 'text',
                                            value: beforeText
                                        });
                                    }
                                }
                                // 添加加粗节点
                                newNodes.push({
                                    type: 'strong',
                                    children: [
                                        {
                                            type: 'text',
                                            value: match[1]
                                        }
                                    ]
                                });
                                lastIndex = match.index + match[0].length;
                            }
                            // 添加剩余的普通文本
                            if (lastIndex < child.value.length) {
                                var afterText = child.value.slice(lastIndex);
                                if (afterText) {
                                    newNodes.push({
                                        type: 'text',
                                        value: afterText
                                    });
                                }
                            }
                            // 替换当前文本节点
                            if (newNodes.length > 0) {
                                var _paragraphNode_children;
                                (_paragraphNode_children = paragraphNode.children).splice.apply(_paragraphNode_children, [
                                    i,
                                    1
                                ].concat(_to_consumable_array(newNodes)));
                                i += newNodes.length - 1; // 调整索引以跳过新插入的节点
                            }
                        }
                    }
                }
            }
        });
        // 处理所有文本节点（作为备用方案）
        visit(tree, 'text', function(node, index, parent) {
            if (node.value && typeof node.value === 'string') {
                // 匹配包含特殊字符的加粗文本（美元符号、百分号、引号、书名号、其他数字相关符号）
                var strongPattern = /\*\*([^*\n]*[$%#@&+\-=\w\d.，。、；：！？""''（）【】《》]+[^*\n]*?)\*\*/g;
                if (strongPattern.test(node.value)) {
                    // 重置正则表达式
                    strongPattern.lastIndex = 0;
                    var newNodes = [];
                    var lastIndex = 0;
                    var match;
                    // 分割文本并创建新的节点结构
                    while((match = strongPattern.exec(node.value)) !== null){
                        // 添加匹配前的普通文本
                        if (match.index > lastIndex) {
                            var beforeText = node.value.slice(lastIndex, match.index);
                            if (beforeText) {
                                newNodes.push({
                                    type: 'text',
                                    value: beforeText
                                });
                            }
                        }
                        // 添加加粗节点
                        newNodes.push({
                            type: 'strong',
                            children: [
                                {
                                    type: 'text',
                                    value: match[1]
                                }
                            ]
                        });
                        lastIndex = match.index + match[0].length;
                    }
                    // 添加剩余的普通文本
                    if (lastIndex < node.value.length) {
                        var afterText = node.value.slice(lastIndex);
                        if (afterText) {
                            newNodes.push({
                                type: 'text',
                                value: afterText
                            });
                        }
                    }
                    // 替换原节点
                    if (parent && Array.isArray(parent.children) && typeof index === 'number') {
                        var _parent_children;
                        (_parent_children = parent.children).splice.apply(_parent_children, [
                            index,
                            1
                        ].concat(_to_consumable_array(newNodes)));
                    }
                }
            }
        });
    };
}
// 完整的 HTML 渲染解析器
var markdownParser = unified().use(remarkParse).use(remarkHtml).use(remarkGfm) // GFM 插件
.use(fixStrongWithSpecialChars) // 修复包含特殊字符的加粗文本
.use(remarkMath, {
    singleDollarTextMath: true
}).use(remarkRehype, {
    allowDangerousHtml: true
}).use(rehypeRaw).use(rehypeKatex).use(remarkFrontmatter, [
    'yaml'
]);
export default markdownParser;
