4 Commits

Author SHA1 Message Date
f99e2bbe4d docs: fix linting 2023-11-23 00:40:34 +01:00
e642c7ceee fix: update dependencies to latest 2023-11-23 00:37:15 +01:00
9cf3168e66 chore: better Prettier config for easier reviews 2023-10-23 23:11:41 +02:00
b0c27b3c3e build(deps): update latest
All checks were successful
Lint / lint (push) Successful in 54s
Test / test (push) Successful in 33s
2023-07-18 23:18:39 +02:00
16 changed files with 2880 additions and 2790 deletions

View File

@ -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"
--- ---
<!-- <!--

View File

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

View File

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

View File

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

View File

@ -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

View File

@ -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.0"
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"

View File

@ -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.0"
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 }}

View File

@ -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.0"
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"

View File

@ -1,6 +1,3 @@
{ {
"singleQuote": true, "semi": false
"jsxSingleQuote": true,
"semi": false,
"trailingComma": "none"
} }

View File

@ -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>

5342
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -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": "13.0.2"
}, },
"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.9.4",
"editorconfig-checker": "5.0.1", "editorconfig-checker": "5.1.2",
"eslint": "8.43.0", "eslint": "8.54.0",
"eslint-config-conventions": "9.0.0", "eslint-config-conventions": "13.0.0",
"eslint-config-prettier": "8.8.0", "eslint-config-prettier": "9.0.0",
"eslint-plugin-import": "2.27.5", "eslint-plugin-import": "2.29.0",
"eslint-plugin-prettier": "4.2.1", "eslint-plugin-prettier": "5.0.1",
"eslint-plugin-promise": "6.1.1", "eslint-plugin-promise": "6.1.1",
"eslint-plugin-unicorn": "47.0.0", "eslint-plugin-unicorn": "49.0.0",
"husky": "8.0.3", "husky": "8.0.3",
"lint-staged": "13.2.2", "lint-staged": "15.1.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.0",
"semantic-release": "21.0.5" "semantic-release": "22.0.8"
} }
} }

View File

@ -1,47 +1,49 @@
'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}"` const detail = `Link "${hrefSrc}"`
@ -49,13 +51,13 @@ const customRule = {
addError( addError(
onError, onError,
lineNumber, lineNumber,
`${detail} should exist in the file system` `${detail} should exist in the file system`,
) )
return 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>} */
@ -75,15 +77,15 @@ const customRule = {
addError( addError(
onError, onError,
lineNumber, lineNumber,
`${detail} should have a valid fragment` `${detail} should have a valid fragment`,
) )
} }
} }
} }
} }
}) }
}) })
} },
} }
module.exports = customRule module.exports = customRule

View File

@ -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,
}) })
} }
@ -47,25 +47,24 @@ const addError = (onError, lineNumber, detail, context, range, fixInfo) => {
*/ */
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.
@ -84,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
} }
} }
@ -104,7 +103,7 @@ const getMarkdownHeadings = (content) => {
.map((token) => { .map((token) => {
return token.content return token.content
}) })
.join('')}` .join("")}`,
) )
} }
@ -115,5 +114,5 @@ module.exports = {
filterTokens, filterTokens,
addError, addError,
convertHeadingToHTMLFragment, convertHeadingToHTMLFragment,
getMarkdownHeadings getMarkdownHeadings,
} }

View File

@ -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" should exist in the file system' '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" should exist in the file system' '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" should have a valid fragment' 'Link "./Valid.md#not-existing-heading" should have a valid fragment',
) )
}) })

View File

@ -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!"],
) )
}) })
}) })