Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

markdown-link-check

tcort292kISC3.13.7

checks the all of the hyperlinks in a markdown text to determine if they are alive or dead

markdown, md, link, hyperlink, href, check, checker

readme

Test library workflow status

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 markdown-link-check

To install the command line tool globally, run:

npm install -g markdown-link-check

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.md

Alternatively, 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

Please head on to github-action-markdown-link-check.

Run as a pre-commit hook

To run as a pre-commit hook:

- repo: https://github.com/tcort/markdown-link-check
  rev: ...
  hooks:
    - id: markdown-link-check
      args: [-q]

Run in a GitLab pipeline

linkchecker:
  stage: test
  image:
    name: ghcr.io/tcort/markdown-link-check:3.11.2
    entrypoint: ["/bin/sh", "-c"]
  script:
    - markdown-link-check ./docs
  rules:
    - changes:
      - "**/*.md"

Run in other tools

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:

  • markdown string containing markdown formatted text.
  • opts optional options object containing any of the following optional fields:
    • showProgressBar enable an ASCII progress bar.
    • timeout timeout in zeit/ms format. (e.g. "2000ms", 20s, 1m). Default 10s.
    • httpHeaders to apply URL specific headers, see example below.
    • ignorePatterns an array of objects holding regular expressions which a link is checked against and skipped for checking in case of a match. Example: [{ pattern: /foo/ }]
    • 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. The special replacement {{BASEURL}} can be used to dynamically link to the base folder (used from projectBaseUrl) (for example that / points to the root of your local repository). Example: [{ pattern: /^.attachments/, replacement: "file://some/conventional/folder/.attachments" }, { pattern: ^/, replacement: "{{BASEURL}}/"}]. You can add "global": true to use a global regular expression to replace all instances.
    • projectBaseUrl the URL to use for {{BASEURL}} replacement
    • ignoreDisable if this is true then disable comments are ignored.
    • retryOn429 if this is true then retry request when response is an HTTP code 429 after the duration indicated by retry-after header.
    • retryCount the number of retries to be made on a 429 response. Default 2.
    • fallbackRetryDelay the delay in zeit/ms format. (e.g. "2000ms", 20s, 1m) for retries on a 429 response when no retry-after header is returned or when it has an invalid value. Default is 60s.
    • aliveStatusCodes a list of HTTP codes to consider as alive. Example: [200,206]
  • callback function which accepts (err, results).
    • err an Error object when the operation cannot be completed, otherwise null.
    • results an array of objects with the following properties:
      • link the link provided as input
      • status a string set to either alive, ignored or dead.
      • statusCode the HTTP status code. Set to 0 if no HTTP status code was returned (e.g. when the server is down).
      • err any connection error that occurred, otherwise null.

Disable comments

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.

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);
    });
});

Command Line Tool

The command line tool optionally takes 1 argument, the file name or http/https URL. If not supplied, the tool reads from standard input.

markdown-link-check https://github.com/tcort/markdown-link-check/blob/master/README.md
markdown-link-check ./README.md

This checks all files in folder ./docs with file extension *.md:

markdown-link-check ./docs

The files can also be searched for and filtered manually:

find . -name \*.md -print0 | xargs -0 -n1 markdown-link-check

Usage

Usage: markdown-link-check [options] [filenameOrDirectorynameOrUrl]

Options:
  -p, --progress              show progress bar
  -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
  -a, --alive <code>          comma separated list of HTTP code to be considered as alive
  -r, --retry                 retry after the duration indicated in 'retry-after' header when HTTP code is 429
  -h, --help                  display help for command
  -V, --version               display version string (e.g. `1.2.3`)
    , --projectBaseUrl <url>  the URL to use for {{BASEURL}} replacement
Config file format

