fix(tui): preserve heading styling after inline code spans

Headings applied styling (bold+cyan) as an outer wrapper around the
result of renderInlineTokens. When inline elements like codespan emitted
ANSI resets, the outer heading style was killed and only the default
text style was restored via stylePrefix.

Fix: pass a heading-specific InlineStyleContext into renderInlineTokens
so each text segment gets heading styling directly, and stylePrefix
restores heading style after inline elements.
This commit is contained in:
Mario Zechner
2026-03-23 01:07:38 +01:00
parent d501b9ca26
commit ab771a84b2
2 changed files with 76 additions and 6 deletions

View File

@@ -272,15 +272,24 @@ export class Markdown implements Component {
case "heading": {
const headingLevel = token.depth;
const headingPrefix = `${"#".repeat(headingLevel)} `;
const headingText = this.renderInlineTokens(token.tokens || [], styleContext);
let styledHeading: string;
// Build a heading-specific style context so inline tokens (codespan, bold, etc.)
// restore heading styling after their own ANSI resets instead of falling back to
// the default text style.
let headingStyleFn: (text: string) => string;
if (headingLevel === 1) {
styledHeading = this.theme.heading(this.theme.bold(this.theme.underline(headingText)));
} else if (headingLevel === 2) {
styledHeading = this.theme.heading(this.theme.bold(headingText));
headingStyleFn = (text: string) => this.theme.heading(this.theme.bold(this.theme.underline(text)));
} else {
styledHeading = this.theme.heading(this.theme.bold(headingPrefix + headingText));
headingStyleFn = (text: string) => this.theme.heading(this.theme.bold(text));
}
const headingStyleContext: InlineStyleContext = {
applyText: headingStyleFn,
stylePrefix: this.getStylePrefix(headingStyleFn),
};
const headingText = this.renderInlineTokens(token.tokens || [], headingStyleContext);
const styledHeading = headingLevel >= 3 ? headingStyleFn(headingPrefix) + headingText : headingText;
lines.push(styledHeading);
if (nextTokenType && nextTokenType !== "space") {
lines.push(""); // Add spacing after headings (unless space token follows)