Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: <!-- ... -->
stripped = stripped.replace(/<!--[\s\S]*?-->/g, '')
Copy link
Copy Markdown
Member

@alexdln alexdln Feb 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I understand, when there's no description in package.json, npm and algolia takes the description from Markdown. If the comment is long, only the opening tag will remain. What do you think about additional check so that in this case it's truncated to the end?


p.p.s. just note - [\s\S]* is equal to [\s\S]{0,}, i.e. * means that there are more than 0 matches of any character. Therefore, the optional character (?) is not needed here. The optional character may be needed when testing with + instead of *, that equal to [\s\S]{1,} (at least one any character)

Copy link
Copy Markdown
Contributor Author

@TheAlexLichter TheAlexLichter Feb 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I was consider adding it but didn't know how the description is retrieved. Let me update the logic.

Note on the regex: *? is used to be non-greedy and not match more than wanted.


if (packageName) {
// Trim first to handle leading/trailing whitespace from stripped HTML
stripped = stripped.trim()
Expand Down
27 changes: 27 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,31 @@ 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('')
})
})
})
Loading