diff --git a/__tests__/unit/node/contentLoader.test.ts b/__tests__/unit/node/contentLoader.test.ts index 387a58c83..701d948b8 100644 --- a/__tests__/unit/node/contentLoader.test.ts +++ b/__tests__/unit/node/contentLoader.test.ts @@ -1,4 +1,4 @@ -import { mkdtemp, rm, writeFile } from 'node:fs/promises' +import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises' import { tmpdir } from 'node:os' import path from 'node:path' @@ -29,6 +29,18 @@ describe('node/contentLoader', () => { ;(global as any).VITEPRESS_CONFIG = siteConfig } + test('excludes drafts with a string globOptions.ignore pattern', async () => { + await setup(false) + await mkdir(path.join(root!, 'drafts')) + await writeFile(path.join(root!, 'drafts/post.md'), '# Unpublished') + + const data = await createContentLoader('**/*.md', { + globOptions: { cwd: root, ignore: 'drafts/**' } + }).load() + + expect(data.map((page) => page.url)).toEqual(['/', '/other.html']) + }) + test('rendered internal links get .html when cleanUrls is false', async () => { await setup(false) diff --git a/__tests__/unit/node/utils/glob.test.ts b/__tests__/unit/node/utils/glob.test.ts new file mode 100644 index 000000000..a53514643 --- /dev/null +++ b/__tests__/unit/node/utils/glob.test.ts @@ -0,0 +1,38 @@ +import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import path from 'node:path' + +import { glob } from 'node/utils/glob' + +describe('node/utils/glob', () => { + let root: string + + beforeEach(async () => { + root = await mkdtemp(path.join(tmpdir(), 'vitepress-glob-')) + for (const dir of ['drafts', 'dist', 'node_modules']) { + await mkdir(path.join(root, dir)) + await writeFile(path.join(root, dir, 'post.md'), '# Post') + } + await writeFile(path.join(root, 'published.md'), '# Published') + }) + + afterEach(async () => { + await rm(root, { recursive: true, force: true }) + }) + + test.each([{ ignore: 'drafts/**' }, { ignore: ['drafts/**'] }])( + 'accepts an ignore pattern as $ignore', + async ({ ignore }) => { + expect(await glob(['**/*.md'], { cwd: root, ignore })).toEqual([ + 'published.md' + ]) + } + ) + + test('keeps the default exclusions without a custom ignore pattern', async () => { + expect(await glob(['**/*.md'], { cwd: root })).toEqual([ + 'drafts/post.md', + 'published.md' + ]) + }) +}) diff --git a/src/node/utils/glob.ts b/src/node/utils/glob.ts index 47623d6ad..647bf533f 100644 --- a/src/node/utils/glob.ts +++ b/src/node/utils/glob.ts @@ -33,7 +33,7 @@ export async function glob( await _glob(patterns, { expandDirectories: false, ...options, - ignore: ['**/node_modules/**', '**/dist/**', ...(options?.ignore || [])] + ignore: ['**/node_modules/**', '**/dist/**'].concat(options?.ignore || []) }) ).sort() }