Skip to content

Commit

Permalink
feat(rule): add dotInIgnore option (#153)
Browse files Browse the repository at this point in the history
* chore: Format markdown

* feat(rule): add `dotInIgnore` option

* docs: add `dotInIgnore` option
  • Loading branch information
kangetsu121 authored Aug 6, 2024
1 parent 8b4673d commit a6df911
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
33 changes: 21 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ The primary target of this rule is Markdown documents, but it also works on plai

## Installation

```
$ npm install textlint-rule-no-dead-link
```shell
npm install textlint-rule-no-dead-link
```

## Usage

```
$ npm install textlint textlint-rule-no-dead-link
$ textlint --rule textlint-rule-no-dead-link text-to-check.txt
```shell
npm install textlint textlint-rule-no-dead-link
textlint --rule textlint-rule-no-dead-link text-to-check.txt
```

## Features
Expand Down Expand Up @@ -60,8 +60,9 @@ The default options are:
"checkRelative": true,
"baseURI": null,
"ignore": [],
"preferGET": [],
"dotInIgnore": false,
"ignoreRedirects": false,
"preferGET": [],
"retry": 3,
"userAgent": "textlint-rule-no-dead-link/1.0",
"maxRetryTime": 10,
Expand Down Expand Up @@ -112,6 +113,14 @@ Example:
}
```

### dotInIgnore

This rule allows ignore patterns to match filenames starting with a period.
For example, if the `ignore` option contains `"http://example.com/**"` and the `dotInIgnore` option is set to `true`, paths containing filenames that start with `.` (like `"http://example.com/.hidden/index.html"`) will be ignored.
You can disable this behavior by setting `dotInIgnore` to `false`.

_cf_, <https://github.com/isaacs/minimatch?tab=readme-ov-file#dot>

### preferGET

An array of [origins](https://url.spec.whatwg.org/#origin) to lets the rule connect to the origin's URL by `GET` instead of default `HEAD` request.
Expand All @@ -133,7 +142,7 @@ Example:
This rule checks for redirects (3xx status codes) and consider's them an error by default.
To ignore redirects during checks, set this value to `false`.

<!-- Experimental
<!-- Experimental
### concurrency
Expand Down Expand Up @@ -170,17 +179,17 @@ Default: `10`
## CI Integration

Probably, Link Checking take long times.
We recommened to use cron job like GitHub Actions.
We recommend to use cron job like GitHub Actions.

### textlint + [SARIF output](https://www.npmjs.com/package/@microsoft/eslint-formatter-sarif) + [code scanning](https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/about-code-scanning)

Preparing:

```shell
# Install dependencies
$ npm install --save-dev textlint @microsoft/eslint-formatter-sarif textlint-rule-no-dead-link
npm install --save-dev textlint @microsoft/eslint-formatter-sarif textlint-rule-no-dead-link
# Create .textlintrc
$ npx textlint --init
npx textlint --init
```

Following actions check links and upload the status to [code scanning](https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/about-code-scanning).
Expand Down Expand Up @@ -219,7 +228,7 @@ jobs:
## Tests
```
```shell
npm test
```

Expand All @@ -233,4 +242,4 @@ npm test

## License

MIT License (http://nodaguti.mit-license.org/)
MIT License (<http://nodaguti.mit-license.org/>)
10 changes: 6 additions & 4 deletions src/no-dead-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type Options = {
checkRelative: boolean; // {boolean} `false` disables the checks for relative URIs.
baseURI: null | string; // {String|null} a base URI to resolve relative URIs.
ignore: string[]; // {Array<String>} URIs to be skipped from availability checks.
dotInIgnore: boolean; // {boolean} `true` allows ignore patterns to match filenames starting with a period
ignoreRedirects: boolean; // {boolean} `false` ignores redirect status codes.
preferGET: string[]; // {Array<String>} origins to prefer GET over HEAD.
retry: number; // {number} Max retry count
Expand All @@ -28,6 +29,7 @@ const DEFAULT_OPTIONS: Options = {
checkRelative: true, // {boolean} `false` disables the checks for relative URIs.
baseURI: null, // {String|null} a base URI to resolve relative URIs.
ignore: [], // {Array<String>} URIs to be skipped from availability checks.
dotInIgnore: false, // {boolean} `true` allows ignore patterns to match filenames starting with a period
ignoreRedirects: false, // {boolean} `false` ignores redirect status codes.
preferGET: [], // {Array<String>} origins to prefer GET over HEAD.
retry: 3, // {number} Max retry count
Expand Down Expand Up @@ -87,8 +89,8 @@ function isRedirect(code: number) {
return code === 301 || code === 302 || code === 303 || code === 307 || code === 308;
}

function isIgnored(uri: string, ignore: string[] = []) {
return ignore.some((pattern) => minimatch(uri, pattern));
function isIgnored(uri: string, ignore: string[] = [], dotInIgnore: boolean) {
return ignore.some((pattern) => minimatch(uri, pattern, { dot: dotInIgnore }));
}

/**
Expand Down Expand Up @@ -148,7 +150,7 @@ const createCheckAliveURL = (ruleOptions: Options) => {
/**
* Checks if a given URI is alive or not.
*
* Normally, this method following strategiry about retry
* Normally, this method following strategy about retry
*
* 1. Head
* 2. Get
Expand Down Expand Up @@ -277,7 +279,7 @@ const reporter: TextlintRuleReporter<Options> = (context, options) => {
* @param {number} maxRetryCount retry count of linting
*/
const lint = async ({ node, uri, index }: { node: TxtNode; uri: string; index: number }, maxRetryCount: number) => {
if (isIgnored(uri, ruleOptions.ignore)) {
if (isIgnored(uri, ruleOptions.ignore, ruleOptions.dotInIgnore)) {
return;
}

Expand Down
11 changes: 9 additions & 2 deletions test/no-dead-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ tester.run("no-dead-link", rule, {
ext: ".txt"
},
{
text: "should be able to check relative pathes when checkRelative is true: ![robot](index.html)",
text: "should be able to check relative paths when checkRelative is true: ![robot](index.html)",
options: {
baseURI: "https://example.com/"
}
Expand All @@ -44,6 +44,13 @@ tester.run("no-dead-link", rule, {
ignore: ["https://example.com/*"]
}
},
{
text: 'should ignore URLs containing . in their path in the "ignore" option that glob formatted if option is enabled: https://example.com/.hidden/404.html shouldn\'t be checked.',
options: {
ignore: ["https://example.com/**"],
dotInIgnore: true
}
},
{
text: "should ignore relative URIs when `checkRelative` is false: [test](./a.md).",
options: {
Expand Down Expand Up @@ -210,7 +217,7 @@ tester.run("no-dead-link", rule, {
},
{
text: `Support Reference link[^1] in Markdown.
[^1] https://httpstat.us/404`,
errors: [
{
Expand Down

0 comments on commit a6df911

Please sign in to comment.