Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

better sanitisation

Blue Mouse31fe3cd048b7
diff --git a/web/app/utils/toc.ts b/web/app/utils/toc.ts
index bb6b4f2e5..e1525f43c 100644
--- a/web/app/utils/toc.ts
+++ b/web/app/utils/toc.ts
@@ -14,6 +14,20 @@ function escapeHtml(unsafe: string): string {
.replace(/'/g, ''')
}
+function stripHtmlTags(input: string): string {
+ const tagRegex = /<[^>]*>/g
+ let result = input
+ let previous: string
+
+ // Repeatedly remove HTML tags until no more changes occur
+ do {
+ previous = result
+ result = result.replace(tagRegex, '')
+ } while (result !== previous)
+
+ return result.trim()
+}
+
export function generateSlug(text: string): string {
return text
.toLowerCase()
@@ -65,7 +79,7 @@ export function extractTableOfContents(html: string): TocItem[] {
while ((match = headerRegex.exec(html)) !== null) {
const level = parseInt(match[1])
const existingId = match[2]
- const text = match[3].replace(/<[^>]*>/g, '').trim() // Remove HTML tags
+ const text = stripHtmlTags(match[3])
const id = existingId || generateSlug(text)
const tocItem: TocItem = {
@@ -102,7 +116,7 @@ export function ensureHeaderIds(html: string): string {
return match // Already has an ID
}
- const text = content.replace(/<[^>]*>/g, '').trim()
+ const text = stripHtmlTags(content)
const id = generateSlug(text)
return `<h${level} id="${id}"${attrs}>${content}</h${level}>`