(tokens: Token[], theme: string, highlight: CliHighlight | null)
| 117 | } |
| 118 | |
| 119 | function buildMarkdownRenderBlocks(tokens: Token[], theme: string, highlight: CliHighlight | null): MarkdownRenderBlock[] { |
| 120 | const buildStartMs = nowMs(); |
| 121 | const blocks: MarkdownRenderBlock[] = []; |
| 122 | let nonTableContent = ""; |
| 123 | const flushNonTableContent = function flushNonTableContent() { |
| 124 | if (nonTableContent) { |
| 125 | blocks.push({ |
| 126 | type: 'ansi', |
| 127 | content: nonTableContent.trim() |
| 128 | }); |
| 129 | nonTableContent = ""; |
| 130 | } |
| 131 | }; |
| 132 | for (const token of tokens) { |
| 133 | if (token.type === "table") { |
| 134 | flushNonTableContent(); |
| 135 | blocks.push({ |
| 136 | type: 'table', |
| 137 | token: token as Tokens.Table |
| 138 | }); |
| 139 | } else if (token.type === "code") { |
| 140 | flushNonTableContent(); |
| 141 | let cachedFallbackAnsi: string | null = null; |
| 142 | blocks.push({ |
| 143 | type: 'code', |
| 144 | code: token.text, |
| 145 | language: token.lang ?? null, |
| 146 | getFallbackAnsi: () => { |
| 147 | if (cachedFallbackAnsi !== null) { |
| 148 | return cachedFallbackAnsi; |
| 149 | } |
| 150 | const codeFenceFormatStartMs = nowMs(); |
| 151 | cachedFallbackAnsi = formatToken(token, theme, 0, null, null, highlight); |
| 152 | recordMarkdownCodeFenceFormat({ |
| 153 | codeLength: token.text.length, |
| 154 | language: token.lang ?? null, |
| 155 | durationMs: nowMs() - codeFenceFormatStartMs |
| 156 | }); |
| 157 | return cachedFallbackAnsi; |
| 158 | } |
| 159 | }); |
| 160 | } else { |
| 161 | nonTableContent = nonTableContent + formatToken(token, theme, 0, null, null, highlight); |
| 162 | nonTableContent; |
| 163 | } |
| 164 | } |
| 165 | flushNonTableContent(); |
| 166 | recordMarkdownRenderBuild(nowMs() - buildStartMs); |
| 167 | return blocks; |
| 168 | } |
| 169 | |
| 170 | export function getCachedMarkdownRenderBlocks(content: string, theme: string, highlight: CliHighlight | null): MarkdownRenderBlock[] { |
| 171 | configureMarked(); |
no test coverage detected