( text: string, grammar: SyntaxGrammar = defaultGrammar, pos = 0 )
| 300 | }; |
| 301 | |
| 302 | export function parseCode( |
| 303 | text: string, |
| 304 | grammar: SyntaxGrammar = defaultGrammar, |
| 305 | pos = 0 |
| 306 | ): CodeSpan[] { |
| 307 | const spans: CodeSpan[] = []; |
| 308 | |
| 309 | const buf = new Buffer(text, pos); |
| 310 | |
| 311 | while (!buf.atEnd()) { |
| 312 | const result = |
| 313 | grammar.comment?.(buf) ?? |
| 314 | grammar.number?.(buf) ?? |
| 315 | grammar.regex?.(buf) ?? |
| 316 | grammar.string?.(buf) ?? |
| 317 | grammar.keyword?.(buf) ?? |
| 318 | grammar.identifier?.(buf); |
| 319 | |
| 320 | if (result) { |
| 321 | spans.push(result); |
| 322 | continue; |
| 323 | } |
| 324 | |
| 325 | // If last span is default, coalesce with this one |
| 326 | const lastSpan = spans.length ? spans[spans.length - 1] : null; |
| 327 | if (lastSpan?.tag === 'default') { |
| 328 | lastSpan.content += buf.consume(); |
| 329 | } else { |
| 330 | spans.push({ content: buf.consume(), tag: 'default' }); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | return spans; |
| 335 | } |
| 336 | |
| 337 | function renderCode(spans: CodeSpan[], theme = DEFAULT_THEME): StyledSpan[] { |
| 338 | return spans.map((span) => { |
no test coverage detected