config.json:

  • 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. The special replacement {{BASEURL}} can be used to dynamically link to the current working directory (for example that / points to the root of your current working directory). This parameter supports named regex groups the same way as string.replace method in node.
  • httpHeaders: The headers are only applied to links where the link starts with one of the supplied URLs in the urls section.
  • timeout timeout in zeit/ms format. (e.g. "2000ms", 20s, 1m). Default 10s.
  • retryOn429 if this is true then retry request when response is an HTTP code 429 after the duration indicated by retry-after header.
  • retryCount the number of retries to be made on a 429 response. Default 2.
  • fallbackRetryDelay the delay in zeit/ms format. (e.g. "2000ms", 20s, 1m) for retries on a 429 response when no retry-after header is returned or when it has an invalid value. Default is 60s.
  • aliveStatusCodes a list of HTTP codes to consider as alive.
  • projectBaseUrl the URL to use for {{BASEURL}} replacement

Example:

{
  "projectBaseUrl":"${workspaceFolder}",
  "ignorePatterns": [
    {
      "pattern": "^http://example.net"
    }
  ],
  "replacementPatterns": [
    {
      "pattern": "^.attachments",
      "replacement": "file://some/conventional/folder/.attachments"
    },
    {
      "pattern": "^/",
      "replacement": "{{BASEURL}}/"
    },
    {
      "pattern": "%20",
      "replacement": "-",
      "global": true
    },
    {
      "pattern": "images/(?<filename>.*)",
      "replacement": "assets/$<filename>"
    }
  ],
  "httpHeaders": [
    {
      "urls": ["https://example.com"],
      "headers": {
        "Authorization": "Basic Zm9vOmJhcg==",
        "Foo": "Bar"
      }
    }
  ],
  "timeout": "20s",
  "retryOn429": true,
  "retryCount": 5,
  "fallbackRetryDelay": "30s",
  "aliveStatusCodes": [200, 206]
}

Testing

npm test

License

See LICENSE.md

changelog

Changes

Version 3.13.6

  • fix: move script back to its original location

Version 3.13.5

  • fix: MODULE_NOT_FOUND error #368

Version 3.13.4

  • fix: MODULE_NOT_FOUND error #368

Version 3.13.3

  • fix: MODULE_NOT_FOUND error #368

Version 3.13.2

  • fix: MODULE_NOT_FOUND error #368

Version 3.13.1

  • fix: MODULE_NOT_FOUND error #368

Version 3.13.0

  • feat: anchor link checks support HTML tags like <a name="foo"></a> #331 @dklimpel
  • ✨ add support for additional test reporters #364 @dudeofawesome
  • feat: support iterate link check over directories in CLI #334 @dklimpel
  • Update hook names #366 @henrygerardmoore
  • chore(deps): remove lodash #332 @dklimpel
  • fix: add used @eslint/js to dev dependencies in package.json #330 @dklimpel
  • fix: remove not used const url #329 @dklimpel
  • feat: add support for unicode characters in anchor links #328 @dklimpel
  • docs(readme): explain named regex groups in replacementPatterns #327 @AndreyNautilus
  • ci: bump nodejs #367 @dklimpel
  • update dependencies

Version 3.12.2

Version 3.12.1

  • fix: fix crash #297 @CanadaHonk
  • Set ipv4first for tests in CI @dklimpel
  • Bump node for release @dklimpel

Version 3.12.0

  • feat: add basic ignore subpaths argument @CanadaHonk
  • feat: add global option to replacementPatterns @CanadaHonk
  • refactor: replace url.parse with new URL @CanadaHonk
  • docs: update readme to be clearer for projectBaseUrl opt @nwcm
  • feat: Add support to load md files via proxy @dklimpel
  • chore: upgrade dependencies

Version 3.11.2

  • chore(deps-dev): bump eslint from 8.38.0 to 8.39.0 (@dependabot)
  • fixed the error when there is no --config option (@mkusaka)
  • chore(deps): bump commander from 10.0.0 to 10.0.1 (@dependabot)
  • Updated elgohr/Publish-Docker-Github-Action to a supported version (v5) (@elgohr)
  • chore: upgrade dependencies (@tcort)

Version 3.11.1

  • fix:Config-file-and-others-options-ignored (@kevinvalleau)
  • chore(deps-dev): bump eslint from 8.35.0 to 8.38.0 (@dependabot)

