-
-
Notifications
You must be signed in to change notification settings - Fork 605
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: more flexible ignores configuration (#2022)
Co-authored-by: Farnabaz <farnabaz@gmail.com>
- Loading branch information
1 parent
90d1598
commit 12d5a21
Showing
14 changed files
with
200 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Ignore | ||
|
||
The `/ignore` folder contains examples of ignored files. | ||
|
||
Navigating to these routes should result in a 404, and reloading any of these files should not trigger a content refresh: | ||
|
||
- [.dotted file](/features/ignored/dotted-file) | ||
- [-dashed file](/features/ignored/dashed-file) | ||
- [ignored file](/features/ignored/ignored-file) | ||
- [ignored folder](/features/ignored/folder) | ||
|
||
Navigating to these routes should show their content: | ||
|
||
- [included file](/features/ignored/included-file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
This file is ignored: | ||
|
||
- reason: default `ignores` pattern `^[.-]|:[.-]` | ||
- because: it matches the leading - |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
This file is ignored: | ||
|
||
- reason: default `ignores` pattern `^[.-]|:[.-]` | ||
- because: it matches the leading . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
This folder is ignored: | ||
|
||
- reason: custom `ignores` pattern `ignored:hidden` | ||
- because: it matches the folder path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
This file is ignored: | ||
|
||
- reason: custom `ignores` pattern `\\.bak$` | ||
- because: it matches the file extension |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
navigation: false | ||
--- | ||
|
||
This file is included |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Creates a predicate to test storage keys `foo:bar:baz` against configured `ignores` patterns | ||
*/ | ||
export function makeIgnored (ignores: string[], experimental = false): (key: string) => boolean { | ||
// filter empty | ||
ignores = ignores.map(e => e) | ||
|
||
// 2.7+ supports free regexp + slashes | ||
if (experimental) { | ||
const rxAll = ['/\\.', '/-', ...ignores].map(p => new RegExp(p)) | ||
return function isIgnored (key: string): boolean { | ||
const path = '/' + key.replaceAll(':', '/') | ||
return rxAll.some(rx => rx.test(path)) | ||
} | ||
} | ||
|
||
// 2.6 prefixed by unstorage delimiters | ||
const rxAll = ['\\.', '-', ...ignores].map(p => new RegExp(`^${p}|:${p}`)) | ||
return function isIgnored (key: string): boolean { | ||
return rxAll.some(rx => rx.test(key)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { describe, test, expect } from 'vitest' | ||
import { makeIgnored } from '../../src/runtime/utils/config' | ||
|
||
function run (key: string, pattern: string, result: boolean, experimental: boolean) { | ||
const predicate = makeIgnored([pattern], experimental) | ||
const label: string = experimental ? 'new format' : 'old format' | ||
test(label, () => { | ||
expect(predicate(key)).toBe(result) | ||
}) | ||
} | ||
|
||
function runOld (pattern: string, key: string, result: boolean) { | ||
return run(key, pattern, result, false) | ||
} | ||
|
||
function runNew (pattern: string, key: string, result: boolean) { | ||
return run(key, pattern, result, true) | ||
} | ||
|
||
export const testIgnores = () => { | ||
describe('Ignores Options', () => { | ||
describe('any file or folder that contains the word "hidden"', () => { | ||
const key = 'source:content:my-hidden-folder:index.md' | ||
runOld('.+hidden', key, true) | ||
runNew('hidden', key, true) | ||
}) | ||
|
||
describe('any file or folder prefixed with the word "hidden"', () => { | ||
const key = 'source:content:hidden-folder:index.md' | ||
runOld('hidden', key, true) | ||
runNew('/hidden', key, true) | ||
}) | ||
|
||
describe('any folder that exactly matches the word "hidden"', () => { | ||
const key = 'source:content:hidden:index.md' | ||
runOld('hidden:', key, true) | ||
runNew('/hidden/', key, true) | ||
}) | ||
|
||
describe('any file path matching "/path/to/file"', () => { | ||
const key = 'source:content:path:to:file.md' | ||
runOld('path:to:file', key, true) | ||
runNew('/path/to/file', key, true) | ||
}) | ||
|
||
describe('any file with the extension ".bak"', () => { | ||
const key = 'source:content:path:to:ignored.bak' | ||
runOld('.+\\.bak$', key, true) | ||
runNew('\\.bak$', key, true) | ||
}) | ||
|
||
describe('any file with a leading dash', () => { | ||
const key = 'source:content:path:to:-dash.txt' | ||
runOld('', key, true) | ||
runNew('', key, true) | ||
}) | ||
|
||
describe('any file with a leading dot', () => { | ||
const key = 'source:content:path:to:.dot.txt' | ||
runOld('', key, true) | ||
runNew('', key, true) | ||
}) | ||
}) | ||
} |