( content: string, theme: ThemeName, highlight: CliHighlight | null = null, )
| 422 | } |
| 423 | |
| 424 | export function applyMarkdown( |
| 425 | content: string, |
| 426 | theme: ThemeName, |
| 427 | highlight: CliHighlight | null = null, |
| 428 | ): string { |
| 429 | const stripped = stripPromptXMLTags(content) |
| 430 | const shouldCache = stripped.length >= APPLY_MARKDOWN_CACHE_MIN_CONTENT_LENGTH |
| 431 | const cacheKey = shouldCache |
| 432 | ? `${hashContent(stripped)}|${theme}|${highlight ? 1 : 0}` |
| 433 | : undefined |
| 434 | |
| 435 | if (cacheKey) { |
| 436 | const hit = applyMarkdownCache.get(cacheKey) |
| 437 | if (hit !== undefined) { |
| 438 | applyMarkdownCache.delete(cacheKey) |
| 439 | applyMarkdownCache.set(cacheKey, hit) |
| 440 | return hit |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | // String-only markdown preview paths (notably AskUserQuestion previews) do |
| 445 | // not go through Markdown.tsx, so they bypass the block/token caches above. |
| 446 | // Keep a tiny LRU here for large preview bodies to avoid repeatedly lexing |
| 447 | // and re-highlighting the same rendered file/markdown payload on rerender. |
| 448 | const rendered = lexMarkdownPreservingPreformattedDiagrams(stripped) |
| 449 | .map(_ => formatToken(_, theme, 0, null, null, highlight)) |
| 450 | .join('') |
| 451 | .trim() |
| 452 | |
| 453 | if (cacheKey) { |
| 454 | if (applyMarkdownCache.size >= APPLY_MARKDOWN_CACHE_MAX) { |
| 455 | const oldest = applyMarkdownCache.keys().next().value |
| 456 | if (oldest !== undefined) applyMarkdownCache.delete(oldest) |
| 457 | } |
| 458 | applyMarkdownCache.set(cacheKey, rendered) |
| 459 | } |
| 460 | |
| 461 | return rendered |
| 462 | } |
| 463 | |
| 464 | export function formatToken( |
| 465 | token: Token, |
no test coverage detected