( token: Token, width: number, nextTokenType?: string, styleContext?: InlineStyleContext, )
| 325 | } |
| 326 | |
| 327 | private renderToken( |
| 328 | token: Token, |
| 329 | width: number, |
| 330 | nextTokenType?: string, |
| 331 | styleContext?: InlineStyleContext, |
| 332 | ): string[] { |
| 333 | const lines: string[] = []; |
| 334 | |
| 335 | switch (token.type) { |
| 336 | case "heading": { |
| 337 | const headingLevel = token.depth; |
| 338 | const headingPrefix = `${"#".repeat(headingLevel)} `; |
| 339 | |
| 340 | // Build a heading-specific style context so inline tokens (codespan, bold, etc.) |
| 341 | // restore heading styling after their own ANSI resets instead of falling back to |
| 342 | // the default text style. |
| 343 | let headingStyleFn: (text: string) => string; |
| 344 | if (headingLevel === 1) { |
| 345 | headingStyleFn = (text: string) => this.theme.heading(this.theme.bold(this.theme.underline(text))); |
| 346 | } else { |
| 347 | headingStyleFn = (text: string) => this.theme.heading(this.theme.bold(text)); |
| 348 | } |
| 349 | |
| 350 | const headingStyleContext: InlineStyleContext = { |
| 351 | applyText: headingStyleFn, |
| 352 | stylePrefix: this.getStylePrefix(headingStyleFn), |
| 353 | }; |
| 354 | |
| 355 | const headingText = this.renderInlineTokens(token.tokens || [], headingStyleContext); |
| 356 | const styledHeading = headingLevel >= 3 ? headingStyleFn(headingPrefix) + headingText : headingText; |
| 357 | lines.push(styledHeading); |
| 358 | if (nextTokenType && nextTokenType !== "space") { |
| 359 | lines.push(""); // Add spacing after headings (unless space token follows) |
| 360 | } |
| 361 | break; |
| 362 | } |
| 363 | |
| 364 | case "paragraph": { |
| 365 | const paragraphText = this.renderInlineTokens(token.tokens || [], styleContext); |
| 366 | lines.push(paragraphText); |
| 367 | // Don't add spacing if next token is space or list |
| 368 | if (nextTokenType && nextTokenType !== "list" && nextTokenType !== "space") { |
| 369 | lines.push(""); |
| 370 | } |
| 371 | break; |
| 372 | } |
| 373 | |
| 374 | case "text": |
| 375 | lines.push(this.renderInlineTokens([token], styleContext)); |
| 376 | break; |
| 377 | |
| 378 | case "code": { |
| 379 | const indent = this.theme.codeBlockIndent ?? " "; |
| 380 | lines.push(this.theme.codeBlockBorder(`\`\`\`${token.lang || ""}`)); |
| 381 | if (this.theme.highlightCode) { |
| 382 | const highlightedLines = this.theme.highlightCode(token.text, token.lang); |
| 383 | for (const hlLine of highlightedLines) { |
| 384 | lines.push(`${indent}${hlLine}`); |
no test coverage detected