MCPcopy Create free account
hub / github.com/cortex-js/compute-engine / parseSpan

Function parseSpan

src/common/markdown-span.ts:5–141  ·  view source on GitHub ↗
(t: string)

Source from the content-addressed store, hash-verified

3
4/** Parse a markdown string into tagged text fragments */
5export function parseSpan(t: string): TaggedSpan[] {
6 let i = 0;
7 const spans: TaggedSpan[] = [];
8
9 // const match = (regexp: RegExp) => {
10 // const m = t.slice(i).match(regexp);
11 // if (!m) return;
12 // i += m[0].length;
13 // return m[0];
14 // };
15
16 const match = (s: string) => {
17 let j = 0;
18 while (i + j < t.length && t[i + j] === s[j]) j += 1;
19
20 return j === s.length;
21 };
22
23 const peek = () => {
24 if (t[i] === '\\' && t[i + 1] !== undefined) {
25 // Escape characters
26 if ('\\`*_{}[]<>()#+-.!|'.includes(t[i + 1])) {
27 i += 1;
28 return t[i];
29 }
30 }
31 return t[i];
32 };
33
34 const consume = () => {
35 const c = peek();
36 i += 1;
37 return c;
38 };
39
40 const parseCode = () => {
41 // Note: the caller has not consumed the initial backtick
42
43 // A quintuple backtick is a code span for a single backtick
44 if (match('`````')) return { tag: 'code', s: '`' };
45 // A triple backtick is also a code span for a single backtick
46 if (match('```')) return { tag: 'code', s: '`' };
47 i += 1;
48 let s = '';
49
50 if (t[i] === '`' && t[i + 1] === '`') {
51 // In a double backtick code span, there are no escape sequences
52 // and single backticks are preserved verbatim
53 i += 2;
54 // Inside a code span, characters are not escaped, so don't use consume()
55 while (t[i] !== '`' && t[i + 1] !== '`' && i < t.length) {
56 s += t[i];
57 i += 1;
58 }
59 i += 2;
60 spans.push({ tag: 'code', s });
61 }
62

Callers 5

parseHeadingMethod · 0.90
parseCalloutMethod · 0.90
parseListMethod · 0.90
parseTableMethod · 0.90
parseParagraphMethod · 0.90

Calls 8

parseEmphasisFunction · 0.85
parseBoldFunction · 0.85
parseItalicFunction · 0.85
parseTagFunction · 0.85
matchFunction · 0.70
consumeFunction · 0.70
parseCodeFunction · 0.70
sliceMethod · 0.65

Tested by

no test coverage detected