Skip to content

Commit

Permalink
feat: support disable in checkLink
Browse files Browse the repository at this point in the history
  • Loading branch information
sanyuan0704 committed Oct 19, 2022
1 parent 538dfcc commit 97c9209
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 14 deletions.
1 change: 0 additions & 1 deletion docs/.island/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export default defineConfig({
markdown: {
rehypePlugins: [],
remarkPlugins: []
// checkLink: {}
},
route: {
exclude: ['custom.tsx', '**/fragments/**']
Expand Down
6 changes: 5 additions & 1 deletion docs/en/api/config-extension.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -209,6 +209,10 @@ export default defineConfig({
checkLink: {
exclude: ['github.com'],
timeout: 30000
},
checkLink: {
// will close the dead link check
disable: true
}
}
});
Expand Down
6 changes: 5 additions & 1 deletion docs/zh/api/config-extension.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export default defineConfig({
- Type: `Object`
- default: `null`

是否开启文档的链接检查,该配置仅在构建开发环境代码时生效
配置文档的链接检查功能

当文档中的链接无法正常访问时,会抛出错误并终止构建。

Expand All @@ -209,6 +209,10 @@ export default defineConfig({
checkLink: {
exclude: ['github.com'],
timeout: 30000
},
checkLink: {
// 将会关闭死链检查功能
disable: true
}
}
});
Expand Down
1 change: 0 additions & 1 deletion src/node/plugin-island/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
ISLAND_JSX_RUNTIME_PATH,
isProduction,
PUBLIC_DIR,
ROUTE_PATH,
SHARED_PATH
} from '../constants';
import { Plugin, UserConfig } from 'vite';
Expand Down
31 changes: 21 additions & 10 deletions src/node/plugin-mdx/remarkPlugins/deadLinks.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
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
*/
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[] = [];
const internalLinks: string[] = [];

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)
)
) {
Expand Down Expand Up @@ -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`);
});

Expand Down
1 change: 1 addition & 0 deletions src/shared/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,6 @@ export interface MarkdownOptions {
checkLink?: {
exclude?: (string | RegExp)[];
timeout?: number;
disable?: true;
};
}

1 comment on commit 97c9209

@vercel
Copy link

@vercel vercel bot commented on 97c9209 Oct 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.