Merge pull request #441 from qitechgmbh/markdown-inline-styles-in-headings

Support inline styles in markdown headings
This commit is contained in:
Icedsoda authored and GitHub committed 2025-06-30 16:15:22 +02:00
commit 32ba1b0f75
2 files changed
+43 -4

No files matched your search

+10 -2
View File
@@ -157,7 +157,15 @@ export function Markdown({ text }: MarkdownProps) {
),
// eslint-disable-next-line @typescript-eslint/no-unused-vars
strong: ({ node, ...props }) => (
<strong className="font-bold" {...props} />
<strong className="[font-size:inherit] font-bold" {...props} />
),
// eslint-disable-next-line @typescript-eslint/no-unused-vars
em: ({ node, ...props }) => (
<em className="[font-size:inherit] italic" {...props} />
),
// eslint-disable-next-line @typescript-eslint/no-unused-vars
del: ({ node, ...props }) => (
<del className="[font-size:inherit] line-through" {...props} />
),
// eslint-disable-next-line @typescript-eslint/no-unused-vars
img: ({ node, alt, ...props }) => (
@@ -171,7 +179,7 @@ export function Markdown({ text }: MarkdownProps) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
code: ({ node, ...props }) => (
<code
className="rounded-full bg-gray-100 p-0.5 px-2 text-sm"
className="rounded-sm bg-gray-100 p-0.5 px-2 [font-size:inherit]"
{...props}
/>
),
+33 -2
View File
@@ -296,8 +296,10 @@ function parseMarkdownHeadings(markdownContent: string): TocItem[] {
const match = line.match(/^(#{1,6})\s+(.+)$/);
if (match) {
const level = match[1].length;
const title = match[2].trim();
const id = generateHeadingId(title);
const rawTitle = match[2].trim();
// Strip markdown formatting for TOC display
const title = stripMarkdownInlineFormatting(rawTitle);
const id = generateHeadingId(rawTitle); // Use raw title for ID generation
headings.push({ level, title, id });
}
});
@@ -305,6 +307,35 @@ function parseMarkdownHeadings(markdownContent: string): TocItem[] {
return buildTocTree(headings);
}
/**
* Strip markdown inline formatting from text for TOC display
* Removes: **bold**, *italic*, `code`, ~~strikethrough~~, links, etc.
*/
function stripMarkdownInlineFormatting(text: string): string {
return (
text
// Remove bold (**text** or __text__)
.replace(/\*\*(.*?)\*\*/g, "$1")
.replace(/__(.*?)__/g, "$1")
// Remove italic (*text* or _text_)
.replace(/\*(.*?)\*/g, "$1")
.replace(/_(.*?)_/g, "$1")
// Remove strikethrough (~~text~~)
.replace(/~~(.*?)~~/g, "$1")
// Remove inline code (`text`)
.replace(/`([^`]+)`/g, "$1")
// Remove links [text](url) -> text
.replace(/\[([^\]]+)\]\([^)]+\)/g, "$1")
// Remove reference-style links [text][ref] -> text
.replace(/\[([^\]]+)\]\[[^\]]*\]/g, "$1")
// Remove images ![alt](url) -> alt
.replace(/!\[([^\]]*)\]\([^)]+\)/g, "$1")
// Clean up any remaining markdown characters
.replace(/[*_`~]/g, "")
.trim()
);
}
function buildTocTree(
headings: Array<{ level: number; title: string; id: string }>,
): TocItem[] {