import { describe, expect, it } from 'vitest' import { createMarkdown } from '../../src/index.js' describe('anchor plugin', () => { it('should add ids and header anchors to all heading levels', () => { const md = createMarkdown() expect( md.render('# h1\n## h2\n### h3\n#### h4\n##### h5\n###### h6\n'), ).toBe( [ '

h1

', '

h2

', '

h3

', '

h4

', '
h5
', '
h6
', ].join('\n') + '\n', ) }) it('should generate unique slugs for duplicate headings', () => { const md = createMarkdown() expect(md.render('# foo\n## foo\n### foo\n')).toBe( [ '

foo

', '

foo

', '

foo

', ].join('\n') + '\n', ) }) it('should not wrap a heading that already contains a link', () => { const md = createMarkdown() // wrapping the existing link would produce invalid nested `` elements expect(md.render('# [vuepress](https://vuepress.vuejs.org)\n')).toBe( '

vuepress

\n', ) }) it('should fall back to the `heading` placeholder for empty headings', () => { const md = createMarkdown() expect(md.render('#\n')).toBe( '

\n', ) }) it('should fall back to the `heading` placeholder for image-only headings', () => { const md = createMarkdown() expect(md.render('# ![alt](https://example.com/foo.png)\n')).toBe( '

alt

\n', ) }) it('should use the custom slugify function', () => { const md = createMarkdown({ slugify: (str) => `custom-${str.toLowerCase().replaceAll(' ', '-')}`, }) expect(md.render('# Foo Bar\n')).toBe( '

Foo Bar

\n', ) }) it('should only add anchors to the configured heading levels', () => { const md = createMarkdown({ anchor: { level: [2, 3] } }) expect(md.render('# h1\n## h2\n#### h4\n')).toBe( [ '

h1

', '

h2

', '

h4

', ].join('\n') + '\n', ) }) it('should let user options override the default anchor options', () => { const md = createMarkdown({ anchor: { tabIndex: false } }) expect(md.render('# foo\n')).toBe( '

foo

\n', ) }) it('should record used slugs in the markdown env', () => { const md = createMarkdown() const env = {} md.render('# foo\n# foo\n', env) expect(env).toMatchObject({ markdownItAnchor: { slugs: { 'foo': true, 'foo-1': true, }, }, }) }) })