mirror of
https://github.com/theoludwig/markdownlint-rule-relative-links.git
synced 2026-02-20 03:09:05 +01:00
fix: cleaner code + better error messages
This commit is contained in:
33
src/utils.js
33
src/utils.js
@@ -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) => {
|
||||
@@ -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.
|
||||
@@ -98,8 +80,10 @@ const getMarkdownHeadings = (content) => {
|
||||
continue
|
||||
}
|
||||
|
||||
const children = token.children ?? []
|
||||
|
||||
headings.push(
|
||||
`${token.children
|
||||
`${children
|
||||
.map((token) => {
|
||||
return token.content
|
||||
})
|
||||
@@ -112,7 +96,6 @@ const getMarkdownHeadings = (content) => {
|
||||
|
||||
module.exports = {
|
||||
filterTokens,
|
||||
addError,
|
||||
convertHeadingToHTMLFragment,
|
||||
getMarkdownHeadings,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user