mirror of
https://github.com/theoludwig/markdownlint-rule-relative-links.git
synced 2024-12-08 00:45:32 +01:00
chore: better Prettier config for easier reviews
This commit is contained in:
parent
b0c27b3c3e
commit
9cf3168e66
8
.github/ISSUE_TEMPLATE/BUG.md
vendored
8
.github/ISSUE_TEMPLATE/BUG.md
vendored
@ -1,8 +1,8 @@
|
||||
---
|
||||
name: '🐛 Bug Report'
|
||||
about: 'Report an unexpected problem or unintended behavior.'
|
||||
title: '[Bug]'
|
||||
labels: 'bug'
|
||||
name: "🐛 Bug Report"
|
||||
about: "Report an unexpected problem or unintended behavior."
|
||||
title: "[Bug]"
|
||||
labels: "bug"
|
||||
---
|
||||
|
||||
<!--
|
||||
|
8
.github/ISSUE_TEMPLATE/DOCUMENTATION.md
vendored
8
.github/ISSUE_TEMPLATE/DOCUMENTATION.md
vendored
@ -1,8 +1,8 @@
|
||||
---
|
||||
name: '📜 Documentation'
|
||||
about: 'Correct spelling errors, improvements or additions to documentation files (README, CONTRIBUTING...).'
|
||||
title: '[Documentation]'
|
||||
labels: 'documentation'
|
||||
name: "📜 Documentation"
|
||||
about: "Correct spelling errors, improvements or additions to documentation files (README, CONTRIBUTING...)."
|
||||
title: "[Documentation]"
|
||||
labels: "documentation"
|
||||
---
|
||||
|
||||
<!-- 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'
|
||||
about: 'Suggest a new feature idea.'
|
||||
title: '[Feature]'
|
||||
labels: 'feature request'
|
||||
name: "✨ Feature Request"
|
||||
about: "Suggest a new feature idea."
|
||||
title: "[Feature]"
|
||||
labels: "feature request"
|
||||
---
|
||||
|
||||
<!-- 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'
|
||||
about: 'Improve structure/format/performance/refactor/tests of the code.'
|
||||
title: '[Improvement]'
|
||||
labels: 'improvement'
|
||||
name: "🔧 Improvement"
|
||||
about: "Improve structure/format/performance/refactor/tests of the code."
|
||||
title: "[Improvement]"
|
||||
labels: "improvement"
|
||||
---
|
||||
|
||||
<!-- 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'
|
||||
about: 'Further information is requested.'
|
||||
title: '[Question]'
|
||||
labels: 'question'
|
||||
name: "🙋 Question"
|
||||
about: "Further information is requested."
|
||||
title: "[Question]"
|
||||
labels: "question"
|
||||
---
|
||||
|
||||
### Question
|
||||
|
26
.github/workflows/lint.yml
vendored
26
.github/workflows/lint.yml
vendored
@ -1,4 +1,4 @@
|
||||
name: 'Lint'
|
||||
name: "Lint"
|
||||
|
||||
on:
|
||||
push:
|
||||
@ -8,21 +8,21 @@ on:
|
||||
|
||||
jobs:
|
||||
lint:
|
||||
runs-on: 'ubuntu-latest'
|
||||
runs-on: "ubuntu-latest"
|
||||
steps:
|
||||
- uses: 'actions/checkout@v3.5.2'
|
||||
- uses: "actions/checkout@v3.5.2"
|
||||
|
||||
- name: 'Setup Node.js'
|
||||
uses: 'actions/setup-node@v3.6.0'
|
||||
- name: "Setup Node.js"
|
||||
uses: "actions/setup-node@v3.6.0"
|
||||
with:
|
||||
node-version: 'lts/*'
|
||||
cache: 'npm'
|
||||
node-version: "lts/*"
|
||||
cache: "npm"
|
||||
|
||||
- name: 'Install dependencies'
|
||||
run: 'npm clean-install'
|
||||
- name: "Install dependencies"
|
||||
run: "npm clean-install"
|
||||
|
||||
- run: 'npm run lint:commit -- --to "${{ github.sha }}"'
|
||||
- run: 'npm run lint:editorconfig'
|
||||
- run: 'npm run lint:markdown'
|
||||
- run: 'npm run lint:eslint'
|
||||
- run: 'npm run lint:prettier'
|
||||
- run: "npm run lint:editorconfig"
|
||||
- run: "npm run lint:markdown"
|
||||
- run: "npm run lint:eslint"
|
||||
- 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:
|
||||
push:
|
||||
@ -6,32 +6,32 @@ on:
|
||||
|
||||
jobs:
|
||||
release:
|
||||
runs-on: 'ubuntu-latest'
|
||||
runs-on: "ubuntu-latest"
|
||||
permissions:
|
||||
contents: 'write'
|
||||
issues: 'write'
|
||||
pull-requests: 'write'
|
||||
id-token: 'write'
|
||||
contents: "write"
|
||||
issues: "write"
|
||||
pull-requests: "write"
|
||||
id-token: "write"
|
||||
steps:
|
||||
- uses: 'actions/checkout@v3.5.2'
|
||||
- uses: "actions/checkout@v3.5.2"
|
||||
with:
|
||||
fetch-depth: 0
|
||||
persist-credentials: false
|
||||
|
||||
- name: 'Setup Node.js'
|
||||
uses: 'actions/setup-node@v3.6.0'
|
||||
- name: "Setup Node.js"
|
||||
uses: "actions/setup-node@v3.6.0"
|
||||
with:
|
||||
node-version: 'lts/*'
|
||||
cache: 'npm'
|
||||
node-version: "lts/*"
|
||||
cache: "npm"
|
||||
|
||||
- name: 'Install dependencies'
|
||||
run: 'npm clean-install'
|
||||
- name: "Install dependencies"
|
||||
run: "npm clean-install"
|
||||
|
||||
- name: 'Verify the integrity of provenance attestations and registry signatures for installed dependencies'
|
||||
run: 'npm audit signatures'
|
||||
- name: "Verify the integrity of provenance attestations and registry signatures for installed dependencies"
|
||||
run: "npm audit signatures"
|
||||
|
||||
- name: 'Release'
|
||||
run: 'npm run release'
|
||||
- name: "Release"
|
||||
run: "npm run release"
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_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:
|
||||
push:
|
||||
@ -8,18 +8,18 @@ on:
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: 'ubuntu-latest'
|
||||
runs-on: "ubuntu-latest"
|
||||
steps:
|
||||
- uses: 'actions/checkout@v3.5.2'
|
||||
- uses: "actions/checkout@v3.5.2"
|
||||
|
||||
- name: 'Setup Node.js'
|
||||
uses: 'actions/setup-node@v3.6.0'
|
||||
- name: "Setup Node.js"
|
||||
uses: "actions/setup-node@v3.6.0"
|
||||
with:
|
||||
node-version: 'lts/*'
|
||||
cache: 'npm'
|
||||
node-version: "lts/*"
|
||||
cache: "npm"
|
||||
|
||||
- name: 'Install dependencies'
|
||||
run: 'npm clean-install'
|
||||
- name: "Install dependencies"
|
||||
run: "npm clean-install"
|
||||
|
||||
- name: 'Test'
|
||||
run: 'npm run test'
|
||||
- name: "Test"
|
||||
run: "npm run test"
|
||||
|
@ -1,6 +1,3 @@
|
||||
{
|
||||
"singleQuote": true,
|
||||
"jsxSingleQuote": true,
|
||||
"semi": false,
|
||||
"trailingComma": "none"
|
||||
"semi": false
|
||||
}
|
||||
|
38
src/index.js
38
src/index.js
@ -1,39 +1,39 @@
|
||||
'use strict'
|
||||
"use strict"
|
||||
|
||||
const { pathToFileURL } = require('node:url')
|
||||
const fs = require('node:fs')
|
||||
const { pathToFileURL } = require("node:url")
|
||||
const fs = require("node:fs")
|
||||
|
||||
const {
|
||||
filterTokens,
|
||||
addError,
|
||||
convertHeadingToHTMLFragment,
|
||||
getMarkdownHeadings
|
||||
} = require('./utils.js')
|
||||
getMarkdownHeadings,
|
||||
} = require("./utils.js")
|
||||
|
||||
const customRule = {
|
||||
names: ['relative-links'],
|
||||
description: 'Relative links should be valid',
|
||||
tags: ['links'],
|
||||
names: ["relative-links"],
|
||||
description: "Relative links should be valid",
|
||||
tags: ["links"],
|
||||
function: (params, onError) => {
|
||||
filterTokens(params, 'inline', (token) => {
|
||||
filterTokens(params, "inline", (token) => {
|
||||
for (const child of token.children) {
|
||||
const { lineNumber, type, attrs } = child
|
||||
|
||||
/** @type {string | null} */
|
||||
let hrefSrc = null
|
||||
|
||||
if (type === 'link_open') {
|
||||
if (type === "link_open") {
|
||||
for (const attr of attrs) {
|
||||
if (attr[0] === 'href') {
|
||||
if (attr[0] === "href") {
|
||||
hrefSrc = attr[1]
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (type === 'image') {
|
||||
if (type === "image") {
|
||||
for (const attr of attrs) {
|
||||
if (attr[0] === 'src') {
|
||||
if (attr[0] === "src") {
|
||||
hrefSrc = attr[1]
|
||||
break
|
||||
}
|
||||
@ -43,7 +43,7 @@ const customRule = {
|
||||
if (hrefSrc != null) {
|
||||
const url = new URL(hrefSrc, pathToFileURL(params.name))
|
||||
const isRelative =
|
||||
url.protocol === 'file:' && !hrefSrc.startsWith('/')
|
||||
url.protocol === "file:" && !hrefSrc.startsWith("/")
|
||||
if (isRelative) {
|
||||
const detail = `Link "${hrefSrc}"`
|
||||
|
||||
@ -51,13 +51,13 @@ const customRule = {
|
||||
addError(
|
||||
onError,
|
||||
lineNumber,
|
||||
`${detail} should exist in the file system`
|
||||
`${detail} should exist in the file system`,
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
if (type === 'link_open' && url.hash !== '') {
|
||||
const fileContent = fs.readFileSync(url, { encoding: 'utf8' })
|
||||
if (type === "link_open" && url.hash !== "") {
|
||||
const fileContent = fs.readFileSync(url, { encoding: "utf8" })
|
||||
const headings = getMarkdownHeadings(fileContent)
|
||||
|
||||
/** @type {Map<string, number>} */
|
||||
@ -77,7 +77,7 @@ const customRule = {
|
||||
addError(
|
||||
onError,
|
||||
lineNumber,
|
||||
`${detail} should have a valid fragment`
|
||||
`${detail} should have a valid fragment`,
|
||||
)
|
||||
}
|
||||
}
|
||||
@ -85,7 +85,7 @@ const customRule = {
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
module.exports = customRule
|
||||
|
22
src/utils.js
22
src/utils.js
@ -1,4 +1,4 @@
|
||||
const MarkdownIt = require('markdown-it')
|
||||
const MarkdownIt = require("markdown-it")
|
||||
|
||||
/**
|
||||
* Calls the provided function for each matching token.
|
||||
@ -33,7 +33,7 @@ const addError = (onError, lineNumber, detail, context, range, fixInfo) => {
|
||||
detail,
|
||||
context,
|
||||
range,
|
||||
fixInfo
|
||||
fixInfo,
|
||||
})
|
||||
}
|
||||
|
||||
@ -47,7 +47,7 @@ const addError = (onError, lineNumber, detail, context, range, fixInfo) => {
|
||||
*/
|
||||
const convertHeadingToHTMLFragment = (inlineText) => {
|
||||
return (
|
||||
'#' +
|
||||
"#" +
|
||||
encodeURIComponent(
|
||||
inlineText
|
||||
.toLowerCase()
|
||||
@ -56,15 +56,15 @@ const convertHeadingToHTMLFragment = (inlineText) => {
|
||||
// https://ruby-doc.org/core-3.0.2/Regexp.html
|
||||
.replace(
|
||||
/[^\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 ignoredTokens = new Set(['heading_open', 'heading_close'])
|
||||
const headingTags = new Set(["h1", "h2", "h3", "h4", "h5", "h6"])
|
||||
const ignoredTokens = new Set(["heading_open", "heading_close"])
|
||||
|
||||
/**
|
||||
* Gets the headings from a Markdown string.
|
||||
@ -83,9 +83,9 @@ const getMarkdownHeadings = (content) => {
|
||||
|
||||
for (const token of tokens) {
|
||||
if (headingTags.has(token.tag)) {
|
||||
if (token.type === 'heading_open') {
|
||||
if (token.type === "heading_open") {
|
||||
headingToken = token.markup
|
||||
} else if (token.type === 'heading_close') {
|
||||
} else if (token.type === "heading_close") {
|
||||
headingToken = null
|
||||
}
|
||||
}
|
||||
@ -103,7 +103,7 @@ const getMarkdownHeadings = (content) => {
|
||||
.map((token) => {
|
||||
return token.content
|
||||
})
|
||||
.join('')}`
|
||||
.join("")}`,
|
||||
)
|
||||
}
|
||||
|
||||
@ -114,5 +114,5 @@ module.exports = {
|
||||
filterTokens,
|
||||
addError,
|
||||
convertHeadingToHTMLFragment,
|
||||
getMarkdownHeadings
|
||||
getMarkdownHeadings,
|
||||
}
|
||||
|
@ -1,46 +1,46 @@
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert/strict')
|
||||
const test = require("node:test")
|
||||
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({
|
||||
files: ['test/fixtures/Valid.md', 'test/fixtures/Invalid.md'],
|
||||
files: ["test/fixtures/Valid.md", "test/fixtures/Invalid.md"],
|
||||
config: {
|
||||
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/Invalid.md'].length, 3)
|
||||
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'][0]?.ruleDescription,
|
||||
'Relative links should be valid'
|
||||
lintResults["test/fixtures/Invalid.md"][0]?.ruleDescription,
|
||||
"Relative links should be valid",
|
||||
)
|
||||
assert.equal(
|
||||
lintResults['test/fixtures/Invalid.md'][0]?.errorDetail,
|
||||
'Link "./basic.test.js" should exist in the file system'
|
||||
lintResults["test/fixtures/Invalid.md"][0]?.errorDetail,
|
||||
'Link "./basic.test.js" should exist in the file system',
|
||||
)
|
||||
|
||||
assert.equal(
|
||||
lintResults['test/fixtures/Invalid.md'][1]?.ruleDescription,
|
||||
'Relative links should be valid'
|
||||
lintResults["test/fixtures/Invalid.md"][1]?.ruleDescription,
|
||||
"Relative links should be valid",
|
||||
)
|
||||
assert.equal(
|
||||
lintResults['test/fixtures/Invalid.md'][1]?.errorDetail,
|
||||
'Link "../image.png" should exist in the file system'
|
||||
lintResults["test/fixtures/Invalid.md"][1]?.errorDetail,
|
||||
'Link "../image.png" should exist in the file system',
|
||||
)
|
||||
|
||||
assert.equal(
|
||||
lintResults['test/fixtures/Invalid.md'][2]?.ruleDescription,
|
||||
'Relative links should be valid'
|
||||
lintResults["test/fixtures/Invalid.md"][2]?.ruleDescription,
|
||||
"Relative links should be valid",
|
||||
)
|
||||
assert.equal(
|
||||
lintResults['test/fixtures/Invalid.md'][2]?.errorDetail,
|
||||
'Link "./Valid.md#not-existing-heading" should have a valid fragment'
|
||||
lintResults["test/fixtures/Invalid.md"][2]?.errorDetail,
|
||||
'Link "./Valid.md#not-existing-heading" should have a valid fragment',
|
||||
)
|
||||
})
|
||||
|
@ -1,37 +1,37 @@
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert/strict')
|
||||
const test = require("node:test")
|
||||
const assert = require("node:assert/strict")
|
||||
|
||||
const {
|
||||
convertHeadingToHTMLFragment,
|
||||
getMarkdownHeadings
|
||||
} = require('../src/utils.js')
|
||||
getMarkdownHeadings,
|
||||
} = require("../src/utils.js")
|
||||
|
||||
test('utils', async (t) => {
|
||||
await t.test('convertHeadingToHTMLFragment', async () => {
|
||||
test("utils", async (t) => {
|
||||
await t.test("convertHeadingToHTMLFragment", async () => {
|
||||
assert.strictEqual(
|
||||
convertHeadingToHTMLFragment('Valid Fragments'),
|
||||
'#valid-fragments'
|
||||
convertHeadingToHTMLFragment("Valid Fragments"),
|
||||
"#valid-fragments",
|
||||
)
|
||||
assert.strictEqual(
|
||||
convertHeadingToHTMLFragment('Valid Heading With Underscores _'),
|
||||
'#valid-heading-with-underscores-_'
|
||||
convertHeadingToHTMLFragment("Valid Heading With Underscores _"),
|
||||
"#valid-heading-with-underscores-_",
|
||||
)
|
||||
assert.strictEqual(
|
||||
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(
|
||||
convertHeadingToHTMLFragment('🚀 Valid Heading With Emoji'),
|
||||
'#-valid-heading-with-emoji'
|
||||
convertHeadingToHTMLFragment("🚀 Valid Heading With Emoji"),
|
||||
"#-valid-heading-with-emoji",
|
||||
)
|
||||
})
|
||||
|
||||
await t.test('getMarkdownHeadings', async () => {
|
||||
await t.test("getMarkdownHeadings", async () => {
|
||||
assert.deepStrictEqual(
|
||||
getMarkdownHeadings('# Hello\n\n## World\n\n## Hello, world!\n'),
|
||||
['Hello', 'World', 'Hello, world!']
|
||||
getMarkdownHeadings("# Hello\n\n## World\n\n## Hello, world!\n"),
|
||||
["Hello", "World", "Hello, world!"],
|
||||
)
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user