Skip to content

Commit 12d5a21

Browse files
feat: more flexible ignores configuration (#2022)
Co-authored-by: Farnabaz <[email protected]>
1 parent 90d1598 commit 12d5a21

File tree

14 files changed

+200
-23
lines changed

14 files changed

+200
-23
lines changed

docs/content/4.api/3.configuration.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,42 @@ It is highly recommended to prevent modifying default source. If you want to loa
120120
- Type: `string[] | object[]`{lang=ts}
121121
- Default: `['\\.', '-']`{lang=ts}
122122

123-
List of ignore pattern that will be used for excluding content from parsing and rendering.
123+
List of ignore patterns to exclude content from parsing, rendering and watching.
124+
125+
Note that under the hood:
126+
127+
- paths are managed by [unstorage](https://unstorage.unjs.io/usage#usage-1), so you will need to use `:` in place of `/`
128+
- patterns are converted to Regular Expressions, so you will need to escape appropriately
129+
- patterns are actually _prefixes_; use a match-all pattern to negate this (see final example)
130+
131+
```ts [nuxt.config.ts]
132+
export default defineNuxtConfig({
133+
content: {
134+
ignores: [
135+
'hidden', // any file or folder prefixed with the word "hidden"
136+
'hidden:', // any folder that exactly matches the word "hidden"
137+
'path:to:file', // any file path matching "/path/to/file"
138+
'.+\\.bak$', // any file with the extension ".bak"
139+
]
140+
}
141+
})
142+
```
143+
144+
From version 2.7 `ignores` will support a more flexible, idiomatic format.
145+
146+
You can use this from version 2.6 using the `experimental.advancedIgnoresPattern` flag:
124147

125148
```ts [nuxt.config.ts]
126149
export default defineNuxtConfig({
127150
content: {
151+
experimental: {
152+
advancedIgnoresPattern: true
153+
},
128154
ignores: [
129-
'ghost'
155+
'hidden', // any file or folder that contains the word "hidden"
156+
'/hidden/', // any folder that exactly matches the word "hidden"
157+
'/path/to/file', // any file path matching "/path/to/file"
158+
'\\.bak$', // any file with the extension ".bak"
130159
]
131160
}
132161
})
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Ignore
2+
3+
The `/ignore` folder contains examples of ignored files.
4+
5+
Navigating to these routes should result in a 404, and reloading any of these files should not trigger a content refresh:
6+
7+
- [.dotted file](/features/ignored/dotted-file)
8+
- [-dashed file](/features/ignored/dashed-file)
9+
- [ignored file](/features/ignored/ignored-file)
10+
- [ignored folder](/features/ignored/folder)
11+
12+
Navigating to these routes should show their content:
13+
14+
- [included file](/features/ignored/included-file)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This file is ignored:
2+
3+
- reason: default `ignores` pattern `^[.-]|:[.-]`
4+
- because: it matches the leading -
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This file is ignored:
2+
3+
- reason: default `ignores` pattern `^[.-]|:[.-]`
4+
- because: it matches the leading .
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This folder is ignored:
2+
3+
- reason: custom `ignores` pattern `ignored:hidden`
4+
- because: it matches the folder path
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This file is ignored:
2+
3+
- reason: custom `ignores` pattern `\\.bak$`
4+
- because: it matches the file extension
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
navigation: false
3+
---
4+
5+
This file is included

playground/basic/nuxt.config.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@ export default defineNuxtConfig({
99
driver: 'fs',
1010
base: resolve(__dirname, 'content-fa')
1111
}
12-
}
12+
},
13+
14+
experimental: {
15+
advancedIgnoresPattern: true
16+
},
17+
18+
ignores: [
19+
'\\.bak$',
20+
'ignored/folder'
21+
]
1322
},
1423
typescript: {
1524
includeWorkspace: true

playground/basic/pages/[...slug].vue

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
<div class="content-page">
33
<ContentDoc>
44
<template #not-found>
5-
Back
6-
<NuxtLink to="/">
7-
Home page
8-
</NuxtLink>
5+
<p>Not found!</p>
6+
<p>
7+
Go:
8+
<NuxtLink to="#" @click="$router.go(-1)">
9+
Back</NuxtLink>
10+
or
11+
<NuxtLink to="/">
12+
Home</NuxtLink>
13+
</p>
914
</template>
1015
</ContentDoc>
1116
</div>

src/module.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { createStorage } from 'unstorage'
2222
import { joinURL, withLeadingSlash, withTrailingSlash } from 'ufo'
2323
import type { Component } from '@nuxt/schema'
2424
import { name, version } from '../package.json'
25+
import { makeIgnored } from './runtime/utils/config'
2526
import {
2627
CACHE_VERSION,
2728
createWebSocket,
@@ -67,16 +68,18 @@ export interface ModuleOptions {
6768
ws: Partial<ListenOptions>
6869
}
6970
/**
70-
* Contents can located in multiple places, in multiple directories or even in remote git repositories.
71+
* Contents can be located in multiple places, in multiple directories or even in remote git repositories.
7172
* Using sources option you can tell Content module where to look for contents.
7273
*
7374
* @default ['content']
7475
*/
7576
sources: Record<string, MountOptions> | Array<string | MountOptions>
7677
/**
77-
* List of ignore pattern that will be used for excluding content from parsing and rendering.
78+
* List of ignore patterns that will be used to exclude content from parsing, rendering and watching.
7879
*
79-
* @default ['\\.', '-']
80+
* Note that files with a leading . or - are ignored by default
81+
*
82+
* @default []
8083
*/
8184
ignores: Array<string>
8285
/**
@@ -219,7 +222,8 @@ export interface ModuleOptions {
219222
},
220223
experimental: {
221224
clientDB: boolean
222-
stripQueryParameters: boolean
225+
stripQueryParameters: boolean,
226+
advancedIgnoresPattern: boolean
223227
}
224228
}
225229

@@ -258,7 +262,7 @@ export default defineNuxtModule<ModuleOptions>({
258262
}
259263
},
260264
sources: {},
261-
ignores: ['\\.', '-'],
265+
ignores: [],
262266
locales: [],
263267
defaultLocale: undefined,
264268
highlight: false,
@@ -280,7 +284,8 @@ export default defineNuxtModule<ModuleOptions>({
280284
documentDriven: false,
281285
experimental: {
282286
clientDB: false,
283-
stripQueryParameters: false
287+
stripQueryParameters: false,
288+
advancedIgnoresPattern: false
284289
}
285290
},
286291
async setup (options, nuxt) {
@@ -600,7 +605,8 @@ export default defineNuxtModule<ModuleOptions>({
600605
integrity: buildIntegrity,
601606
experimental: {
602607
stripQueryParameters: options.experimental.stripQueryParameters,
603-
clientDB: options.experimental.clientDB && nuxt.options.ssr === false
608+
clientDB: options.experimental.clientDB && nuxt.options.ssr === false,
609+
advancedIgnoresPattern: options.experimental.advancedIgnoresPattern
604610
},
605611
api: {
606612
baseURL: options.api.baseURL
@@ -632,6 +638,13 @@ export default defineNuxtModule<ModuleOptions>({
632638
tailwindConfig.content.push(resolve(nuxt.options.buildDir, 'content-cache', 'parsed/**/*.md'))
633639
})
634640

641+
// ignore files
642+
const { advancedIgnoresPattern } = contentContext.experimental
643+
const isIgnored = makeIgnored(contentContext.ignores, advancedIgnoresPattern)
644+
if (contentContext.ignores.length && !advancedIgnoresPattern) {
645+
logger.warn('The `ignores` config is being made more flexible in version 2.7. See the docs for more information: `https://content.nuxtjs.org/api/configuration#ignores`')
646+
}
647+
635648
// Setup content dev module
636649
if (!nuxt.options.dev) {
637650
nuxt.hook('build:before', async () => {
@@ -648,11 +661,8 @@ export default defineNuxtModule<ModuleOptions>({
648661

649662
// Filter invalid characters & ignore patterns
650663
const invalidKeyCharacters = "'\"?#/".split('')
651-
const contentIgnores: Array<RegExp> = contentContext.ignores.map((p: any) =>
652-
typeof p === 'string' ? new RegExp(`^${p}|:${p}`) : p
653-
)
654664
keys = keys.filter((key) => {
655-
if (key.startsWith('preview:') || contentIgnores.some(prefix => prefix.test(key))) {
665+
if (key.startsWith('preview:') || isIgnored(key)) {
656666
return false
657667
}
658668
if (invalidKeyCharacters.some(ik => key.includes(ik))) {
@@ -699,7 +709,7 @@ export default defineNuxtModule<ModuleOptions>({
699709
// Watch contents
700710
await nitro.storage.watch(async (event: WatchEvent, key: string) => {
701711
// Ignore events that are not related to content
702-
if (!key.startsWith(MOUNT_PREFIX)) {
712+
if (!key.startsWith(MOUNT_PREFIX) || isIgnored(key)) {
703713
return
704714
}
705715
key = key.substring(MOUNT_PREFIX.length)
@@ -718,6 +728,7 @@ interface ModulePublicRuntimeConfig {
718728
experimental: {
719729
stripQueryParameters: boolean
720730
clientDB: boolean
731+
advancedIgnoresPattern: boolean
721732
}
722733

723734
defaultLocale: ModuleOptions['defaultLocale']

0 commit comments

Comments
 (0)