chore: dependency vendoring of markdownlint-rule-helpers

This commit is contained in:
2024-01-12 01:02:59 +01:00
parent 92f35daeaf
commit 64396954e4
7 changed files with 96 additions and 261 deletions

View File

@ -3,8 +3,8 @@
const { pathToFileURL } = require("node:url")
const fs = require("node:fs")
const { filterTokens } = require("./markdownlint-rule-helpers/helpers.js")
const {
filterTokens,
convertHeadingToHTMLFragment,
getMarkdownHeadings,
getMarkdownIdOrAnchorNameFragments,

View File

@ -0,0 +1,38 @@
/**
* Dependency Vendoring of `markdownlint-rule-helpers`
* @see https://www.npmjs.com/package/markdownlint-rule-helpers
*/
/** @typedef {import('markdownlint').RuleParams} MarkdownLintRuleParams */
/** @typedef {import('markdownlint').MarkdownItToken} MarkdownItToken */
/**
* Calls the provided function for each matching token.
*
* @param {MarkdownLintRuleParams} params RuleParams instance.
* @param {string} type Token type identifier.
* @param {(token: MarkdownItToken) => void} handler Callback function.
* @returns {void}
*/
const filterTokens = (params, type, handler) => {
for (const token of params.tokens) {
if (token.type === type) {
handler(token)
}
}
}
/**
* Gets a Regular Expression for matching the specified HTML attribute.
*
* @param {string} name HTML attribute name.
* @returns {RegExp} Regular Expression for matching.
*/
const getHtmlAttributeRe = (name) => {
return new RegExp(`\\s${name}\\s*=\\s*['"]?([^'"\\s>]*)`, "iu")
}
module.exports = {
filterTokens,
getHtmlAttributeRe,
}

View File

@ -1,25 +1,6 @@
const MarkdownIt = require("markdown-it")
// @ts-ignore
const { getHtmlAttributeRe } = require("markdownlint-rule-helpers")
/** @typedef {import('markdownlint').RuleParams} MarkdownLintRuleParams */
/** @typedef {import('markdownlint').MarkdownItToken} MarkdownItToken */
/**
* Calls the provided function for each matching token.
*
* @param {MarkdownLintRuleParams} params RuleParams instance.
* @param {string} type Token type identifier.
* @param {(token: MarkdownItToken) => void} handler Callback function.
* @returns {void}
*/
const filterTokens = (params, type, handler) => {
for (const token of params.tokens) {
if (token.type === type) {
handler(token)
}
}
}
const { getHtmlAttributeRe } = require("./markdownlint-rule-helpers/helpers.js")
/**
* Converts a Markdown heading into an HTML fragment according to the rules
@ -114,12 +95,12 @@ const getMarkdownIdOrAnchorNameFragments = (content) => {
for (const token of tokens) {
const anchorMatch =
anchorIdRe.exec(token.content) || anchorNameRe.exec(token.content)
if (!anchorMatch || anchorMatch.length === 0) {
if (anchorMatch == null) {
continue
}
const anchorIdOrName = anchorMatch[1]
if (anchorIdOrName.length <= 0) {
if (anchorIdOrName == null || anchorIdOrName.length <= 0) {
continue
}
@ -133,7 +114,6 @@ const getMarkdownIdOrAnchorNameFragments = (content) => {
}
module.exports = {
filterTokens,
convertHeadingToHTMLFragment,
getMarkdownHeadings,
getMarkdownIdOrAnchorNameFragments,