Skip to content

Commit 6dbc417

Browse files
committed
fix: use more resilient preexisting stripHtmlTags function
1 parent a76bd69 commit 6dbc417

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

server/utils/readme.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import sanitizeHtml from 'sanitize-html'
33
import { hasProtocol } from 'ufo'
44
import type { ReadmeResponse, TocItem } from '#shared/types/readme'
55
import { convertBlobOrFileToRawUrl, type RepositoryInfo } from '#shared/utils/git-providers'
6-
import { decodeHtmlEntities } from '#shared/utils/html'
6+
import { decodeHtmlEntities, stripHtmlTags } from '#shared/utils/html'
77
import { convertToEmoji } from '#shared/utils/emoji'
88
import { toProxiedImageUrl } from '#server/utils/image-proxy'
99

@@ -194,22 +194,6 @@ const ALLOWED_ATTR: Record<string, string[]> = {
194194
'p': ['align'],
195195
}
196196

197-
/**
198-
* Strip all HTML tags from a string, looping until stable to prevent
199-
* incomplete sanitization from nested/interleaved tags
200-
* (e.g. `<scr<script>ipt>` → `<script>` after one pass).
201-
*/
202-
function stripHtmlTags(text: string): string {
203-
const tagPattern = /<[^>]*>/g
204-
let result = text
205-
let previous: string
206-
do {
207-
previous = result
208-
result = result.replace(tagPattern, '')
209-
} while (result !== previous)
210-
return result
211-
}
212-
213197
/**
214198
* Generate a GitHub-style slug from heading text.
215199
* - Convert to lowercase

shared/utils/html.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ export function decodeHtmlEntities(text: string): string {
1212
return text.replace(/&(?:amp|lt|gt|quot|apos|nbsp|#39);/g, match => htmlEntities[match] || match)
1313
}
1414

15+
/**
16+
* Strip all HTML tags from a string, looping until stable to prevent
17+
* incomplete sanitization from nested/interleaved tags
18+
* (e.g. `<scr<script>ipt>` → `<script>` after one pass).
19+
*/
1520
export function stripHtmlTags(text: string): string {
16-
return text.replace(/<[^>]*>/g, '')
21+
const tagPattern = /<[^>]*>/g
22+
let result = text
23+
let previous: string
24+
do {
25+
previous = result
26+
result = result.replace(tagPattern, '')
27+
} while (result !== previous)
28+
return result
1729
}

0 commit comments

Comments
 (0)