MCPcopy Create free account
hub / github.com/Noumena-Network/code / formatToken

Function formatToken

src/utils/markdown.ts:464–695  ·  view source on GitHub ↗
(
  token: Token,
  theme: ThemeName,
  listDepth = 0,
  orderedListNumber: number | null = null,
  parent: Token | null = null,
  highlight: CliHighlight | null = null,
)

Source from the content-addressed store, hash-verified

462}
463
464export function formatToken(
465 token: Token,
466 theme: ThemeName,
467 listDepth = 0,
468 orderedListNumber: number | null = null,
469 parent: Token | null = null,
470 highlight: CliHighlight | null = null,
471): string {
472 switch (token.type) {
473 case 'blockquote': {
474 const inner = (token.tokens ?? [])
475 .map(_ => formatToken(_, theme, 0, null, null, highlight))
476 .join('')
477 // Prefix each line with a dim vertical bar. Keep text italic but at
478 // normal brightness — chalk.dim is nearly invisible on dark themes.
479 const bar = chalk.dim(BLOCKQUOTE_BAR)
480 return inner
481 .split(EOL)
482 .map(line =>
483 stripAnsi(line).trim() ? `${bar} ${chalk.italic(line)}` : line,
484 )
485 .join(EOL)
486 }
487 case 'code': {
488 if (!highlight) {
489 return token.text + EOL
490 }
491 let language = 'plaintext'
492 if (token.lang) {
493 if (highlight.supportsLanguage(token.lang)) {
494 language = token.lang
495 } else {
496 logForDebugging(
497 `Language not supported while highlighting code, falling back to plaintext: ${token.lang}`,
498 )
499 }
500 }
501 return highlight.highlight(token.text, { language }) + EOL
502 }
503 case 'codespan': {
504 // inline code
505 return color('permission', theme)(token.text)
506 }
507 case 'em':
508 return chalk.italic(
509 (token.tokens ?? [])
510 .map(_ => formatToken(_, theme, 0, null, parent, highlight))
511 .join(''),
512 )
513 case 'strong':
514 return chalk.bold(
515 (token.tokens ?? [])
516 .map(_ => formatToken(_, theme, 0, null, parent, highlight))
517 .join(''),
518 )
519 case 'heading':
520 switch (token.depth) {
521 case 1: // h1

Callers 4

formatCellFunction · 0.85
applyMarkdownFunction · 0.85
getDisplayTextFunction · 0.85

Calls 8

colorFunction · 0.85
createHyperlinkFunction · 0.85
getListNumberFunction · 0.85
linkifyIssueReferencesFunction · 0.85
getDisplayTextFunction · 0.85
padAlignedFunction · 0.85
maxMethod · 0.80
logForDebuggingFunction · 0.70

Tested by

no test coverage detected