mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 14:00:13 +01:00
Reimplement MD033/no-inline-html using micromark tokens.
This commit is contained in:
parent
08b31da0aa
commit
48a92d41a4
6 changed files with 171 additions and 200 deletions
|
|
@ -193,19 +193,6 @@ module.exports.escapeForRegExp = function escapeForRegExp(str) {
|
|||
return str.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
|
||||
};
|
||||
|
||||
// Un-escapes Markdown content (simple algorithm; not a parser)
|
||||
const escapedMarkdownRe = /\\./g;
|
||||
module.exports.unescapeMarkdown =
|
||||
function unescapeMarkdown(markdown, replacement) {
|
||||
return markdown.replace(escapedMarkdownRe, (match) => {
|
||||
const char = match[1];
|
||||
if ("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~".includes(char)) {
|
||||
return replacement || char;
|
||||
}
|
||||
return match;
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Return the string representation of a fence markup character.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -24,14 +24,14 @@ const { parse, postprocess, preprocess } = require("../micromark/micromark.cjs")
|
|||
* Parses a Markdown document and returns (frozen) tokens.
|
||||
*
|
||||
* @param {string} markdown Markdown document.
|
||||
* @returns {Token[]} Markdown tokens (frozen).
|
||||
* @param {Object} [options] Options for micromark.
|
||||
* @returns {Token[]} Micromark tokens (frozen).
|
||||
*/
|
||||
function micromarkParse(markdown) {
|
||||
function micromarkParse(markdown, options) {
|
||||
|
||||
// Use micromark to parse document into Events
|
||||
const encoding = undefined;
|
||||
const eol = true;
|
||||
const options = undefined;
|
||||
const chunks = preprocess()(markdown, encoding, eol);
|
||||
const parseContext = parse(options).document().write(chunks);
|
||||
const events = postprocess(parseContext);
|
||||
|
|
@ -79,6 +79,39 @@ function micromarkParse(markdown) {
|
|||
return document;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter a list of Micromark tokens by predicate.
|
||||
*
|
||||
* @param {Token[]} tokens Micromark tokens.
|
||||
* @param {Function} predicate Filter predicate.
|
||||
* @returns {Token[]} Filtered tokens.
|
||||
*/
|
||||
function filterByPredicate(tokens, predicate) {
|
||||
const result = [];
|
||||
const pending = [ ...tokens ];
|
||||
let token = null;
|
||||
while ((token = pending.shift())) {
|
||||
if (predicate(token)) {
|
||||
result.push(token);
|
||||
}
|
||||
pending.unshift(...token.tokens);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter a list of Micromark tokens by type.
|
||||
*
|
||||
* @param {Token[]} tokens Micromark tokens.
|
||||
* @param {string[]} types Types to allow.
|
||||
* @returns {Token[]} Filtered tokens.
|
||||
*/
|
||||
function filterByTypes(tokens, ...types) {
|
||||
return filterByPredicate(tokens, (token) => types.includes(token.type));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
filterByPredicate,
|
||||
filterByTypes,
|
||||
"parse": micromarkParse
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue