Skip to content

Commit 33a906a

Browse files
committed
og: strip markdown link/bold/italic/code syntax from slide titles
1 parent 0b5cadb commit 33a906a

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

  • packages/slidev/node/commands

packages/slidev/node/commands/og.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,26 @@ function firstParagraph(content: string): string {
102102
.slice(0, 300)
103103
}
104104

105+
// Strip markdown link syntax from a heading title — slidev passes the raw
106+
// heading source as `slide.title`, so `# Demo: [foo.bar](https://foo.bar)`
107+
// arrives as `Demo: [foo.bar](https://foo.bar)`. Keep the anchor text, drop
108+
// the URL. Also collapse leftover `**bold**` / `*italic*` / `` `code` ``
109+
// markers so the title reads naturally in social cards.
110+
function stripMd(s: string): string {
111+
return s
112+
.replace(/\[([^\]]+)\]\([^)]+\)/g, '$1')
113+
.replace(/\*\*?([^*]+)\*\*?/g, '$1')
114+
.replace(/`([^`]+)`/g, '$1')
115+
.replace(/\s+/g, ' ')
116+
.trim()
117+
}
118+
105119
function pickTitle(slide: SlideInfo, deckTitle: string, no: number): string {
106120
const fm = slide.frontmatter ?? {}
107121
if (typeof fm.title === 'string' && fm.title.trim())
108-
return fm.title.trim()
122+
return stripMd(fm.title)
109123
if (typeof slide.title === 'string' && slide.title.trim())
110-
return slide.title.trim()
124+
return stripMd(slide.title)
111125
return `${deckTitle} — Slide ${no}`
112126
}
113127

0 commit comments

Comments
 (0)