Skip to content

Commit

Permalink
fix: cleaner code + better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
theoludwig committed Jan 9, 2024
1 parent fcd0340 commit 1ddcdc7
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 56 deletions.
1 change: 1 addition & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ jobs:
- run: "npm run lint:markdown"
- run: "npm run lint:eslint"
- run: "npm run lint:prettier"
- run: "npm run lint:javascript"
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
. "$(dirname "$0")/_/husky.sh"

npm run lint:staged
npm run lint:javascript
npm run test
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ With `awesome.md` content:
Running [markdownlint-cli2](https://github.com/DavidAnson/markdownlint-cli2) with `markdownlint-rule-relative-links` will output:

```sh
awesome.md:3 relative-links Relative links should be valid [Link "./invalid.txt" should exist in the file system]
awesome.md:3 relative-links Relative links should be valid ["./invalid.txt" should exist in the file system]
```

### Additional features
Expand Down
24 changes: 24 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"lib": ["ESNext"],
"target": "ESNext",
"module": "CommonJS",
"moduleResolution": "Node",
"checkJs": true,
"allowJs": true,
"noEmit": true,
"rootDir": ".",
"baseUrl": ".",
"strict": true,
"allowUnusedLabels": false,
"allowUnreachableCode": false,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
"noUncheckedIndexedAccess": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"forceConsistentCasingInFileNames": true
}
}
27 changes: 25 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"lint:markdown": "markdownlint-cli2",
"lint:eslint": "eslint . --max-warnings 0 --report-unused-disable-directives --ignore-path .gitignore",
"lint:prettier": "prettier . --check --ignore-path .gitignore",
"lint:javascript": "tsc --project jsconfig.json --noEmit",
"lint:staged": "lint-staged",
"test": "node --test ./test",
"release": "semantic-release",
Expand All @@ -48,6 +49,7 @@
"devDependencies": {
"@commitlint/cli": "18.4.4",
"@commitlint/config-conventional": "18.4.4",
"@types/markdown-it": "13.0.7",
"@types/node": "20.10.8",
"editorconfig-checker": "5.1.2",
"eslint": "8.56.0",
Expand All @@ -63,6 +65,7 @@
"markdownlint-cli2": "0.11.0",
"pinst": "3.0.0",
"prettier": "3.1.1",
"semantic-release": "22.0.12"
"semantic-release": "22.0.12",
"typescript": "5.3.3"
}
}
31 changes: 17 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,27 @@ const fs = require("node:fs")

const {
filterTokens,
addError,
convertHeadingToHTMLFragment,
getMarkdownHeadings,
} = require("./utils.js")

/** @typedef {import('markdownlint').Rule} MarkdownLintRule */

/**
* @type {MarkdownLintRule}
*/
const customRule = {
names: ["relative-links"],
description: "Relative links should be valid",
tags: ["links"],
function: (params, onError) => {
filterTokens(params, "inline", (token) => {
for (const child of token.children) {
const { lineNumber, type, attrs } = child
const children = token.children ?? []
for (const child of children) {
const { type, attrs, lineNumber } = child

/** @type {string | null} */
let hrefSrc = null
/** @type {string | undefined} */
let hrefSrc

if (type === "link_open") {
for (const attr of attrs) {
Expand All @@ -45,14 +50,13 @@ const customRule = {
const isRelative =
url.protocol === "file:" && !hrefSrc.startsWith("/")
if (isRelative) {
const detail = `Link "${hrefSrc}"`
const detail = `"${hrefSrc}"`

if (!fs.existsSync(url)) {
addError(
onError,
onError({
lineNumber,
`${detail} should exist in the file system`,
)
detail: `${detail} should exist in the file system`,
})
continue
}

Expand All @@ -74,11 +78,10 @@ const customRule = {
})

if (!headingsHTMLFragments.includes(url.hash)) {
addError(
onError,
onError({
lineNumber,
`${detail} should have a valid fragment`,
)
detail: `${detail} should have a valid fragment identifier`,
})
}
}
}
Expand Down
33 changes: 8 additions & 25 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const MarkdownIt = require("markdown-it")

/** @typedef {import('markdownlint').RuleParams} MarkdownLintRuleParams */
/** @typedef {import('markdownlint').MarkdownItToken} MarkdownItToken */

/**
* Calls the provided function for each matching token.
*
* @param {object} params RuleParams instance.
* @param {MarkdownLintRuleParams} params RuleParams instance.
* @param {string} type Token type identifier.
* @param {Function} handler Callback function.
* @param {(token: MarkdownItToken) => void} handler Callback function.
* @returns {void}
*/
const filterTokens = (params, type, handler) => {
Expand All @@ -16,27 +19,6 @@ const filterTokens = (params, type, handler) => {
}
}

/**
* Adds a generic error object via the onError callback.
*
* @param {object} onError RuleOnError instance.
* @param {number} lineNumber Line number.
* @param {string} [detail] Error details.
* @param {string} [context] Error context.
* @param {number[]} [range] Column and length of error.
* @param {object} [fixInfo] RuleOnErrorFixInfo instance.
* @returns {void}
*/
const addError = (onError, lineNumber, detail, context, range, fixInfo) => {
onError({
lineNumber,
detail,
context,
range,
fixInfo,
})
}

/**
* Converts a Markdown heading into an HTML fragment according to the rules
* used by GitHub.
Expand Down Expand Up @@ -98,8 +80,10 @@ const getMarkdownHeadings = (content) => {
continue
}

const children = token.children ?? []

headings.push(
`${token.children
`${children
.map((token) => {
return token.content
})
Expand All @@ -112,7 +96,6 @@ const getMarkdownHeadings = (content) => {

module.exports = {
filterTokens,
addError,
convertHeadingToHTMLFragment,
getMarkdownHeadings,
}
24 changes: 12 additions & 12 deletions test/basic.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const test = require("node:test")
const { test } = require("node:test")
const assert = require("node:assert/strict")

const { markdownlint } = require("markdownlint").promises
Expand All @@ -14,33 +14,33 @@ test("ensure the rule validate correctly", async () => {
},
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,
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,
'"./basic.test.js" should exist in the file system',
)

assert.equal(
lintResults["test/fixtures/Invalid.md"][1]?.ruleDescription,
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,
'"../image.png" should exist in the file system',
)

assert.equal(
lintResults["test/fixtures/Invalid.md"][2]?.ruleDescription,
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,
'"./Valid.md#not-existing-heading" should have a valid fragment identifier',
)
})
2 changes: 1 addition & 1 deletion test/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const test = require("node:test")
const { test } = require("node:test")
const assert = require("node:assert/strict")

const {
Expand Down

0 comments on commit 1ddcdc7

Please sign in to comment.