feat: catch localhost links as dead links

pull/486/head
Evan You 3 years ago
parent 7950e884a5
commit 7387649ff7

@ -38,7 +38,7 @@ $ yarn docs:build
$ yarn docs:serve
```
The `serve` command will boot up local static web server that serves the files from `.vitepress/dist` at http://localhost:5000. It's an easy way to check if the production build looks OK in your local environment.
The `serve` command will boot up local static web server that serves the files from `.vitepress/dist` at `http://localhost:5000`. It's an easy way to check if the production build looks OK in your local environment.
You may configure the port of the server py passing `--port` flag as an argument.
@ -50,7 +50,7 @@ You may configure the port of the server py passing `--port` flag as an argument
}
```
Now the `docs:serve` method will launch the server at http://localhost:8080.
Now the `docs:serve` method will launch the server at `http://localhost:8080`.
## GitHub Pages

@ -44,7 +44,7 @@ This section will help you build a basic VitePress documentation site from groun
$ yarn docs:dev
```
VitePress will start a hot-reloading development server at http://localhost:3000.
VitePress will start a hot-reloading development server at `http://localhost:3000`.
By now, you should have a basic but functional VitePress documentation site.

@ -24,6 +24,10 @@ export const linkPlugin = (
Object.entries(externalAttrs).forEach(([key, val]) => {
token.attrSet(key, val)
})
// catch localhost links as dead link
if (url.replace(EXTERNAL_URL_RE, '').startsWith('//localhost:')) {
pushLink(url)
}
} else if (
// internal anchor links
!url.startsWith('#') &&
@ -70,11 +74,15 @@ export const linkPlugin = (
}
// export it for existence check
const data = (md as any).__data as MarkdownParsedData
const links = data.links || (data.links = [])
links.push(url.replace(/\.html$/, ''))
pushLink(url.replace(/\.html$/, ''))
// markdown-it encodes the uri
hrefAttr[1] = decodeURI(url)
}
function pushLink(link: string) {
const data = (md as any).__data as MarkdownParsedData
const links = data.links || (data.links = [])
links.push(link)
}
}

@ -4,7 +4,7 @@ import matter from 'gray-matter'
import LRUCache from 'lru-cache'
import { createMarkdownRenderer, MarkdownOptions } from './markdown/markdown'
import { deeplyParseHeader } from './utils/parseHeader'
import { PageData, HeadConfig } from './shared'
import { PageData, HeadConfig, EXTERNAL_URL_RE } from './shared'
import { slash } from './utils/slash'
import chalk from 'chalk'
import _debug from 'debug'
@ -83,10 +83,26 @@ export function createMarkdownToVueRenderFn(
}
// validate data.links
const deadLinks = []
const deadLinks: string[] = []
const recordDeadLink = (url: string) => {
console.warn(
chalk.yellow(
`\n(!) Found dead link ${chalk.cyan(url)} in file ${chalk.white.dim(
file
)}`
)
)
deadLinks.push(url)
}
if (data.links) {
const dir = path.dirname(file)
for (let url of data.links) {
if (url.replace(EXTERNAL_URL_RE, '').startsWith('//localhost:')) {
recordDeadLink(url)
continue
}
url = url.replace(/[?#].*$/, '').replace(/\.(html|md)$/, '')
if (url.endsWith('/')) url += `index`
const resolved = decodeURIComponent(
@ -100,14 +116,7 @@ export function createMarkdownToVueRenderFn(
!pages.includes(resolved) &&
!fs.existsSync(path.resolve(dir, publicDir, `${resolved}.html`))
) {
console.warn(
chalk.yellow(
`\n(!) Found dead link ${chalk.cyan(
url
)} in file ${chalk.white.dim(file)}`
)
)
deadLinks.push(url)
recordDeadLink(url)
}
}
}

Loading…
Cancel
Save