mirror of
https://github.com/theoludwig/markdownlint-rule-relative-links.git
synced 2025-05-27 11:37:24 +02:00
chore: better Prettier config for easier reviews
This commit is contained in:
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,
|
||||
}
|
||||
|
Reference in New Issue
Block a user