fix(node): preserve string glob ignore patterns (#5435)

pull/5439/head
lllleolin-max 2 weeks ago committed by GitHub
parent 304207c497
commit 80c8a28fd6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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)

@ -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'
])
})
})

@ -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()
}

Loading…
Cancel
Save