MCPcopy Create free account
hub / github.com/Feather-2/Burner-X / protectMarkdownCodeSegments

Function protectMarkdownCodeSegments

js/processing/markdown_processor.js:22–82  ·  view source on GitHub ↗

* 将 Markdown 中的代码区段(包括 ``code``、```code``` 等)提取为占位符,避免后续公式解析破坏代码内容。 * @param {string} md - Markdown 文本 * @returns {{ text: string, placeholders: Array<{ placeholder: string, segment: string }> }}

(md)

Source from the content-addressed store, hash-verified

20 * @returns {{ text: string, placeholders: Array<{ placeholder: string, segment: string }> }}
21 */
22 function protectMarkdownCodeSegments(md) {
23 if (!md || typeof md !== 'string') {
24 return { text: md, placeholders: [] };
25 }
26
27 const placeholders = [];
28 const basePlaceholder = 'PBTOKEN' + Date.now().toString(36) + Math.random().toString(36).slice(2) + 'Z';
29 const suffix = 'X';
30 let result = '';
31 let i = 0;
32 const len = md.length;
33
34 while (i < len) {
35 const char = md[i];
36 if (char === '`' && (i === 0 || md[i - 1] !== '\\')) {
37 const start = i;
38 let j = i;
39 while (j < len && md[j] === '`') {
40 j++;
41 }
42 const fenceLen = j - start;
43 const fence = '`'.repeat(fenceLen);
44 let searchIndex = j;
45 let closingIndex = -1;
46
47 while (searchIndex < len) {
48 const idx = md.indexOf(fence, searchIndex);
49 if (idx === -1) {
50 break;
51 }
52 const prevChar = idx > 0 ? md[idx - 1] : '';
53 const nextChar = md[idx + fenceLen];
54 if (prevChar === '\\') {
55 searchIndex = idx + fenceLen;
56 continue;
57 }
58 if (nextChar === '`') {
59 searchIndex = idx + 1;
60 continue;
61 }
62 closingIndex = idx;
63 break;
64 }
65
66 if (closingIndex !== -1) {
67 const end = closingIndex + fenceLen;
68 const segment = md.slice(start, end);
69 const placeholder = basePlaceholder + placeholders.length + suffix;
70 placeholders.push({ placeholder: placeholder, segment: segment });
71 result += placeholder;
72 i = end;
73 continue;
74 }
75 }
76
77 result += char;
78 i++;
79 }

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected