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
2 changes: 1 addition & 1 deletion app/components/Compare/PackageSelector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ onClickOutside(containerRef, () => {
v-if="result.description"
class="text-xs text-fg-muted truncate mt-0.5 w-full block"
>
{{ decodeHtmlEntities(result.description) }}
{{ stripHtmlTags(decodeHtmlEntities(result.description)) }}
</span>
</ButtonBase>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/components/Package/TableRow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const allMaintainersText = computed(() => {
v-if="isColumnVisible('description')"
class="py-2 px-3 text-sm text-fg-muted max-w-xs truncate"
>
{{ decodeHtmlEntities(pkg.description || '-') }}
{{ stripHtmlTags(decodeHtmlEntities(pkg.description || '-')) }}
Copy link
Copy Markdown
Contributor

@vmrjnvc vmrjnvc Feb 27, 2026

Choose a reason for hiding this comment

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

now with this changes there could be full empty descriptions displayed, but there should be dash if there is no description

Image

so correct should be {{ stripHtmlTags(decodeHtmlEntities(pkg.description || '')) || '-'}} because if there is just html in the description, all will be removed and we don't have fallback

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

good catch! would you like to open a quick PR for that? 🙏🏼

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

sure, no problem!

here #1763

</td>

<!-- Downloads -->
Expand Down
18 changes: 1 addition & 17 deletions server/utils/readme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import sanitizeHtml from 'sanitize-html'
import { hasProtocol } from 'ufo'
import type { ReadmeResponse, TocItem } from '#shared/types/readme'
import { convertBlobOrFileToRawUrl, type RepositoryInfo } from '#shared/utils/git-providers'
import { decodeHtmlEntities } from '#shared/utils/html'
import { decodeHtmlEntities, stripHtmlTags } from '#shared/utils/html'
import { convertToEmoji } from '#shared/utils/emoji'
import { toProxiedImageUrl } from '#server/utils/image-proxy'

Expand Down Expand Up @@ -194,22 +194,6 @@ const ALLOWED_ATTR: Record<string, string[]> = {
'p': ['align'],
}

/**
* Strip all HTML tags from a string, looping until stable to prevent
* incomplete sanitization from nested/interleaved tags
* (e.g. `<scr<script>ipt>` → `<script>` after one pass).
*/
function stripHtmlTags(text: string): string {
const tagPattern = /<[^>]*>/g
let result = text
let previous: string
do {
previous = result
result = result.replace(tagPattern, '')
} while (result !== previous)
return result
}

/**
* Generate a GitHub-style slug from heading text.
* - Convert to lowercase
Expand Down
16 changes: 16 additions & 0 deletions shared/utils/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,19 @@ const htmlEntities: Record<string, string> = {
export function decodeHtmlEntities(text: string): string {
return text.replace(/&(?:amp|lt|gt|quot|apos|nbsp|#39);/g, match => htmlEntities[match] || match)
}

/**
* Strip all HTML tags from a string, looping until stable to prevent
* incomplete sanitization from nested/interleaved tags
* (e.g. `<scr<script>ipt>` → `<script>` after one pass).
*/
export function stripHtmlTags(text: string): string {
Comment thread
danielroe marked this conversation as resolved.
const tagPattern = /<[^>]*>/g
let result = text
let previous: string
do {
previous = result
result = result.replace(tagPattern, '')
} while (result !== previous)
return result
}
25 changes: 24 additions & 1 deletion test/unit/shared/utils/html.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { decodeHtmlEntities } from '../../../../shared/utils/html'
import { decodeHtmlEntities, stripHtmlTags } from '../../../../shared/utils/html'

describe('decodeHtmlEntities', () => {
it.each([
Expand All @@ -26,3 +26,26 @@ describe('decodeHtmlEntities', () => {
expect(decodeHtmlEntities('&unknown;')).toBe('&unknown;')
})
})

describe('stripHtmlTags', () => {
it('removes simple HTML tags', () => {
expect(stripHtmlTags('<b>bold</b>')).toBe('bold')
})

it('removes anchor tags keeping text content', () => {
expect(stripHtmlTags('<a href="https://example.com">link</a>')).toBe('link')
})

it('removes self-closing tags', () => {
expect(stripHtmlTags('before<br/>after')).toBe('beforeafter')
})

it('leaves plain text unchanged', () => {
expect(stripHtmlTags('no tags here')).toBe('no tags here')
})

it('works with decodeHtmlEntities to clean descriptions', () => {
const raw = '&lt;a href=&quot;url&quot;&gt;link&lt;/a&gt; and text'
expect(stripHtmlTags(decodeHtmlEntities(raw))).toBe('link and text')
})
})
Loading