Skip to content

Commit f878368

Browse files
committed
fix: handle possible collisions
1 parent 5d1e15f commit f878368

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

server/utils/readme.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ function slugify(text: string): string {
184184
function resolveUrl(url: string, packageName: string, repoInfo?: RepositoryInfo): string {
185185
if (!url) return url
186186
if (url.startsWith('#')) {
187-
return url
187+
// Prefix anchor links to match heading IDs (avoids collision with page IDs)
188+
return `#user-content-${url.slice(1)}`
188189
}
189190
if (hasProtocol(url, { acceptRelative: true })) {
190191
try {
@@ -292,7 +293,11 @@ export async function renderReadmeHtml(
292293
// Handle duplicate slugs (GitHub-style: foo, foo-1, foo-2)
293294
const count = usedSlugs.get(slug) ?? 0
294295
usedSlugs.set(slug, count + 1)
295-
const id = count === 0 ? slug : `${slug}-${count}`
296+
const uniqueSlug = count === 0 ? slug : `${slug}-${count}`
297+
298+
// Prefix with 'user-content-' to avoid collisions with page IDs
299+
// (e.g., #install, #dependencies, #versions are used by the package page)
300+
const id = `user-content-${uniqueSlug}`
296301

297302
return `<h${semanticLevel} id="${id}" data-level="${depth}">${text}</h${semanticLevel}>\n`
298303
}

0 commit comments

Comments
 (0)