@lobehub/ui
Version:
Lobe UI is an open-source UI component library for building AIGC web apps
1 lines • 6.17 kB
Source Map (JSON)
{"version":3,"file":"remarkGfmPlus.mjs","names":["textNodes: any[]","node"],"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;CACD;AAED,MAAa,iBAAiB,UAAgC,EAAE,KAAK;CACnE,MAAM,EAAE,gBAAgB,4BAA4B;AAEpD,SAAQ,SAAc;AAEpB,QAAM,OAAO,SAAc;AACzB,OAAI,CAAC,KAAK,YAAY,CAAC,MAAM,QAAQ,KAAK,SAAS,CAAE;GAErD,MAAM,WAAW,KAAK;GACtB,IAAI,IAAI;AAER,UAAO,IAAI,SAAS,QAAQ;IAC1B,MAAM,cAAc,SAAS;AAG7B,QAA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