import { createMarkdownItAsync } from 'markdown-it-async' import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises' import { tmpdir } from 'node:os' import path from 'node:path' import { processIncludes } from 'node/utils/processIncludes' describe('node/utils/processIncludes', () => { let root: string beforeEach(async () => { root = await mkdtemp(path.join(tmpdir(), 'vitepress-includes-')) }) afterEach(async () => { await rm(root, { recursive: true, force: true }) }) async function write(name: string, src: string) { await writeFile(path.join(root, name), src) } async function run(name: string) { const file = path.join(root, name) const src = await readFile(file, 'utf8') return processIncludes(createMarkdownItAsync(), root, src, file, [], false) } test('leaves a self-include unexpanded', async () => { await write('a.md', '# A\n\n\n') expect(await run('a.md')).toContain('') }) test('leaves circular includes unexpanded', async () => { await write('a.md', 'A-content\n\n\n') await write('b.md', 'B-content\n\n\n') const result = await run('a.md') expect(result).toContain('B-content') expect(result).toContain('') }) test('expands repeated includes outside the ancestor chain', async () => { await write( 'a.md', '\n\n' ) await write('b.md', 'B-content\n\n\n') await write('c.md', 'C-content\n\n\n') await write('d.md', 'D-content\n') expect((await run('a.md')).match(/D-content/g)).toHaveLength(2) }) })