Version 3.11.0

  • tests: add test for link to a header with special character #227 (@kayman-mk)
  • fix: make --quiet flag only output errors #230 (@tcrasset)
  • feat: Add CLI arg for projectBaseUrl #232 (@pvallone)
  • chore: Add Dependabot for version updates #234 (@nschonni)
  • ci: Add current Node versions to matrix build #235 (@nschonni)
  • ci: Remove disabled Docker workflow #242 (@nschonni)
  • chore: Convert JSHint to ESLint #243 (@nschonni)
  • ci: Use array for release type schema #244 (@nschonni)
  • docs: Add how to run as pre-commit to README #245 (@vorburger)
  • chore: upgrade dependencies (@tcort)
  • fix: broken link in README.md (@tcort)

Version 3.10.3

  • upgrade dependencies

Version 3.10.2

  • fix missing dependency

Version 3.10.1

  • upgrade dependencies
  • fix anchor regressions (@reyang)
  • fix ci build (@chenrui333 @ nschonni)

Version 3.10.0

  • 91 check anchor links.

Version 3.9.3

  • 184 fix issue with config loading. (@kenji-miyake)

Version 3.9.2

  • 182 fix issue with markdown-link-check utility. set baseUrl for each file. (@kenji-miyake)

Version 3.9.1

  • 78/#179 support multi inputs (@kenji-miyake)

  • 181 Update marked and markdown-link-extractor dependencies. (@tcort)

Version 3.9.0

  • 149 add -V, --version command line flags (@tcort)

  • 164 indent console output (@brandones)

  • 170 pre-commit support (@janosh)

  • 173 update docs (@karl-johan-grahn)

  • 174 Symlink executable into PATH (@carlsmedstad)

  • 177 upgrade command line tool from request to needle (@tcort)

Version 3.8.8

  • Update docs with respect to recursive scan

Version 3.8.7

  • update dependencies #161

Version 3.8.6

  • support env vars in http headers (#146)
  • add {{BASEURL}} as replacement pattern (#145)

Version 3.8.5

  • 142 update link-check dependency

Version 3.8.4

  • 106 / #114 / #126 better 429 handling

  • 129 / #135 add timeout option

  • 122 fix typo

  • 139 add other tools and linters

  • 121 / #132 github actions

  • 110 / #127 / #113 / #133 / #140 docker images

  • update dependencies

Version 3.8.3

  • update dependencies (Fixes #86)

Version 3.8.2

  • update dependencies

Version 3.8.1

  • update dependencies
  • Remove unnecessary files in the package published to npmjs #92

Version 3.8.0

  • update dependencies #81
  • Surface dead links in results output #82
  • Add support for disable comments #83
  • Suggest --save-dev rather than --save in README.md #63

Version 3.7.3

  • update dependencies #74

Version 3.7.2

  • Fix fs access anti pattern #62

Version 3.7.1

  • Fix accessing fs.constants.F_OK #58

Version 3.7.0

  • Add verbose mode for showing detailed error information #55
  • Fix issue with fs constants #53
  • Fix invalid argument errors #54

Version 3.6.2

  • fix crash when 1st link is ignored/replaced

Version 3.6.1

  • ignore query string in links to local files

Version 3.6.0

  • replacement patterns

Version 3.5.5

  • better handling of malformed URLs and unsupported protocols.
  • support RFC6068 style hfields in mailto: links.

Version 3.5.4

  • update markdown-link-extractor dependency to support image links with image size attributes

Version 3.5.3

  • docker run fixes

Version 3.5.2

  • support for parentheses in URLs

Version 3.5.1

  • don't show 'No hyperlinks found!' when quiet

Version 3.5.0

  • introduce --quiet mode that displays errors (dead links) only
  • support for ignore patterns to skip the link check for certain links

Version 3.4.0

  • support for providing custom HTTP headers

Version 3.3.1

  • update dependencies to avoid CVE-2018-3728