@pchalin/markdownlint-rule-link-pattern
v0.3.0
Published
A markdownlint rule factory for validating link URLs against regex patterns
Maintainers
Readme
@pchalin/markdownlint-rule-link-pattern
A markdownlint rule factory for validating link URLs against regex patterns.
Each call to createLinkPatternRule() produces a markdownlint rule that can be
independently configured and disabled, and that flags link URLs matching a given
regex.
[!IMPORTANT]
This package is published to npm only under the
@pchalinscope. The unscoped name is not ours and was once used for malware (GHSA-q3xp-j858-q9xf); never install it.
Install
npm install @pchalin/markdownlint-rule-link-pattern --save-devUsage
1. Create rule instances
Create a file (e.g., link-rules.mjs) that defines your rules:
import { createLinkPatternRule } from '@pchalin/markdownlint-rule-link-pattern';
export default [
createLinkPatternRule('no-http-urls', 'Flag non-HTTPS URLs'),
createLinkPatternRule('no-example-com', 'Flag example.com links'),
];2. Register with markdownlint-cli2
In .markdownlint-cli2.yaml:
customRules:
- ./link-rules.mjs3. Configure each rule
In .markdownlint.yaml:
no-http-urls:
pattern: 'http://(?!localhost)'
message: Use https instead of http.
no-example-com:
pattern: 'example\.com'
message: Do not link to example.com.Config shape
Each rule instance reads its configuration from the markdownlint config under its rule name:
| Property | Type | Required | Description |
| ------------ | ------ | -------- | ------------------------------------------------------------------------------- |
| pattern | string | yes | Regex pattern to match against link URLs. |
| message | string | yes | Error message shown when a link matches. |
| skip_regex | string | no | URLs matching this regex are skipped. |
| replace | string | no | Replacement text; when set, enables --fix (supports $1, $2 for captures). |
Example with skip_regex
To skip URLs containing template directives (e.g., Hugo {{ }}):
no-example-com:
pattern: 'example\.com'
message: Do not link to example.com.
skip_regex: '\{\{.*\}\}'Example with replace (auto-fix)
To enable markdownlint --fix for a rule, add replace:
no-http-urls:
pattern: 'http://'
message: Use https instead of http.
replace: 'https://'
no-otel-external-urls:
pattern: 'https?://(?:www\.)?opentelemetry\.io/'
message: Use site-relative path.
replace: '/'Link types checked
The rule checks all link-like tokens produced by the micromark parser:
- Inline links:
[text](url) - Images:
 - Reference definitions:
[label]: url - Autolinks:
<https://...> - Bare URLs:
https://...(GFM extension)
