UNPKG

@lobehub/ui

Version:

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

137 lines (119 loc) 4.01 kB
import { preprocessLaTeX } from "./latex"; // Cache configuration var CACHE_SIZE = 50; /** * Cache for storing processed content to avoid redundant processing */ export var contentCache = new Map(); /** * Adds content to the cache with size limitation * Removes oldest entry if cache size limit is reached * * @param key The cache key * @param value The processed content to store */ export var addToCache = function addToCache(key, value) { if (contentCache.size >= CACHE_SIZE) { // Remove the oldest cache entry var firstKey = contentCache.keys().next().value; if (firstKey) contentCache.delete(firstKey); } contentCache.set(key, value); }; /** * Transforms citation references in the format [n] to markdown links * * @param rawContent The markdown content with citation references * @param length The number of citations * @returns The content with citations transformed to markdown links */ export var transformCitations = function transformCitations(rawContent) { var length = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; if (length === 0) return rawContent; // 生成引用索引 var citationIndices = Array.from({ length: length }).fill('').map(function (_, index) { return index + 1; }); // 匹配所有潜在的引用 var pattern = new RegExp("\\[(".concat(citationIndices.join('|'), ")\\]"), 'g'); var matches = []; var match; while ((match = pattern.exec(rawContent)) !== null) { matches.push({ id: match[1], index: match.index, length: match[0].length }); } // 识别所有需要排除的区域 var excludedRanges = []; // 查找LaTeX块 $$...$$ var latexBlockRegex = /\$\$([\S\s]*?)\$\$/g; while ((match = latexBlockRegex.exec(rawContent)) !== null) { excludedRanges.push({ end: match.index + match[0].length - 1, start: match.index }); } // 查找行内LaTeX $...$ var inlineLatexRegex = /\$([^$]*?)\$/g; while ((match = inlineLatexRegex.exec(rawContent)) !== null) { excludedRanges.push({ end: match.index + match[0].length - 1, start: match.index }); } // 查找代码块 ```...``` var codeBlockRegex = /```([\S\s]*?)```/g; while ((match = codeBlockRegex.exec(rawContent)) !== null) { excludedRanges.push({ end: match.index + match[0].length - 1, start: match.index }); } // 查找行内代码 `...` var inlineCodeRegex = /`([^`]*?)`/g; while ((match = inlineCodeRegex.exec(rawContent)) !== null) { excludedRanges.push({ end: match.index + match[0].length - 1, start: match.index }); } // 过滤掉在排除区域内的引用 var validMatches = matches.filter(function (citation) { return !excludedRanges.some(function (range) { return citation.index >= range.start && citation.index <= range.end; }); }); // 从后向前替换,避免索引变化问题 var result = rawContent; for (var i = validMatches.length - 1; i >= 0; i--) { var citation = validMatches[i]; var before = result.slice(0, Math.max(0, citation.index)); var after = result.slice(Math.max(0, citation.index + citation.length)); result = before + "[#citation-".concat(citation.id, "](citation-").concat(citation.id, ")") + after; } // 处理连续引用 return result.replaceAll('][', '] ['); }; /** * Preprocessing options for markdown content */ export var preprocessMarkdownContent = function preprocessMarkdownContent(str) { var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, enableCustomFootnotes = _ref.enableCustomFootnotes, enableLatex = _ref.enableLatex, citationsLength = _ref.citationsLength; var content = str; // Process LaTeX expressions if (enableLatex) { content = preprocessLaTeX(content); } // Process custom footnotes/citations if (enableCustomFootnotes) { content = transformCitations(content, citationsLength); } return content; };