@lobehub/ui
Version:
Lobe UI is an open-source UI component library for building AIGC web apps
1 lines • 6.2 kB
Source Map (JSON)
{"version":3,"file":"remarkGfmPlus.mjs","names":[],"sources":["../../../src/Markdown/plugins/remarkGfmPlus.ts"],"sourcesContent":["import { visit } from 'unist-util-visit';\n\ninterface RemarkGfmPlusOptions {\n allowHtmlTags?: string[];\n}\n\nconst DEFAULT_ALLOW_HTML_TAGS = [\n 'sub',\n 'sup',\n 'ins',\n 'kbd',\n 'b',\n 'strong',\n 'i',\n 'em',\n 'mark',\n 'del',\n 'u',\n];\n\nexport const remarkGfmPlus = (options: RemarkGfmPlusOptions = {}) => {\n const { allowHtmlTags = DEFAULT_ALLOW_HTML_TAGS } = options;\n\n return (tree: any) => {\n // 遍历所有父节点,查找分离的HTML标签模式\n visit(tree, (node: any) => {\n if (!node.children || !Array.isArray(node.children)) return;\n\n const children = node.children;\n let i = 0;\n\n while (i < children.length) {\n const currentNode = children[i];\n\n // 查找开始标签\n if (currentNode.type === 'html' && currentNode.value) {\n const tagPattern = `^<(${allowHtmlTags.join('|')})>$`;\n const startTagMatch = currentNode.value.match(new RegExp(tagPattern));\n\n if (startTagMatch) {\n const tagName = startTagMatch[1];\n\n // 查找对应的结束标签\n let endIndex = -1;\n const textNodes: any[] = [];\n\n for (let j = i + 1; j < children.length; j++) {\n const nextNode = children[j];\n\n if (nextNode.type === 'html' && nextNode.value === `</${tagName}>`) {\n endIndex = j;\n break;\n } else if (nextNode.type === 'text') {\n textNodes.push(nextNode);\n } else {\n // 如果遇到其他类型的节点,停止查找\n break;\n }\n }\n\n if (endIndex !== -1) {\n // 收集所有文本内容\n const textContent = textNodes.map((node) => node.value).join('');\n\n // 创建新的自定义节点\n const newNode = {\n children: [{ type: 'text', value: textContent }],\n data: {\n hName: tagName,\n hProperties: {},\n },\n type: tagName,\n };\n\n // 替换从开始标签到结束标签的所有节点\n const removeCount = endIndex - i + 1;\n children.splice(i, removeCount, newNode);\n\n // 继续处理下一个节点\n i++;\n continue;\n }\n }\n }\n\n i++;\n }\n });\n\n // 保留对文本节点中完整HTML标签的处理(作为备用)\n visit(tree, 'text', (node, index = 0, parent) => {\n if (!node.value || typeof node.value !== 'string') return;\n\n // 处理HTML实体编码的标签\n const encodedTagPattern = `<(${allowHtmlTags.join('|')})>(.*?)<\\\\/\\\\1>`;\n const encodedTagRegex = new RegExp(encodedTagPattern, 'gi');\n const text = node.value;\n\n if (!encodedTagRegex.test(text)) return;\n\n // 重置正则表达式的 lastIndex\n encodedTagRegex.lastIndex = 0;\n\n const newNodes = [];\n let lastIndex = 0;\n let match;\n\n while ((match = encodedTagRegex.exec(text)) !== null) {\n const [fullMatch, tagName, content] = match;\n const startIndex = match.index;\n\n // 添加匹配前的文本\n if (startIndex > lastIndex) {\n newNodes.push({\n type: 'text',\n value: text.slice(lastIndex, startIndex),\n });\n }\n\n // 添加特殊标签节点\n newNodes.push({\n children: [{ type: 'text', value: content }],\n data: {\n hName: tagName,\n hProperties: {},\n },\n type: tagName,\n });\n\n lastIndex = startIndex + fullMatch.length;\n }\n\n // 添加剩余文本\n if (lastIndex < text.length) {\n newNodes.push({\n type: 'text',\n value: text.slice(lastIndex),\n });\n }\n\n // 替换当前节点\n if (newNodes.length > 0 && parent) {\n parent.children.splice(index, 1, ...newNodes);\n return index + newNodes.length - 1;\n }\n });\n };\n};\n"],"mappings":";;AAMA,MAAM,0BAA0B;CAC9B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AAEA,MAAa,iBAAiB,UAAgC,CAAC,MAAM;CACnE,MAAM,EAAE,gBAAgB,4BAA4B;CAEpD,QAAQ,SAAc;EAEpB,MAAM,OAAO,SAAc;GACzB,IAAI,CAAC,KAAK,YAAY,CAAC,MAAM,QAAQ,KAAK,QAAQ,GAAG;GAErD,MAAM,WAAW,KAAK;GACtB,IAAI,IAAI;GAER,OAAO,IAAI,SAAS,QAAQ;IAC1B,MAAM,cAAc,SAAS;IAG7B,IAAI,YAAY,SAA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