UNPKG

@lobehub/ui

Version:

Lobe UI is an open-source UI component library for building AIGC web apps

1 lines 5.99 kB
{"version":3,"file":"utils.mjs","names":[],"sources":["../../../src/hooks/useMarkdown/utils.ts"],"sourcesContent":["import { preprocessLaTeX } from './latex';\n\n// Cache configuration\nconst CACHE_SIZE = 50;\n\n/**\n * Cache for storing processed content to avoid redundant processing\n */\nexport const contentCache = new Map<string, string>();\n\n/**\n * Adds content to the cache with size limitation\n * Removes oldest entry if cache size limit is reached\n *\n * @param key The cache key\n * @param value The processed content to store\n */\nexport const addToCache = (key: string, value: string) => {\n if (contentCache.size >= CACHE_SIZE) {\n // Remove the oldest cache entry\n const firstKey = contentCache.keys().next().value;\n if (firstKey) contentCache.delete(firstKey);\n }\n contentCache.set(key, value);\n};\n\n/**\n * Transforms citation references in the format [n] to markdown links\n *\n * @param rawContent The markdown content with citation references\n * @param length The number of citations\n * @returns The content with citations transformed to markdown links\n */\nexport const transformCitations = (rawContent: string, length: number = 0) => {\n if (length === 0) return rawContent;\n\n // 生成引用索引\n const citationIndices = Array.from({ length })\n .fill('')\n .map((_, index) => index + 1);\n\n // 匹配所有潜在的引用\n const pattern = new RegExp(`\\\\[(${citationIndices.join('|')})\\\\]`, 'g');\n const matches: { id: string; index: number; length: number }[] = [];\n\n let match;\n while ((match = pattern.exec(rawContent)) !== null) {\n matches.push({\n id: match[1],\n index: match.index,\n length: match[0].length,\n });\n }\n\n // 识别所有需要排除的区域\n const excludedRanges: { end: number; start: number }[] = [];\n\n // 查找LaTeX块 $$...$$\n const latexBlockRegex = /\\$\\$([\\s\\S]*?)\\$\\$/g;\n while ((match = latexBlockRegex.exec(rawContent)) !== null) {\n excludedRanges.push({\n end: match.index + match[0].length - 1,\n start: match.index,\n });\n }\n\n // 查找行内LaTeX $...$\n const inlineLatexRegex = /\\$([^$]*)\\$/g;\n while ((match = inlineLatexRegex.exec(rawContent)) !== null) {\n excludedRanges.push({\n end: match.index + match[0].length - 1,\n start: match.index,\n });\n }\n\n // 查找代码块 ```...```\n const codeBlockRegex = /```([\\s\\S]*?)```/g;\n while ((match = codeBlockRegex.exec(rawContent)) !== null) {\n excludedRanges.push({\n end: match.index + match[0].length - 1,\n start: match.index,\n });\n }\n\n // 查找行内代码 `...`\n const inlineCodeRegex = /`([^`]*)`/g;\n while ((match = inlineCodeRegex.exec(rawContent)) !== null) {\n excludedRanges.push({\n end: match.index + match[0].length - 1,\n start: match.index,\n });\n }\n\n // 过滤掉在排除区域内的引用\n const validMatches = matches.filter((citation) => {\n return !excludedRanges.some(\n (range) => citation.index >= range.start && citation.index <= range.end,\n );\n });\n\n // 从后向前替换,避免索引变化问题\n let result = rawContent;\n for (let i = validMatches.length - 1; i >= 0; i--) {\n const citation = validMatches[i];\n const before = result.slice(0, Math.max(0, citation.index));\n const after = result.slice(Math.max(0, citation.index + citation.length));\n result = before + `[#citation-${citation.id}](citation-${citation.id})` + after;\n }\n\n // 处理连续引用\n return result.replaceAll('][', '] [');\n};\n\n/**\n * Preprocessing options for markdown content\n */\ninterface PreprocessOptions {\n citationsLength?: number;\n enableCustomFootnotes?: boolean;\n enableLatex?: boolean;\n}\n\nexport const preprocessMarkdownContent = (\n str: string,\n { enableCustomFootnotes, enableLatex, citationsLength }: PreprocessOptions = {},\n) => {\n let content = str;\n\n // Process LaTeX expressions\n if (enableLatex) {\n content = preprocessLaTeX(content);\n }\n\n // Process custom footnotes/citations\n if (enableCustomFootnotes) {\n content = transformCitations(content, citationsLength);\n }\n\n return content;\n};\n"],"mappings":";;AAGA,MAAM,aAAa;;;;AAKnB,MAAa,+BAAe,IAAI,IAAoB;;;;;;;;AASpD,MAAa,cAAc,KAAa,UAAkB;CACxD,IAAI,aAAa,QAAQ,YAAY;EAEnC,MAAM,WAAW,aAAa,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC;EAC5C,IAAI,UAAU,aAAa,OAAO,QAAQ;CAC5C;CACA,aAAa,IAAI,KAAK,KAAK;AAC7B;;;;;;;;AASA,MAAa,sBAAsB,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