Skip to content

Commit 859e9a4

Browse files
committed
test: cover links in renderer heading
1 parent 9fdbec4 commit 859e9a4

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

test/unit/server/utils/readme.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,40 @@ describe('HTML output', () => {
591591
expect(result.html).toContain('id="user-content-api-1"')
592592
})
593593

594+
describe('heading anchors (renderer.heading)', () => {
595+
it('strips a full-line anchor wrapper and uses inner text for slug, toc, and permalink', async () => {
596+
const markdown = '## <a href="https://example.com">My Section</a>'
597+
const result = await renderReadmeHtml(markdown, 'test-pkg')
598+
599+
expect(result.toc).toEqual([{ text: 'My Section', depth: 2, id: 'user-content-my-section' }])
600+
expect(result.html).toBe(
601+
`<h3 id="user-content-my-section" data-level="2"><a href="#user-content-my-section">My Section</a></h3>\n`,
602+
)
603+
})
604+
605+
it('uses a trailing empty permalink when heading content already includes a link (no nested anchors)', async () => {
606+
const markdown = '### See <a href="https://example.com">docs</a> for more'
607+
const result = await renderReadmeHtml(markdown, 'test-pkg')
608+
609+
expect(result.toc).toEqual([
610+
{ text: 'See docs for more', depth: 3, id: 'user-content-see-docs-for-more' },
611+
])
612+
expect(result.html).toBe(
613+
`<h3 id="user-content-see-docs-for-more" data-level="3">See <a href="https://example.com" rel="nofollow noreferrer noopener" target="_blank">docs</a> for more<a href="#user-content-see-docs-for-more"></a></h3>\n`,
614+
)
615+
})
616+
617+
it('applies the same permalink pattern to raw HTML headings that contain links', async () => {
618+
const md = '<h2>Guide: <a href="https://example.com/page">page</a></h2>'
619+
const result = await renderReadmeHtml(md, 'test-pkg')
620+
621+
expect(result.toc).toEqual([{ text: 'Guide: page', depth: 2, id: 'user-content-guide-page' }])
622+
expect(result.html).toBe(
623+
'<h3 id="user-content-guide-page" data-level="2">Guide: <a href="https://example.com/page" rel="nofollow noreferrer noopener" target="_blank">page</a><a href="#user-content-guide-page"></a></h3>',
624+
)
625+
})
626+
})
627+
594628
it('preserves supported attributes on raw HTML headings', async () => {
595629
const md = '<h1 align="center">My Package</h1>'
596630
const result = await renderReadmeHtml(md, 'test-pkg')

0 commit comments

Comments
 (0)