diff --git a/docs/guide/deploy.md b/docs/guide/deploy.md index 9c884e5f..25c3058f 100644 --- a/docs/guide/deploy.md +++ b/docs/guide/deploy.md @@ -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 diff --git a/docs/guide/getting-started.md b/docs/guide/getting-started.md index 8436901e..2b891fd3 100644 --- a/docs/guide/getting-started.md +++ b/docs/guide/getting-started.md @@ -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. diff --git a/src/node/markdown/plugins/link.ts b/src/node/markdown/plugins/link.ts index 7d7c71f7..38063208 100644 --- a/src/node/markdown/plugins/link.ts +++ b/src/node/markdown/plugins/link.ts @@ -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) + } } diff --git a/src/node/markdownToVue.ts b/src/node/markdownToVue.ts index 083f60da..fc3c755d 100644 --- a/src/node/markdownToVue.ts +++ b/src/node/markdownToVue.ts @@ -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) } } }