diff --git a/docs/reference/site-config.md b/docs/reference/site-config.md index 59f176a2..7c3fa190 100644 --- a/docs/reference/site-config.md +++ b/docs/reference/site-config.md @@ -282,10 +282,12 @@ export default { ### ignoreDeadLinks -- Type: `boolean | 'localhostLinks'` +- Type: `boolean | 'localhostLinks' | (string | RegExp | ((link: string) => boolean))[]` - Default: `false` -When set to `true`, VitePress will not fail builds due to dead links. When set to `'localhostLinks'`, the build will fail on dead links, but won't check `localhost` links. +When set to `true`, VitePress will not fail builds due to dead links. + +When set to `'localhostLinks'`, the build will fail on dead links, but won't check `localhost` links. ```ts export default { @@ -293,6 +295,25 @@ export default { } ``` +It can also be an array of extact url string, regex patterns, or custom filter functions. + +```ts +export default { + ignoreDeadLinks: [ + // ignore exact url "/playground" + '/playground', + // ignore all localhost links + /^https?:\/\/localhost/, + // ignore all links include "/repl/"" + /\/repl\//, + // custom function, ignore all links include "ignore" + (url) => { + return url.toLowerCase().includes('ignore') + } + ] +} +``` + ### mpa - Type: `boolean` diff --git a/package.json b/package.json index e5501dae..1149519a 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "1.0.0-alpha.62", "description": "Vite & Vue powered static site generator", "type": "module", - "packageManager": "pnpm@7.30.0", + "packageManager": "pnpm@7.30.3", "main": "dist/node/index.js", "types": "types/index.d.ts", "exports": { diff --git a/src/node/config.ts b/src/node/config.ts index 763aea03..63b8e9ec 100644 --- a/src/node/config.ts +++ b/src/node/config.ts @@ -85,7 +85,10 @@ export interface UserConfig * * @default false */ - ignoreDeadLinks?: boolean | 'localhostLinks' + ignoreDeadLinks?: + | boolean + | 'localhostLinks' + | (string | RegExp | ((link: string) => boolean))[] /** * Don't force `.html` on URLs. diff --git a/src/node/markdownToVue.ts b/src/node/markdownToVue.ts index 8df264bb..0d48de5a 100644 --- a/src/node/markdownToVue.ts +++ b/src/node/markdownToVue.ts @@ -125,19 +125,36 @@ export async function createMarkdownToVueRenderFn( deadLinks.push(url) } + function shouldIgnoreDeadLink(url: string) { + if (!siteConfig?.ignoreDeadLinks) { + return false + } + if (siteConfig.ignoreDeadLinks === true) { + return true + } + if (siteConfig.ignoreDeadLinks === 'localhostLinks') { + return url.replace(EXTERNAL_URL_RE, '').startsWith('//localhost') + } + + return siteConfig.ignoreDeadLinks.some((ignore) => { + if (typeof ignore === 'string') { + return url === ignore + } + if (ignore instanceof RegExp) { + return ignore.test(url) + } + if (typeof ignore === 'function') { + return ignore(url) + } + return false + }) + } + if (links) { const dir = path.dirname(file) for (let url of links) { if (/\.(?!html|md)\w+($|\?)/i.test(url)) continue - if ( - siteConfig?.ignoreDeadLinks !== 'localhostLinks' && - url.replace(EXTERNAL_URL_RE, '').startsWith('//localhost:') - ) { - recordDeadLink(url) - continue - } - url = url.replace(/[?#].*$/, '').replace(/\.(html|md)$/, '') if (url.endsWith('/')) url += `index` let resolved = decodeURIComponent( @@ -151,7 +168,8 @@ export async function createMarkdownToVueRenderFn( siteConfig?.rewrites.inv[resolved + '.md']?.slice(0, -3) || resolved if ( !pages.includes(resolved) && - !fs.existsSync(path.resolve(dir, publicDir, `${resolved}.html`)) + !fs.existsSync(path.resolve(dir, publicDir, `${resolved}.html`)) && + !shouldIgnoreDeadLink(url) ) { recordDeadLink(url) } diff --git a/src/node/plugin.ts b/src/node/plugin.ts index 0d05c021..1881d2ed 100644 --- a/src/node/plugin.ts +++ b/src/node/plugin.ts @@ -70,7 +70,6 @@ export async function createVitePressPlugin( vue: userVuePluginOptions, vite: userViteConfig, pages, - ignoreDeadLinks, lastUpdated, cleanUrls } = siteConfig @@ -195,7 +194,7 @@ export async function createVitePressPlugin( }, renderStart() { - if (hasDeadLinks && !ignoreDeadLinks) { + if (hasDeadLinks) { throw new Error(`One or more pages contain dead links.`) } },