Skip to content

Commit 0531e3f

Browse files
authored
fix: readme rendering result lacks anchor point jump (#994)
1 parent c12afc2 commit 0531e3f

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

server/utils/readme.ts

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -139,23 +139,24 @@ const ALLOWED_TAGS = [
139139
]
140140

141141
const ALLOWED_ATTR: Record<string, string[]> = {
142-
a: ['href', 'title', 'target', 'rel'],
143-
img: ['src', 'alt', 'title', 'width', 'height', 'align'],
144-
source: ['src', 'srcset', 'type', 'media'],
145-
button: ['class', 'title', 'type', 'aria-label', 'data-copy'],
146-
th: ['colspan', 'rowspan', 'align'],
147-
td: ['colspan', 'rowspan', 'align'],
148-
h3: ['id', 'data-level', 'align'],
149-
h4: ['id', 'data-level', 'align'],
150-
h5: ['id', 'data-level', 'align'],
151-
h6: ['id', 'data-level', 'align'],
152-
blockquote: ['data-callout'],
153-
details: ['open'],
154-
code: ['class'],
155-
pre: ['class', 'style'],
156-
span: ['class', 'style'],
157-
div: ['class', 'style', 'align'],
158-
p: ['align'],
142+
'*': ['id'], // Allow id on all tags
143+
'a': ['href', 'title', 'target', 'rel'],
144+
'img': ['src', 'alt', 'title', 'width', 'height', 'align'],
145+
'source': ['src', 'srcset', 'type', 'media'],
146+
'button': ['class', 'title', 'type', 'aria-label', 'data-copy'],
147+
'th': ['colspan', 'rowspan', 'align'],
148+
'td': ['colspan', 'rowspan', 'align'],
149+
'h3': ['data-level', 'align'],
150+
'h4': ['data-level', 'align'],
151+
'h5': ['data-level', 'align'],
152+
'h6': ['data-level', 'align'],
153+
'blockquote': ['data-callout'],
154+
'details': ['open'],
155+
'code': ['class'],
156+
'pre': ['class', 'style'],
157+
'span': ['class', 'style'],
158+
'div': ['class', 'style', 'align'],
159+
'p': ['align'],
159160
}
160161

161162
// GitHub-style callout types
@@ -397,6 +398,14 @@ ${html}
397398

398399
const rawHtml = marked.parse(content) as string
399400

401+
// Helper to prefix id attributes with 'user-content-'
402+
const prefixId = (tagName: string, attribs: sanitizeHtml.Attributes) => {
403+
if (attribs.id && !attribs.id.startsWith('user-content-')) {
404+
attribs.id = `user-content-${attribs.id}`
405+
}
406+
return { tagName, attribs }
407+
}
408+
400409
const sanitized = sanitizeHtml(rawHtml, {
401410
allowedTags: ALLOWED_TAGS,
402411
allowedAttributes: ALLOWED_ATTR,
@@ -436,6 +445,11 @@ ${html}
436445
}
437446
return { tagName, attribs }
438447
},
448+
div: prefixId,
449+
p: prefixId,
450+
span: prefixId,
451+
section: prefixId,
452+
article: prefixId,
439453
},
440454
})
441455

0 commit comments

Comments
 (0)