Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/composables/useMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ function stripAndEscapeHtml(text: string, packageName?: string): string {
// Only match tags that start with a letter or / (to avoid matching things like "a < b > c")
stripped = stripped.replace(/<\/?[a-z][^>]*>/gi, '')

// Strip HTML comments: <!-- ... --> (including unclosed comments from truncation)
stripped = stripped.replace(/<!--[\s\S]*?(-->|$)/g, '')

if (packageName) {
// Trim first to handle leading/trailing whitespace from stripped HTML
stripped = stripped.trim()
Expand Down
32 changes: 32 additions & 0 deletions test/nuxt/composables/use-markdown.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,36 @@ describe('useMarkdown', () => {
expect(processed.value).toBe('bold and <strong>also bold</strong>')
})
})

describe('HTML comment stripping', () => {
it('strips HTML comments', () => {
const processed = useMarkdown({ text: '<!-- automd:badges color=yellow -->A library' })
expect(processed.value).toBe('A library')
})

it('strips HTML comments from the middle of text', () => {
const processed = useMarkdown({ text: 'Before <!-- comment --> after' })
expect(processed.value).toBe('Before after')
})

it('strips multiple HTML comments', () => {
const processed = useMarkdown({ text: '<!-- first -->Text <!-- second -->here' })
expect(processed.value).toBe('Text here')
})

it('strips multiline HTML comments', () => {
const processed = useMarkdown({ text: '<!-- multi\nline\ncomment -->Text' })
expect(processed.value).toBe('Text')
})

it('returns empty string when description is only a comment', () => {
const processed = useMarkdown({ text: '<!-- automd:badges color=yellow -->' })
expect(processed.value).toBe('')
})

it('strips unclosed HTML comments (truncated)', () => {
const processed = useMarkdown({ text: 'A library <!-- automd:badges color=yel' })
expect(processed.value).toBe('A library ')
})
})
})
Loading