Skip to content

Commit

Permalink
fix: off by default and increase the cache to avoid secondary detecti…
Browse files Browse the repository at this point in the history
…on (#108)

* fix: off by default and increase the cache to avoid secondary detection

* fix: remove checkLink config

* fix: useless code
  • Loading branch information
callqh authored Oct 20, 2022
1 parent 97c9209 commit d7b8265
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
8 changes: 6 additions & 2 deletions docs/en/api/config-extension.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ 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.

:::danger
This configuration is off by default. When manually turned on, the build will be blocked under poor network conditions.
:::

```js
import { defineConfig } from 'islandjs';

Expand All @@ -211,8 +215,8 @@ export default defineConfig({
timeout: 30000
},
checkLink: {
// will close the dead link check
disable: true
// will enable the dead link check
enable: true
}
}
});
Expand Down
8 changes: 6 additions & 2 deletions docs/zh/api/config-extension.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ export default defineConfig({

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

:::danger
该配置默认关闭。当手动开启后,在网络状况不好的情况下会阻塞构建
:::

```js
import { defineConfig } from 'islandjs';

Expand All @@ -211,8 +215,8 @@ export default defineConfig({
timeout: 30000
},
checkLink: {
// 将会关闭死链检查功能
disable: true
// 将会开启死链检查功能
enable: true
}
}
});
Expand Down
18 changes: 13 additions & 5 deletions src/node/plugin-mdx/remarkPlugins/deadLinks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isProduction } from '../../constants';
import { normalizeRoutePath } from '../../plugin-routes/RouteService';
import type { Plugin } from 'unified';
import { visit } from 'unist-util-visit';
Expand All @@ -6,13 +7,14 @@ import checkLinks from 'check-links';
import ora from 'ora';
import { MarkdownOptions } from 'shared/types/index';

const checkedLinks = new Map();
/**
* Remark plugin to normalize a link href
*/
export const remarkCheckDeadLinks: Plugin<
[{ checkLink: MarkdownOptions['checkLink'] }]
> = ({ checkLink }) => {
if (checkLink?.disable) {
if (!checkLink?.enable) {
return;
}

Expand All @@ -27,7 +29,11 @@ export const remarkCheckDeadLinks: Plugin<
if (!url) {
return;
}
if (internalLinks.includes(url) || externalLinks.includes(url)) {
if (
internalLinks.includes(url) ||
externalLinks.includes(url) ||
checkedLinks.has(url)
) {
return;
}

Expand All @@ -41,7 +47,9 @@ export const remarkCheckDeadLinks: Plugin<
}

if (!url.startsWith('http') && !url.startsWith('https')) {
internalLinks.push(normalizeRoutePath(url?.split('#')[0]));
const normalizeUrl = normalizeRoutePath(url?.split('#')[0]);
internalLinks.push(normalizeUrl);
checkedLinks.set(normalizeUrl, true);
return;
}

Expand All @@ -50,6 +58,7 @@ export const remarkCheckDeadLinks: Plugin<
return;
}
externalLinks.push(url);
checkedLinks.set(url, true);
});

const errorInfos: string[] = [];
Expand All @@ -58,7 +67,6 @@ export const remarkCheckDeadLinks: Plugin<
errorInfos.push(`Internal link to ${link} is dead`);
}
});

// If the timeout is set too short, some links will be judged as dead links
const results = await checkLinks(externalLinks, {
timeout
Expand All @@ -77,7 +85,7 @@ export const remarkCheckDeadLinks: Plugin<
// output error info
if (errorInfos.length > 0) {
errorInfos?.forEach((err) => ora().fail(err));
process.exit();
isProduction() && process.exit();
}
};
};
1 change: 1 addition & 0 deletions src/node/type.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "check-links"
2 changes: 1 addition & 1 deletion src/shared/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,6 @@ export interface MarkdownOptions {
checkLink?: {
exclude?: (string | RegExp)[];
timeout?: number;
disable?: true;
enable?: boolean;
};
}

1 comment on commit d7b8265

@vercel
Copy link

@vercel vercel bot commented on d7b8265 Oct 20, 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.