@boillodmanuel/markdown-link-check
v4.7.0
Published
checks the all of the hyperlinks in a markdown text to determine if they are alive or dead
Maintainers
Readme
ℹ️ Fork of tcort repository (version 3.8.3), with several improvements:
- completely rewritten in typescript
- see CHANGELOG.md since v3.8.3 to get full list of improvements (multiple inputs support, cache, concurrency, statistics, debug traces,...)
markdown-link-check
Extracts links from markdown texts and checks whether each link is
alive (200 OK) or dead.
mailto: links are validated with isemail.
Installation
To add the module to your project, run:
npm install --save-dev @boillodmanuel/markdown-link-checkTo install the command line tool globally, run:
npm install -g @boillodmanuel/markdown-link-checkUsage
Using from Command line tool (CLI)
The command line tool checks markdown links from 1 or several files or URLs or from standard input.
Note: You can either use this library from a javascript/typescript project (as a node dependency) or from command line. To run the command line, type:
markdown-link-check README.mdif you installed it globallynpx markdown-link-check README.mdif you installed it as a project dependency.
Documentation
Usage: markdown-link-check [options] [filenameOrUrls...]
Argument:
[filenameOrUrls...]: one or several files or URLs. If absent, process stdin
Options:
Options specific to command line:
-c, --config [config]: apply a config file (JSON), holding e.g. url specific header configuration-q, --quiet: displays errors only-v, --verbose: displays detailed error information--print-summary: print total number of inputs and links process with details about link status (alive, ignored, dead, error)--print-cache-statsprints cache usage (hits and misses).--print-long-checksprints links that took more than given delay to verify. Default delay is5000, configure it with--long-checks-max-durationoption.--long-checks-max-durationconfigure delay for long check. Default is5000.-h, --helpdisplay help for command
Options that override configuration file:
-a, --alive <codes>comma separated list of HTTP code to be considered as alive--retry-on-errorretry after the an error--retry-on-429retry after the duration indicated in 'retry-after' header when HTTP code is 429--timeout <duration>timeout in zeit/ms format. (e.g.2000ms,20s,1m). Default is10s.-e, --fileEncoding <encoding>set file enconding. Default isutf-8.-d, --debugdisplays debug traces (very verbose)
Examples
Simple examples:
# Check links from a markdown file hosted on the web
markdown-link-check https://github.com/tcort/markdown-link-check/blob/master/README.md
# Check links from a local markdown file
markdown-link-check ./README.md
# Check links from multiples markdown file
markdown-link-check ./README.md ./CHANGELOG.md
# Check links from stdin
curl -s https://github.com/tcort/markdown-link-check/blob/master/README.md | markdown-link-checkAdvanced examples:
Check links for each files
# Check links from a local markdown folder (recursive)
find . -name \*.md -exec markdown-link-check {} \;Check links for all files (run a single process which can leverage cache)
# Add files in a bash array (using -print0 to support file names with space or special characters)
FILES=()
while IFS= read -r -d $'\0' FILE; do
FILES+=("$FILE")
done < <(find . -name '*.md' ! -path './node_modules/*' -print0)
markdown-link-check --print-summary "${FILES[@]}"Using from a node project
This part is not documented. The best way to kwow how to use this library from code is to look at run function in /src/bin/markdown-link-check.ts` which is the entry point of the command line utility.
Configuration
You can configure the processing with using a configuration file described below.
Configuration file format
File config.json attributes per category:
Input content:
fileEncoding: set file enconding. Default isutf-8.
Link management:
ignorePatterns: An array of objects holding regular expressions which a link is checked against and skipped for checking in case of a match.replacementPatterns: An array of objects holding regular expressions which are replaced in a link with their corresponding replacement string. This behavior allows (for example) to adapt to certain platform conventions hosting the Markdown.httpHeaders: The headers are only applied to links where the link starts with one of the supplied URLs in theurlssection.resolveAbsolutePathWithBaseUrl: iftruelinks with absolute path (e.g./section/page1.md) are resolved frombaseUrl(see below) where as links with with relative path (e.g../page2.md) are resolved from current url/path. This option reflects some wiki behavior. Default isfalse.baseUrl: base url/path used to resolved links with absolute path ifresolveAbsolutePathWithBaseUrlis true. The value should starts withfile://,http://orhttps://.
Error management:
timeouttimeout in zeit/ms format. (e.g."2000ms",20s,1m). Default10s.retryOnErrorif this istruethen retry after an error.retryOn429if this istruethen retry request when response is an HTTP code 429 after the duration indicated by theretry-afterresponse header or by thefallbackRetryDelayoption.retryCountthe number of retries to be made on a 429 response. Default2.fallbackRetryDelaythe delay in zeit/ms format. (e.g."2000ms",20s,1m) for retries on a 429 response when noretry-afterheader is returned or when it has an invalid value. Default is5s.aliveStatusCodesa list of HTTP codes to consider as alive.
Concurrency:
concurrentFileCheck: Number of file or url processed concurrently. Default is 2.concurrentCheck: Number of links processed concurrently for a single file or url. Default is 2.
Debug:
debugdisplays debug tracesdebugToStdErrredirect debug trace to sterr instead of stdout
Example:
{
"ignorePatterns": [
{
"pattern": "^http://example.net"
}
],
"replacementPatterns": [
{
"pattern": "^.attachments",
"replacement": "file://some/conventional/folder/.attachments"
}
],
"httpHeaders": [
{
"urls": ["https://example.com"],
"headers": {
"Authorization": "Basic Zm9vOmJhcg==",
"Foo": "Bar"
}
}
],
"timeout": "20s",
"retryOn429": true,
"retryCount": 5,
"fallbackRetryDelay": "30s",
"aliveStatusCodes": [200, 206],
"concurrentFileCheck": 5,
"concurrentCheck": 2,
"fileEncoding": "utf-8",
"resolveAbsolutePathWithBaseUrl": true,
"baseUrl": "/var/wiki",
"debug": false,
"debugToStdErr": false,
}Ignore some links from markdown
You can write html comments to disable markdown-link-check for parts of the text.
<!-- markdown-link-check-disable -->disables markdown link check.<!-- markdown-link-check-enable -->reenables markdown link check.<!-- markdown-link-check-disable-next-line -->disables markdown link check for the next line.<!-- markdown-link-check-disable-line -->disables markdown link check for this line.
Documentation from original repository
Note: The documentation in this section comes from original repository and is not maintained in this fork but kept for information. this library
Run using Docker
Docker images are built with each release. Use the stable tag for the current stable release.
Add current directory with your README.md file as read only volume to docker run:
docker run -v ${PWD}:/tmp:ro --rm -i ghcr.io/tcort/markdown-link-check:stable /tmp/README.mdAlternatively, if you wish to target a specific release, images are tagged with semantic versions (i.e. 3, 3.8, 3.8.3)
Run in a GitHub action
Note: Github action is not maintained in this fork.
Please head on to github-action-markdown-link-check.
Run in other tools
- Mega-Linter: Linters aggregator including markdown-link-check
API
markdownLinkCheck(markdown, [opts,] callback)
Given a string containing markdown formatted text and a callback,
extract all of the links and check if they're alive or dead. Call the
callback with (err, results)
Parameters:
markdownstring containing markdown formatted text.optsoptional options object containing any of the following optional fields:baseUrlthe base URL for relative links.showProgressBarenable an ASCII progress bar.timeouttimeout in zeit/ms format. (e.g."2000ms",20s,1m). Default10s.httpHeadersto apply URL specific headers, see example below.ignorePatternsan array of objects holding regular expressions which a link is checked against and skipped for checking in case of a match. Example:[{ pattern: /foo/ }]replacementPatternsan array of objects holding regular expressions which are replaced in a link with their corresponding replacement string. This behavior allows (for example) to adapt to certain platform conventions hosting the Markdown. Example:[{ pattern: /^.attachments/, replacement: "file://some/conventional/folder/.attachments" }]ignoreDisableif this istruethen disable comments are ignored.retryOn429if this istruethen retry request when response is an HTTP code 429 after the duration indicated byretry-afterheader.retryCountthe number of retries to be made on a 429 response. Default2.fallbackRetryDelaythe delay in zeit/ms format. (e.g."2000ms",20s,1m) for retries on a 429 response when noretry-afterheader is returned or when it has an invalid value. Default is60s.aliveStatusCodesa list of HTTP codes to consider as alive. Example:[200,206]
callbackfunction which accepts(err, results).erran Error object when the operation cannot be completed, otherwisenull.resultsan array of objects with the following properties:linkthelinkprovided as inputstatusa string set to eitheralive,ignoredordead.statusCodethe HTTP status code. Set to0if no HTTP status code was returned (e.g. when the server is down).errany connection error that occurred, otherwisenull.
Examples
Module
Basic usage:
'use strict';
var markdownLinkCheck = require('markdown-link-check');
markdownLinkCheck('[example](http://example.com)', function (err, results) {
if (err) {
console.error('Error', err);
return;
}
results.forEach(function (result) {
console.log('%s is %s', result.link, result.status);
});
});With options, for example using URL specific headers:
'use strict';
var markdownLinkCheck = require('markdown-link-check');
markdownLinkCheck('[example](http://example.com)', { httpHeaders: [{ urls: ['http://example.com'], headers: { 'Authorization': 'Basic Zm9vOmJhcg==' }}] }, function (err, results) {
if (err) {
console.error('Error', err);
return;
}
results.forEach(function (result) {
console.log('%s is %s', result.link, result.status);
});
});Test command line
npx markdown-link-check test/file1.md
npx markdown-link-check --inputs test/file1.md test/file2.mdLicense
See LICENSE.md
