{"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,KAAqB;;;;;;;;AASrD,MAAa,cAAc,KAAa,UAAkB;AACxD,KAAI,aAAa,QAAQ,YAAY;EAEnC,MAAM,WAAW,aAAa,MAAM,CAAC,MAAM,CAAC;AAC5C,MAAI,SAAU,cAAa,OAAO,SAAS;;AAE7C,cAAa,IAAI,KAAK,MAAM;;;;;;;;;AAU9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