mirror of https://github.com/vuejs/vitepress
Allows custom themes to opt out of markup and behavior added on top of vanilla markdown rendering: - `anchor`, `emoji`, `toc`, `component`, `image` now also accept `false` - new `preWrapper` and `snippet` boolean options (default true) - `lineNumbers` is a no-op when `preWrapper` is disabled, as its markup depends on the wrapper close #4484 close #4556 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>pull/5335/head
parent
e235dbeb8a
commit
b8d9c8f9a9
@ -0,0 +1,105 @@
|
||||
import {
|
||||
createMarkdownRenderer,
|
||||
disposeMdItInstance,
|
||||
type MarkdownOptions
|
||||
} from 'node/markdown/markdown'
|
||||
|
||||
async function render(src: string, options: MarkdownOptions = {}) {
|
||||
disposeMdItInstance()
|
||||
const md = await createMarkdownRenderer('.', {
|
||||
highlight: (code) => code,
|
||||
...options
|
||||
})
|
||||
return md.renderAsync(src)
|
||||
}
|
||||
|
||||
describe('node/markdown/markdown', () => {
|
||||
describe('disabling built-in plugins', () => {
|
||||
test('anchor', async () => {
|
||||
const enabled = await render('# Hello World')
|
||||
expect(enabled).toContain('id="hello-world"')
|
||||
expect(enabled).toContain('header-anchor')
|
||||
|
||||
const disabled = await render('# Hello World', { anchor: false })
|
||||
expect(disabled).not.toContain('id=')
|
||||
expect(disabled).not.toContain('header-anchor')
|
||||
})
|
||||
|
||||
test('attrs', async () => {
|
||||
const enabled = await render('## Title {#custom-id}')
|
||||
expect(enabled).toContain('id="custom-id"')
|
||||
|
||||
const disabled = await render('## Title {#custom-id}', { attrs: false })
|
||||
expect(disabled).not.toContain('id="custom-id"')
|
||||
expect(disabled).toContain('{#custom-id}')
|
||||
})
|
||||
|
||||
test('emoji', async () => {
|
||||
expect(await render(':tada:')).toContain('🎉')
|
||||
expect(await render(':tada:', { emoji: false })).toContain(':tada:')
|
||||
})
|
||||
|
||||
test('toc', async () => {
|
||||
const src = '# Title\n\n[[toc]]'
|
||||
expect(await render(src)).toContain('table-of-contents')
|
||||
|
||||
const disabled = await render(src, { toc: false })
|
||||
expect(disabled).not.toContain('table-of-contents')
|
||||
expect(disabled).toContain('[[toc]]')
|
||||
})
|
||||
|
||||
test('preWrapper', async () => {
|
||||
const src = '```js\nconst a = 1\n```'
|
||||
const enabled = await render(src)
|
||||
expect(enabled).toContain('<div class="language-js">')
|
||||
expect(enabled).toContain('class="copy"')
|
||||
|
||||
const disabled = await render(src, { preWrapper: false })
|
||||
expect(disabled).not.toContain('<div class="language-js">')
|
||||
expect(disabled).not.toContain('class="copy"')
|
||||
})
|
||||
|
||||
test('preWrapper disables line numbers with it', async () => {
|
||||
const src = '```js\nconst a = 1\n```'
|
||||
const enabled = await render(src, { lineNumbers: true })
|
||||
expect(enabled).toContain('line-numbers-wrapper')
|
||||
|
||||
const disabled = await render(src, {
|
||||
preWrapper: false,
|
||||
lineNumbers: true
|
||||
})
|
||||
expect(disabled).not.toContain('line-numbers-wrapper')
|
||||
})
|
||||
|
||||
test('snippet', async () => {
|
||||
const disabled = await render('<<< ./foo.js', { snippet: false })
|
||||
expect(disabled).toContain('<<< ./foo.js')
|
||||
})
|
||||
|
||||
test('image', async () => {
|
||||
const src = ''
|
||||
const enabled = await render(src, { image: { lazyLoad: true } })
|
||||
expect(enabled).toContain('loading="lazy"')
|
||||
|
||||
const disabled = await render(src, { image: false })
|
||||
expect(disabled).not.toContain('loading="lazy"')
|
||||
})
|
||||
|
||||
test('component', async () => {
|
||||
const src = 'text\n<MyComponent/>\nmore'
|
||||
const enabled = await render(src)
|
||||
expect(enabled).toContain('</p>\n<MyComponent/><p>')
|
||||
|
||||
const disabled = await render(src, { component: false })
|
||||
expect(disabled).toContain('<p>text\n<MyComponent/>\nmore</p>')
|
||||
})
|
||||
|
||||
test('tableTabIndex', async () => {
|
||||
const src = '| a |\n| --- |\n| b |'
|
||||
expect(await render(src)).toContain('tabindex="0"')
|
||||
expect(await render(src, { tableTabIndex: false })).not.toContain(
|
||||
'tabindex'
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
Loading…
Reference in new issue