A textlint rule to check if all links are alive
npm install textlint-rule-no-dead-link


textlint rule
to make sure every link in a document is available.
The primary target of this rule is Markdown documents, but it also works on plain text documents (See tests).
``shell`
npm install textlint-rule-no-dead-link
`shell`
npm install textlint textlint-rule-no-dead-link
textlint --rule textlint-rule-no-dead-link text-to-check.txt
Shows an error if a link is dead (i.e. its server returns one of the "non-ok" responses).

Shows an error if a link is obsolete or moved to another location (i.e. its server returns one of the "redirect" responses).
This error is fixable and textlint will automatically replace the obsolete links with their new ones if you run it with --fix option.
Sometimes your files contain relative URIs, which don't have domain information in an URI string.
In this case, we have to somehow resolve the relative URIs and convert them into absolute URIs.
The resolution strategy is as follows:
1. If baseURI is specified, use that path to resolve relative URIs (See the below section for details).Unable to resolve the relative URI
2. If not, try to get the path of the file being linted and use its parent folder as the base path.
3. If that's not available (e.g., when you are performing linting from API), put an error .
Please write your configurations in .textlintrc.
The default options are:
`json`
{
"rules": {
"no-dead-link": {
"checkRelative": true,
"baseURI": null,
"ignore": [],
"dotInIgnore": false,
"ignoreRedirects": false,
"preferGET": [],
"retry": 3,
"userAgent": "textlint-rule-no-dead-link/1.0",
"maxRetryTime": 10,
"maxRetryAfterTime": 90,
"linkMaxAge": 30000,
"httpOnly": false
}
}
}
This rule checks the availability of relative URIs by default.
You can turn off the checks by passing false to this option.
The base URI to be used for resolving relative URIs.
Though its name, you can pass either an URI starting with http or https, or an file path starting with /.
Examples:
`json`
"no-dead-link": {
"baseURI": "http://example.com/"
}
`json`
"no-dead-link": {
"baseURI": "/Users/textlint/path/to/parent/folder/"
}
An array of URIs or globs to be ignored.
These list will be skipped from the availability checks.
Example:
`json`
"no-dead-link": {
"ignore": [
"http://example.com/not-exist/index.html",
"http://example.com/*" // glob format
]
}
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.dotInIgnore
You can disable this behavior by setting to false.
_cf_,
An array of origins to lets the rule connect to the origin's URL by GET instead of default HEAD request.
Although the rule will fall back to GET method when HEAD request is failed (status code is not between 200 and 300), in order to shorten time to run your test, you can use this option when you are sure that target origin always returns 5xx for HEAD request.
Example:
`json`
"no-dead-link": {
"preferGET": [
"http://example.com"
]
}
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.
This rule checks the url with retry.
The default max retry count is 3.
Customize User-Agent http header.
The max of waiting seconds for retry. It is related to retry option.
:memo: It does affect to Retry-After header. If you want to max waiting seconds for Retry-After header, please use maxRetryAfterTime option.
Default: 10
The max of allow waiting time second for Retry-After header value.
Some website like GitHub returns Retry-After header value with 429 too many requests.maxRetryAfterTime
This option is for that Retry-After.
Default: 10
The time (in milliseconds) that the cache for the result indicating whether a link is available.
Default: 30000
Controls whether all links are treated as HTTP/HTTPS links. If true, links like /path/to are checked for existence via HTTP/HTTPS communication rather than the filesystem. The Base URI is the base of the redirect source URL for redirects, and baseURI otherwise.
Default: false
Probably, Link Checking take long times.
We recommend to use cron job like GitHub Actions.
Preparing:
`shell`Install dependencies
npm install --save-dev textlint @microsoft/eslint-formatter-sarif textlint-rule-no-dead-linkCreate .textlintrc
npx textlint --init
Following actions check links and upload the status to code scanning.
You can see the result at https://github.com/{owner}/{repo}/security/code-scanning.
`yaml
name: Link Check
on:
workflow_dispatch:
schedule:
- cron: '45 15 *'
permissions:
contents: read
security-events: write
jobs:
test:
runs-on: ubuntu-latest
name: Link Check
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18
- run: npm ci
- run: npx textlint -f @microsoft/eslint-formatter-sarif -o textlint.sarif || exit 0 # workaround https://github.com/textlint/textlint/issues/103
- name: Upload SARIF file
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: textlint.sarif
category: textlint
`
`shell`
npm test
1. Fork it!
2. Create your feature branch: git checkout -b my-new-featuregit commit -am 'Add some feature'
3. Commit your changes: git push origin my-new-feature`
4. Push to the branch:
5. Submit a pull request :D
MIT License (