mirror of
https://github.com/theoludwig/markdownlint-rule-relative-links.git
synced 2025-05-27 11:37:24 +02:00
Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
9c87395d82
|
|||
f99e2bbe4d
|
|||
e642c7ceee
|
|||
9cf3168e66
|
|||
b0c27b3c3e
|
|||
a33c682974
|
8
.github/ISSUE_TEMPLATE/BUG.md
vendored
8
.github/ISSUE_TEMPLATE/BUG.md
vendored
@ -1,8 +1,8 @@
|
|||||||
---
|
---
|
||||||
name: '🐛 Bug Report'
|
name: "🐛 Bug Report"
|
||||||
about: 'Report an unexpected problem or unintended behavior.'
|
about: "Report an unexpected problem or unintended behavior."
|
||||||
title: '[Bug]'
|
title: "[Bug]"
|
||||||
labels: 'bug'
|
labels: "bug"
|
||||||
---
|
---
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
|
8
.github/ISSUE_TEMPLATE/DOCUMENTATION.md
vendored
8
.github/ISSUE_TEMPLATE/DOCUMENTATION.md
vendored
@ -1,8 +1,8 @@
|
|||||||
---
|
---
|
||||||
name: '📜 Documentation'
|
name: "📜 Documentation"
|
||||||
about: 'Correct spelling errors, improvements or additions to documentation files (README, CONTRIBUTING...).'
|
about: "Correct spelling errors, improvements or additions to documentation files (README, CONTRIBUTING...)."
|
||||||
title: '[Documentation]'
|
title: "[Documentation]"
|
||||||
labels: 'documentation'
|
labels: "documentation"
|
||||||
---
|
---
|
||||||
|
|
||||||
<!-- Please make sure your issue has not already been fixed. -->
|
<!-- Please make sure your issue has not already been fixed. -->
|
||||||
|
8
.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md
vendored
8
.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md
vendored
@ -1,8 +1,8 @@
|
|||||||
---
|
---
|
||||||
name: '✨ Feature Request'
|
name: "✨ Feature Request"
|
||||||
about: 'Suggest a new feature idea.'
|
about: "Suggest a new feature idea."
|
||||||
title: '[Feature]'
|
title: "[Feature]"
|
||||||
labels: 'feature request'
|
labels: "feature request"
|
||||||
---
|
---
|
||||||
|
|
||||||
<!-- Please make sure your issue has not already been fixed. -->
|
<!-- Please make sure your issue has not already been fixed. -->
|
||||||
|
8
.github/ISSUE_TEMPLATE/IMPROVEMENT.md
vendored
8
.github/ISSUE_TEMPLATE/IMPROVEMENT.md
vendored
@ -1,8 +1,8 @@
|
|||||||
---
|
---
|
||||||
name: '🔧 Improvement'
|
name: "🔧 Improvement"
|
||||||
about: 'Improve structure/format/performance/refactor/tests of the code.'
|
about: "Improve structure/format/performance/refactor/tests of the code."
|
||||||
title: '[Improvement]'
|
title: "[Improvement]"
|
||||||
labels: 'improvement'
|
labels: "improvement"
|
||||||
---
|
---
|
||||||
|
|
||||||
<!-- Please make sure your issue has not already been fixed. -->
|
<!-- Please make sure your issue has not already been fixed. -->
|
||||||
|
8
.github/ISSUE_TEMPLATE/QUESTION.md
vendored
8
.github/ISSUE_TEMPLATE/QUESTION.md
vendored
@ -1,8 +1,8 @@
|
|||||||
---
|
---
|
||||||
name: '🙋 Question'
|
name: "🙋 Question"
|
||||||
about: 'Further information is requested.'
|
about: "Further information is requested."
|
||||||
title: '[Question]'
|
title: "[Question]"
|
||||||
labels: 'question'
|
labels: "question"
|
||||||
---
|
---
|
||||||
|
|
||||||
### Question
|
### Question
|
||||||
|
26
.github/workflows/lint.yml
vendored
26
.github/workflows/lint.yml
vendored
@ -1,4 +1,4 @@
|
|||||||
name: 'Lint'
|
name: "Lint"
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
@ -8,21 +8,21 @@ on:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
lint:
|
lint:
|
||||||
runs-on: 'ubuntu-latest'
|
runs-on: "ubuntu-latest"
|
||||||
steps:
|
steps:
|
||||||
- uses: 'actions/checkout@v3.5.2'
|
- uses: "actions/checkout@v4.1.1"
|
||||||
|
|
||||||
- name: 'Setup Node.js'
|
- name: "Setup Node.js"
|
||||||
uses: 'actions/setup-node@v3.6.0'
|
uses: "actions/setup-node@v4.0.1"
|
||||||
with:
|
with:
|
||||||
node-version: 'lts/*'
|
node-version: "lts/*"
|
||||||
cache: 'npm'
|
cache: "npm"
|
||||||
|
|
||||||
- name: 'Install dependencies'
|
- name: "Install dependencies"
|
||||||
run: 'npm clean-install'
|
run: "npm clean-install"
|
||||||
|
|
||||||
- run: 'npm run lint:commit -- --to "${{ github.sha }}"'
|
- run: 'npm run lint:commit -- --to "${{ github.sha }}"'
|
||||||
- run: 'npm run lint:editorconfig'
|
- run: "npm run lint:editorconfig"
|
||||||
- run: 'npm run lint:markdown'
|
- run: "npm run lint:markdown"
|
||||||
- run: 'npm run lint:eslint'
|
- run: "npm run lint:eslint"
|
||||||
- run: 'npm run lint:prettier'
|
- run: "npm run lint:prettier"
|
||||||
|
34
.github/workflows/release.yml
vendored
34
.github/workflows/release.yml
vendored
@ -1,4 +1,4 @@
|
|||||||
name: 'Release'
|
name: "Release"
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
@ -6,32 +6,32 @@ on:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
release:
|
release:
|
||||||
runs-on: 'ubuntu-latest'
|
runs-on: "ubuntu-latest"
|
||||||
permissions:
|
permissions:
|
||||||
contents: 'write'
|
contents: "write"
|
||||||
issues: 'write'
|
issues: "write"
|
||||||
pull-requests: 'write'
|
pull-requests: "write"
|
||||||
id-token: 'write'
|
id-token: "write"
|
||||||
steps:
|
steps:
|
||||||
- uses: 'actions/checkout@v3.5.2'
|
- uses: "actions/checkout@v4.1.1"
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
|
|
||||||
- name: 'Setup Node.js'
|
- name: "Setup Node.js"
|
||||||
uses: 'actions/setup-node@v3.6.0'
|
uses: "actions/setup-node@v4.0.1"
|
||||||
with:
|
with:
|
||||||
node-version: 'lts/*'
|
node-version: "lts/*"
|
||||||
cache: 'npm'
|
cache: "npm"
|
||||||
|
|
||||||
- name: 'Install dependencies'
|
- name: "Install dependencies"
|
||||||
run: 'npm clean-install'
|
run: "npm clean-install"
|
||||||
|
|
||||||
- name: 'Verify the integrity of provenance attestations and registry signatures for installed dependencies'
|
- name: "Verify the integrity of provenance attestations and registry signatures for installed dependencies"
|
||||||
run: 'npm audit signatures'
|
run: "npm audit signatures"
|
||||||
|
|
||||||
- name: 'Release'
|
- name: "Release"
|
||||||
run: 'npm run release'
|
run: "npm run release"
|
||||||
env:
|
env:
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
|
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
|
||||||
|
22
.github/workflows/test.yml
vendored
22
.github/workflows/test.yml
vendored
@ -1,4 +1,4 @@
|
|||||||
name: 'Test'
|
name: "Test"
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
@ -8,18 +8,18 @@ on:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
test:
|
test:
|
||||||
runs-on: 'ubuntu-latest'
|
runs-on: "ubuntu-latest"
|
||||||
steps:
|
steps:
|
||||||
- uses: 'actions/checkout@v3.5.2'
|
- uses: "actions/checkout@v4.1.1"
|
||||||
|
|
||||||
- name: 'Setup Node.js'
|
- name: "Setup Node.js"
|
||||||
uses: 'actions/setup-node@v3.6.0'
|
uses: "actions/setup-node@v4.0.1"
|
||||||
with:
|
with:
|
||||||
node-version: 'lts/*'
|
node-version: "lts/*"
|
||||||
cache: 'npm'
|
cache: "npm"
|
||||||
|
|
||||||
- name: 'Install dependencies'
|
- name: "Install dependencies"
|
||||||
run: 'npm clean-install'
|
run: "npm clean-install"
|
||||||
|
|
||||||
- name: 'Test'
|
- name: "Test"
|
||||||
run: 'npm run test'
|
run: "npm run test"
|
||||||
|
@ -1,6 +1,3 @@
|
|||||||
{
|
{
|
||||||
"singleQuote": true,
|
"semi": false
|
||||||
"jsxSingleQuote": true,
|
|
||||||
"semi": false,
|
|
||||||
"trailingComma": "none"
|
|
||||||
}
|
}
|
||||||
|
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@ -3,6 +3,6 @@
|
|||||||
"prettier.configPath": ".prettierrc.json",
|
"prettier.configPath": ".prettierrc.json",
|
||||||
"editor.formatOnSave": true,
|
"editor.formatOnSave": true,
|
||||||
"editor.codeActionsOnSave": {
|
"editor.codeActionsOnSave": {
|
||||||
"source.fixAll": true
|
"source.fixAll": "explicit"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
2
LICENSE
2
LICENSE
@ -1,4 +1,4 @@
|
|||||||
MIT License
|
# MIT License
|
||||||
|
|
||||||
Copyright (c) Théo LUDWIG
|
Copyright (c) Théo LUDWIG
|
||||||
|
|
||||||
|
12
README.md
12
README.md
@ -5,12 +5,12 @@
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="./CONTRIBUTING.md"><img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat" /></a>
|
<a href="./CONTRIBUTING.md"><img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat" alt="CONTRIBUTING" /></a>
|
||||||
<a href="./LICENSE"><img src="https://img.shields.io/badge/licence-MIT-blue.svg" alt="Licence MIT"/></a>
|
<a href="./LICENSE"><img src="https://img.shields.io/badge/licence-MIT-blue.svg" alt="Licence MIT"/></a>
|
||||||
<a href="./CODE_OF_CONDUCT.md"><img src="https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg" alt="Contributor Covenant" /></a>
|
<a href="./CODE_OF_CONDUCT.md"><img src="https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg" alt="Contributor Covenant" /></a>
|
||||||
<br />
|
<br />
|
||||||
<a href="https://github.com/theoludwig/markdownlint-rule-relative-links/actions/workflows/lint.yml"><img src="https://github.com/theoludwig/markdownlint-rule-relative-links/actions/workflows/lint.yml/badge.svg?branch=develop" /></a>
|
<a href="https://github.com/theoludwig/markdownlint-rule-relative-links/actions/workflows/lint.yml"><img src="https://github.com/theoludwig/markdownlint-rule-relative-links/actions/workflows/lint.yml/badge.svg?branch=develop" alt="Lint" /></a>
|
||||||
<a href="https://github.com/theoludwig/markdownlint-rule-relative-linksactions/workflows/test.yml"><img src="https://github.com/theoludwig/markdownlint-rule-relative-links/actions/workflows/test.yml/badge.svg?branch=develop" /></a>
|
<a href="https://github.com/theoludwig/markdownlint-rule-relative-linksactions/workflows/test.yml"><img src="https://github.com/theoludwig/markdownlint-rule-relative-links/actions/workflows/test.yml/badge.svg?branch=develop" alt="Test" /></a>
|
||||||
<br />
|
<br />
|
||||||
<a href="https://conventionalcommits.org"><img src="https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg" alt="Conventional Commits" /></a>
|
<a href="https://conventionalcommits.org"><img src="https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg" alt="Conventional Commits" /></a>
|
||||||
<a href="https://github.com/semantic-release/semantic-release"><img src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg" alt="semantic-release" /></a>
|
<a href="https://github.com/semantic-release/semantic-release"><img src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg" alt="semantic-release" /></a>
|
||||||
@ -21,7 +21,7 @@
|
|||||||
|
|
||||||
**markdownlint-rule-relative-links** is a [markdownlint](https://github.com/DavidAnson/markdownlint) custom rule to validate relative links.
|
**markdownlint-rule-relative-links** is a [markdownlint](https://github.com/DavidAnson/markdownlint) custom rule to validate relative links.
|
||||||
|
|
||||||
It ensures that relative links (using `file:` protocol) are working and not "dead" which means that it exists in the file system of the project that uses [markdownlint](https://github.com/DavidAnson/markdownlint).
|
It ensures that relative links (using `file:` protocol) are working and exists in the file system of the project that uses [markdownlint](https://github.com/DavidAnson/markdownlint).
|
||||||
|
|
||||||
### Example
|
### Example
|
||||||
|
|
||||||
@ -37,13 +37,13 @@ With `awesome.md` content:
|
|||||||
```md
|
```md
|
||||||
[abc](./abc.txt)
|
[abc](./abc.txt)
|
||||||
|
|
||||||
[Dead link](./dead.txt)
|
[Invalid link](./invalid.txt)
|
||||||
```
|
```
|
||||||
|
|
||||||
Running [markdownlint-cli2](https://github.com/DavidAnson/markdownlint-cli2) with `markdownlint-rule-relative-links` will output:
|
Running [markdownlint-cli2](https://github.com/DavidAnson/markdownlint-cli2) with `markdownlint-rule-relative-links` will output:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
awesome.md:3 relative-links Relative links should be valid [Link "./dead.txt" is dead]
|
awesome.md:3 relative-links Relative links should be valid [Link "./invalid.txt" should exist in the file system]
|
||||||
```
|
```
|
||||||
|
|
||||||
### Related links
|
### Related links
|
||||||
|
5447
package-lock.json
generated
5447
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
34
package.json
34
package.json
@ -33,7 +33,7 @@
|
|||||||
"lint:commit": "commitlint",
|
"lint:commit": "commitlint",
|
||||||
"lint:editorconfig": "editorconfig-checker",
|
"lint:editorconfig": "editorconfig-checker",
|
||||||
"lint:markdown": "markdownlint-cli2",
|
"lint:markdown": "markdownlint-cli2",
|
||||||
"lint:eslint": "eslint . --ignore-path .gitignore",
|
"lint:eslint": "eslint . --max-warnings 0 --report-unused-disable-directives --ignore-path .gitignore",
|
||||||
"lint:prettier": "prettier . --check --ignore-path .gitignore",
|
"lint:prettier": "prettier . --check --ignore-path .gitignore",
|
||||||
"lint:staged": "lint-staged",
|
"lint:staged": "lint-staged",
|
||||||
"test": "node --test ./test",
|
"test": "node --test ./test",
|
||||||
@ -43,26 +43,26 @@
|
|||||||
"postpublish": "pinst --enable"
|
"postpublish": "pinst --enable"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"markdown-it": "13.0.1"
|
"markdown-it": "14.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@commitlint/cli": "17.6.5",
|
"@commitlint/cli": "18.4.3",
|
||||||
"@commitlint/config-conventional": "17.6.5",
|
"@commitlint/config-conventional": "18.4.3",
|
||||||
"@types/node": "20.3.1",
|
"@types/node": "20.10.5",
|
||||||
"editorconfig-checker": "5.0.1",
|
"editorconfig-checker": "5.1.2",
|
||||||
"eslint": "8.43.0",
|
"eslint": "8.56.0",
|
||||||
"eslint-config-conventions": "9.0.0",
|
"eslint-config-conventions": "13.1.0",
|
||||||
"eslint-config-prettier": "8.8.0",
|
"eslint-config-prettier": "9.1.0",
|
||||||
"eslint-plugin-import": "2.27.5",
|
"eslint-plugin-import": "2.29.1",
|
||||||
"eslint-plugin-prettier": "4.2.1",
|
"eslint-plugin-prettier": "5.1.2",
|
||||||
"eslint-plugin-promise": "6.1.1",
|
"eslint-plugin-promise": "6.1.1",
|
||||||
"eslint-plugin-unicorn": "47.0.0",
|
"eslint-plugin-unicorn": "50.0.1",
|
||||||
"husky": "8.0.3",
|
"husky": "8.0.3",
|
||||||
"lint-staged": "13.2.2",
|
"lint-staged": "15.2.0",
|
||||||
"markdownlint": "0.29.0",
|
"markdownlint": "0.32.1",
|
||||||
"markdownlint-cli2": "0.8.1",
|
"markdownlint-cli2": "0.11.0",
|
||||||
"pinst": "3.0.0",
|
"pinst": "3.0.0",
|
||||||
"prettier": "2.8.8",
|
"prettier": "3.1.1",
|
||||||
"semantic-release": "21.0.5"
|
"semantic-release": "22.0.12"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
64
src/index.js
64
src/index.js
@ -1,57 +1,63 @@
|
|||||||
'use strict'
|
"use strict"
|
||||||
|
|
||||||
const { pathToFileURL } = require('node:url')
|
const { pathToFileURL } = require("node:url")
|
||||||
const fs = require('node:fs')
|
const fs = require("node:fs")
|
||||||
|
|
||||||
const {
|
const {
|
||||||
filterTokens,
|
filterTokens,
|
||||||
addError,
|
addError,
|
||||||
convertHeadingToHTMLFragment,
|
convertHeadingToHTMLFragment,
|
||||||
getMarkdownHeadings
|
getMarkdownHeadings,
|
||||||
} = require('./utils.js')
|
} = require("./utils.js")
|
||||||
|
|
||||||
const customRule = {
|
const customRule = {
|
||||||
names: ['relative-links'],
|
names: ["relative-links"],
|
||||||
description: 'Relative links should be valid',
|
description: "Relative links should be valid",
|
||||||
tags: ['links'],
|
tags: ["links"],
|
||||||
function: (params, onError) => {
|
function: (params, onError) => {
|
||||||
filterTokens(params, 'inline', (token) => {
|
filterTokens(params, "inline", (token) => {
|
||||||
token.children.forEach((child) => {
|
for (const child of token.children) {
|
||||||
const { lineNumber, type, attrs } = child
|
const { lineNumber, type, attrs } = child
|
||||||
|
|
||||||
/** @type {string | null} */
|
/** @type {string | null} */
|
||||||
let hrefSrc = null
|
let hrefSrc = null
|
||||||
|
|
||||||
if (type === 'link_open') {
|
if (type === "link_open") {
|
||||||
attrs.forEach((attr) => {
|
for (const attr of attrs) {
|
||||||
if (attr[0] === 'href') {
|
if (attr[0] === "href") {
|
||||||
hrefSrc = attr[1]
|
hrefSrc = attr[1]
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'image') {
|
if (type === "image") {
|
||||||
attrs.forEach((attr) => {
|
for (const attr of attrs) {
|
||||||
if (attr[0] === 'src') {
|
if (attr[0] === "src") {
|
||||||
hrefSrc = attr[1]
|
hrefSrc = attr[1]
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hrefSrc != null) {
|
if (hrefSrc != null) {
|
||||||
const url = new URL(hrefSrc, pathToFileURL(params.name))
|
const url = new URL(hrefSrc, pathToFileURL(params.name))
|
||||||
const isRelative =
|
const isRelative =
|
||||||
url.protocol === 'file:' && !hrefSrc.startsWith('/')
|
url.protocol === "file:" && !hrefSrc.startsWith("/")
|
||||||
if (isRelative) {
|
if (isRelative) {
|
||||||
const detail = `Link "${hrefSrc}" is not valid`
|
const detail = `Link "${hrefSrc}"`
|
||||||
|
|
||||||
if (!fs.existsSync(url)) {
|
if (!fs.existsSync(url)) {
|
||||||
addError(onError, lineNumber, detail)
|
addError(
|
||||||
return
|
onError,
|
||||||
|
lineNumber,
|
||||||
|
`${detail} should exist in the file system`,
|
||||||
|
)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'link_open' && url.hash !== '') {
|
if (type === "link_open" && url.hash !== "") {
|
||||||
const fileContent = fs.readFileSync(url, { encoding: 'utf8' })
|
const fileContent = fs.readFileSync(url, { encoding: "utf8" })
|
||||||
const headings = getMarkdownHeadings(fileContent)
|
const headings = getMarkdownHeadings(fileContent)
|
||||||
|
|
||||||
/** @type {Map<string, number>} */
|
/** @type {Map<string, number>} */
|
||||||
@ -68,14 +74,18 @@ const customRule = {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if (!headingsHTMLFragments.includes(url.hash)) {
|
if (!headingsHTMLFragments.includes(url.hash)) {
|
||||||
addError(onError, lineNumber, detail)
|
addError(
|
||||||
|
onError,
|
||||||
|
lineNumber,
|
||||||
|
`${detail} should have a valid fragment`,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
},
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = customRule
|
module.exports = customRule
|
||||||
|
32
src/utils.js
32
src/utils.js
@ -1,9 +1,9 @@
|
|||||||
const MarkdownIt = require('markdown-it')
|
const MarkdownIt = require("markdown-it")
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calls the provided function for each matching token.
|
* Calls the provided function for each matching token.
|
||||||
*
|
*
|
||||||
* @param {Object} params RuleParams instance.
|
* @param {object} params RuleParams instance.
|
||||||
* @param {string} type Token type identifier.
|
* @param {string} type Token type identifier.
|
||||||
* @param {Function} handler Callback function.
|
* @param {Function} handler Callback function.
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
@ -19,12 +19,12 @@ const filterTokens = (params, type, handler) => {
|
|||||||
/**
|
/**
|
||||||
* Adds a generic error object via the onError callback.
|
* Adds a generic error object via the onError callback.
|
||||||
*
|
*
|
||||||
* @param {Object} onError RuleOnError instance.
|
* @param {object} onError RuleOnError instance.
|
||||||
* @param {number} lineNumber Line number.
|
* @param {number} lineNumber Line number.
|
||||||
* @param {string} [detail] Error details.
|
* @param {string} [detail] Error details.
|
||||||
* @param {string} [context] Error context.
|
* @param {string} [context] Error context.
|
||||||
* @param {number[]} [range] Column and length of error.
|
* @param {number[]} [range] Column and length of error.
|
||||||
* @param {Object} [fixInfo] RuleOnErrorFixInfo instance.
|
* @param {object} [fixInfo] RuleOnErrorFixInfo instance.
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
const addError = (onError, lineNumber, detail, context, range, fixInfo) => {
|
const addError = (onError, lineNumber, detail, context, range, fixInfo) => {
|
||||||
@ -33,7 +33,7 @@ const addError = (onError, lineNumber, detail, context, range, fixInfo) => {
|
|||||||
detail,
|
detail,
|
||||||
context,
|
context,
|
||||||
range,
|
range,
|
||||||
fixInfo
|
fixInfo,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,32 +41,30 @@ const addError = (onError, lineNumber, detail, context, range, fixInfo) => {
|
|||||||
* Converts a Markdown heading into an HTML fragment according to the rules
|
* Converts a Markdown heading into an HTML fragment according to the rules
|
||||||
* used by GitHub.
|
* used by GitHub.
|
||||||
*
|
*
|
||||||
* Taken from <https://github.com/DavidAnson/markdownlint/blob/d01180ec5a014083ee9d574b693a8d7fbc1e566d/lib/md051.js#L19>
|
* @see https://github.com/DavidAnson/markdownlint/blob/d01180ec5a014083ee9d574b693a8d7fbc1e566d/lib/md051.js#L1
|
||||||
*
|
|
||||||
* @param {string} inlineText Inline token for heading.
|
* @param {string} inlineText Inline token for heading.
|
||||||
* @returns {string} Fragment string for heading.
|
* @returns {string} Fragment string for heading.
|
||||||
*/
|
*/
|
||||||
const convertHeadingToHTMLFragment = (inlineText) => {
|
const convertHeadingToHTMLFragment = (inlineText) => {
|
||||||
return (
|
return (
|
||||||
'#' +
|
"#" +
|
||||||
encodeURIComponent(
|
encodeURIComponent(
|
||||||
inlineText
|
inlineText
|
||||||
.toLowerCase()
|
.toLowerCase()
|
||||||
// RegExp source with Ruby's \p{Word} expanded into its General Categories
|
// RegExp source with Ruby's \p{Word} expanded into its General Categories
|
||||||
// eslint-disable-next-line max-len
|
|
||||||
// https://github.com/gjtorikian/html-pipeline/blob/main/lib/html/pipeline/toc_filter.rb
|
// https://github.com/gjtorikian/html-pipeline/blob/main/lib/html/pipeline/toc_filter.rb
|
||||||
// https://ruby-doc.org/core-3.0.2/Regexp.html
|
// https://ruby-doc.org/core-3.0.2/Regexp.html
|
||||||
.replace(
|
.replace(
|
||||||
/[^\p{Letter}\p{Mark}\p{Number}\p{Connector_Punctuation}\- ]/gu,
|
/[^\p{Letter}\p{Mark}\p{Number}\p{Connector_Punctuation}\- ]/gu,
|
||||||
''
|
"",
|
||||||
)
|
)
|
||||||
.replace(/ /gu, '-')
|
.replace(/ /gu, "-"),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const headingTags = new Set(['h1', 'h2', 'h3', 'h4', 'h5', 'h6'])
|
const headingTags = new Set(["h1", "h2", "h3", "h4", "h5", "h6"])
|
||||||
const ignoredTokens = new Set(['heading_open', 'heading_close'])
|
const ignoredTokens = new Set(["heading_open", "heading_close"])
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the headings from a Markdown string.
|
* Gets the headings from a Markdown string.
|
||||||
@ -85,9 +83,9 @@ const getMarkdownHeadings = (content) => {
|
|||||||
|
|
||||||
for (const token of tokens) {
|
for (const token of tokens) {
|
||||||
if (headingTags.has(token.tag)) {
|
if (headingTags.has(token.tag)) {
|
||||||
if (token.type === 'heading_open') {
|
if (token.type === "heading_open") {
|
||||||
headingToken = token.markup
|
headingToken = token.markup
|
||||||
} else if (token.type === 'heading_close') {
|
} else if (token.type === "heading_close") {
|
||||||
headingToken = null
|
headingToken = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -105,7 +103,7 @@ const getMarkdownHeadings = (content) => {
|
|||||||
.map((token) => {
|
.map((token) => {
|
||||||
return token.content
|
return token.content
|
||||||
})
|
})
|
||||||
.join('')}`
|
.join("")}`,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,5 +114,5 @@ module.exports = {
|
|||||||
filterTokens,
|
filterTokens,
|
||||||
addError,
|
addError,
|
||||||
convertHeadingToHTMLFragment,
|
convertHeadingToHTMLFragment,
|
||||||
getMarkdownHeadings
|
getMarkdownHeadings,
|
||||||
}
|
}
|
||||||
|
@ -1,46 +1,46 @@
|
|||||||
const test = require('node:test')
|
const test = require("node:test")
|
||||||
const assert = require('node:assert/strict')
|
const assert = require("node:assert/strict")
|
||||||
|
|
||||||
const { markdownlint } = require('markdownlint').promises
|
const { markdownlint } = require("markdownlint").promises
|
||||||
|
|
||||||
const relativeLinks = require('../src/index.js')
|
const relativeLinks = require("../src/index.js")
|
||||||
|
|
||||||
test('ensure the rule validate correctly', async () => {
|
test("ensure the rule validate correctly", async () => {
|
||||||
const lintResults = await markdownlint({
|
const lintResults = await markdownlint({
|
||||||
files: ['test/fixtures/Valid.md', 'test/fixtures/Invalid.md'],
|
files: ["test/fixtures/Valid.md", "test/fixtures/Invalid.md"],
|
||||||
config: {
|
config: {
|
||||||
default: false,
|
default: false,
|
||||||
'relative-links': true
|
"relative-links": true,
|
||||||
},
|
},
|
||||||
customRules: [relativeLinks]
|
customRules: [relativeLinks],
|
||||||
})
|
})
|
||||||
assert.equal(lintResults['test/fixtures/Valid.md'].length, 0)
|
assert.equal(lintResults["test/fixtures/Valid.md"].length, 0)
|
||||||
assert.equal(lintResults['test/fixtures/Invalid.md'].length, 3)
|
assert.equal(lintResults["test/fixtures/Invalid.md"].length, 3)
|
||||||
|
|
||||||
assert.equal(
|
assert.equal(
|
||||||
lintResults['test/fixtures/Invalid.md'][0]?.ruleDescription,
|
lintResults["test/fixtures/Invalid.md"][0]?.ruleDescription,
|
||||||
'Relative links should be valid'
|
"Relative links should be valid",
|
||||||
)
|
)
|
||||||
assert.equal(
|
assert.equal(
|
||||||
lintResults['test/fixtures/Invalid.md'][0]?.errorDetail,
|
lintResults["test/fixtures/Invalid.md"][0]?.errorDetail,
|
||||||
'Link "./basic.test.js" is not valid'
|
'Link "./basic.test.js" should exist in the file system',
|
||||||
)
|
)
|
||||||
|
|
||||||
assert.equal(
|
assert.equal(
|
||||||
lintResults['test/fixtures/Invalid.md'][1]?.ruleDescription,
|
lintResults["test/fixtures/Invalid.md"][1]?.ruleDescription,
|
||||||
'Relative links should be valid'
|
"Relative links should be valid",
|
||||||
)
|
)
|
||||||
assert.equal(
|
assert.equal(
|
||||||
lintResults['test/fixtures/Invalid.md'][1]?.errorDetail,
|
lintResults["test/fixtures/Invalid.md"][1]?.errorDetail,
|
||||||
'Link "../image.png" is not valid'
|
'Link "../image.png" should exist in the file system',
|
||||||
)
|
)
|
||||||
|
|
||||||
assert.equal(
|
assert.equal(
|
||||||
lintResults['test/fixtures/Invalid.md'][2]?.ruleDescription,
|
lintResults["test/fixtures/Invalid.md"][2]?.ruleDescription,
|
||||||
'Relative links should be valid'
|
"Relative links should be valid",
|
||||||
)
|
)
|
||||||
assert.equal(
|
assert.equal(
|
||||||
lintResults['test/fixtures/Invalid.md'][2]?.errorDetail,
|
lintResults["test/fixtures/Invalid.md"][2]?.errorDetail,
|
||||||
'Link "./Valid.md#not-existing-heading" is not valid'
|
'Link "./Valid.md#not-existing-heading" should have a valid fragment',
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
@ -1,37 +1,37 @@
|
|||||||
const test = require('node:test')
|
const test = require("node:test")
|
||||||
const assert = require('node:assert/strict')
|
const assert = require("node:assert/strict")
|
||||||
|
|
||||||
const {
|
const {
|
||||||
convertHeadingToHTMLFragment,
|
convertHeadingToHTMLFragment,
|
||||||
getMarkdownHeadings
|
getMarkdownHeadings,
|
||||||
} = require('../src/utils.js')
|
} = require("../src/utils.js")
|
||||||
|
|
||||||
test('utils', async (t) => {
|
test("utils", async (t) => {
|
||||||
await t.test('convertHeadingToHTMLFragment', async () => {
|
await t.test("convertHeadingToHTMLFragment", async () => {
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
convertHeadingToHTMLFragment('Valid Fragments'),
|
convertHeadingToHTMLFragment("Valid Fragments"),
|
||||||
'#valid-fragments'
|
"#valid-fragments",
|
||||||
)
|
)
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
convertHeadingToHTMLFragment('Valid Heading With Underscores _'),
|
convertHeadingToHTMLFragment("Valid Heading With Underscores _"),
|
||||||
'#valid-heading-with-underscores-_'
|
"#valid-heading-with-underscores-_",
|
||||||
)
|
)
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
convertHeadingToHTMLFragment(
|
convertHeadingToHTMLFragment(
|
||||||
`Valid Heading With Quotes ' And Double Quotes "`
|
`Valid Heading With Quotes ' And Double Quotes "`,
|
||||||
),
|
),
|
||||||
'#valid-heading-with-quotes--and-double-quotes-'
|
"#valid-heading-with-quotes--and-double-quotes-",
|
||||||
)
|
)
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
convertHeadingToHTMLFragment('🚀 Valid Heading With Emoji'),
|
convertHeadingToHTMLFragment("🚀 Valid Heading With Emoji"),
|
||||||
'#-valid-heading-with-emoji'
|
"#-valid-heading-with-emoji",
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
await t.test('getMarkdownHeadings', async () => {
|
await t.test("getMarkdownHeadings", async () => {
|
||||||
assert.deepStrictEqual(
|
assert.deepStrictEqual(
|
||||||
getMarkdownHeadings('# Hello\n\n## World\n\n## Hello, world!\n'),
|
getMarkdownHeadings("# Hello\n\n## World\n\n## Hello, world!\n"),
|
||||||
['Hello', 'World', 'Hello, world!']
|
["Hello", "World", "Hello, world!"],
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user