diff --git a/docs/.island/config.ts b/docs/.island/config.ts index cd67f350..050081b8 100644 --- a/docs/.island/config.ts +++ b/docs/.island/config.ts @@ -18,7 +18,6 @@ export default defineConfig({ markdown: { rehypePlugins: [], remarkPlugins: [] - // checkLink: {} }, route: { exclude: ['custom.tsx', '**/fragments/**'] diff --git a/docs/en/api/config-extension.md b/docs/en/api/config-extension.md index 2b4513ab..cf79fc3d 100644 --- a/docs/en/api/config-extension.md +++ b/docs/en/api/config-extension.md @@ -197,7 +197,7 @@ export default defineConfig({ - Type: `Object` - default: `null` -Whether to enable the link check of the document, this configuration only takes effect when building the development environment code. +Configure the dead link check behavior of the document. When a link in the documentation is not accessible properly, an error is thrown and the build is terminated. @@ -209,6 +209,10 @@ export default defineConfig({ checkLink: { exclude: ['github.com'], timeout: 30000 + }, + checkLink: { + // will close the dead link check + disable: true } } }); diff --git a/docs/zh/api/config-extension.md b/docs/zh/api/config-extension.md index b68e565a..6e3a86dd 100644 --- a/docs/zh/api/config-extension.md +++ b/docs/zh/api/config-extension.md @@ -197,7 +197,7 @@ export default defineConfig({ - Type: `Object` - default: `null` -是否开启文档的链接检查,该配置仅在构建开发环境代码时生效。 +配置文档的链接检查功能。 当文档中的链接无法正常访问时,会抛出错误并终止构建。 @@ -209,6 +209,10 @@ export default defineConfig({ checkLink: { exclude: ['github.com'], timeout: 30000 + }, + checkLink: { + // 将会关闭死链检查功能 + disable: true } } }); diff --git a/src/node/plugin-island/config.ts b/src/node/plugin-island/config.ts index d31be945..3790b231 100644 --- a/src/node/plugin-island/config.ts +++ b/src/node/plugin-island/config.ts @@ -7,7 +7,6 @@ import { ISLAND_JSX_RUNTIME_PATH, isProduction, PUBLIC_DIR, - ROUTE_PATH, SHARED_PATH } from '../constants'; import { Plugin, UserConfig } from 'vite'; diff --git a/src/node/plugin-mdx/remarkPlugins/deadLinks.ts b/src/node/plugin-mdx/remarkPlugins/deadLinks.ts index ddd177db..7d4b0ae0 100644 --- a/src/node/plugin-mdx/remarkPlugins/deadLinks.ts +++ b/src/node/plugin-mdx/remarkPlugins/deadLinks.ts @@ -1,11 +1,10 @@ -import { MarkdownOptions } from './../../../shared/types/index'; import { normalizeRoutePath } from '../../plugin-routes/RouteService'; -import { isProduction } from '../../../node/constants'; import type { Plugin } from 'unified'; import { visit } from 'unist-util-visit'; import { routeService } from '../../plugin-routes'; import checkLinks from 'check-links'; import ora from 'ora'; +import { MarkdownOptions } from 'shared/types/index'; /** * Remark plugin to normalize a link href @@ -13,7 +12,11 @@ import ora from 'ora'; export const remarkCheckDeadLinks: Plugin< [{ checkLink: MarkdownOptions['checkLink'] }] > = ({ checkLink }) => { - if (!checkLink || !isProduction()) return; + if (checkLink?.disable) { + return; + } + + const { exclude = [], timeout = 10000 } = checkLink || {}; return async (tree) => { const externalLinks: string[] = []; @@ -21,12 +24,16 @@ export const remarkCheckDeadLinks: Plugin< visit(tree, 'link', (node: { url: string }) => { const url = node.url; - if (!url) return; - if (internalLinks.includes(url) || externalLinks.includes(url)) return; + if (!url) { + return; + } + if (internalLinks.includes(url) || externalLinks.includes(url)) { + return; + } if ( - checkLink.exclude && - checkLink.exclude.some((skipPattern: string | RegExp) => + exclude && + exclude.some((skipPattern: string | RegExp) => new RegExp(skipPattern).test(url) ) ) { @@ -54,12 +61,16 @@ export const remarkCheckDeadLinks: Plugin< // If the timeout is set too short, some links will be judged as dead links const results = await checkLinks(externalLinks, { - timeout: checkLink?.timeout || 30000 + timeout }); Object.keys(results).forEach((url) => { const result = results[url]; - if (result.status !== 'dead') return; - if (!externalLinks.includes(url)) return; + if (result.status !== 'dead') { + return; + } + if (!externalLinks.includes(url)) { + return; + } errorInfos.push(`External link to ${url} is dead`); }); diff --git a/src/shared/types/index.ts b/src/shared/types/index.ts index 43673bb0..29ea7cd7 100644 --- a/src/shared/types/index.ts +++ b/src/shared/types/index.ts @@ -201,5 +201,6 @@ export interface MarkdownOptions { checkLink?: { exclude?: (string | RegExp)[]; timeout?: number; + disable?: true; }; }