feat: more flexible `ignoreDeadLinks` (#2135)

pull/2141/head
Anthony Fu 3 years ago committed by GitHub
parent eac03f26e2
commit 3235c23313
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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 <Badge type="warning" text="experimental" />
- Type: `boolean`

@ -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": {

@ -85,7 +85,10 @@ export interface UserConfig<ThemeConfig = any>
*
* @default false
*/
ignoreDeadLinks?: boolean | 'localhostLinks'
ignoreDeadLinks?:
| boolean
| 'localhostLinks'
| (string | RegExp | ((link: string) => boolean))[]
/**
* Don't force `.html` on URLs.

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

@ -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.`)
}
},

Loading…
Cancel
Save