mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Refactor RegExps to avoid the possibility of polynomial backtracking (fixes #657).
This commit is contained in:
parent
9b1840a5a4
commit
e0219411c6
8 changed files with 14 additions and 16 deletions
|
@ -525,7 +525,7 @@ specify a custom `RegExp` or use the value `null` to disable the feature.
|
|||
The default value:
|
||||
|
||||
```javascript
|
||||
/((^---\s*$[\s\S]+?^---\s*)|(^\+\+\+\s*$[\s\S]+?^(\+\+\+|\.\.\.)\s*)|(^\{\s*$[\s\S]+?^\}\s*))(\r\n|\r|\n|$)/m
|
||||
/((^---[^\S\r\n\u2028\u2029]*$[\s\S]+?^---\s*)|(^\+\+\+[^\S\r\n\u2028\u2029]*$[\s\S]+?^(\+\+\+|\.\.\.)\s*)|(^\{[^\S\r\n\u2028\u2029]*$[\s\S]+?^\}\s*))(\r\n|\r|\n|$)/m
|
||||
```
|
||||
|
||||
Ignores [YAML](https://en.wikipedia.org/wiki/YAML),
|
||||
|
|
|
@ -36,7 +36,7 @@ module.exports.nextLinesRe = nextLinesRe;
|
|||
|
||||
// Regular expression for matching common front matter (YAML and TOML)
|
||||
module.exports.frontMatterRe =
|
||||
/((^---\s*$[\s\S]+?^---\s*)|(^\+\+\+\s*$[\s\S]+?^(\+\+\+|\.\.\.)\s*)|(^\{\s*$[\s\S]+?^\}\s*))(\r\n|\r|\n|$)/m;
|
||||
/((^---[^\S\r\n\u2028\u2029]*$[\s\S]+?^---\s*)|(^\+\+\+[^\S\r\n\u2028\u2029]*$[\s\S]+?^(\+\+\+|\.\.\.)\s*)|(^\{[^\S\r\n\u2028\u2029]*$[\s\S]+?^\}\s*))(\r\n|\r|\n|$)/m;
|
||||
|
||||
// Regular expression for matching the start of inline disable/enable comments
|
||||
const inlineCommentStartRe =
|
||||
|
@ -1554,7 +1554,7 @@ function freezeToken(token) {
|
|||
/**
|
||||
* Annotate tokens with line/lineNumber and freeze them.
|
||||
*
|
||||
* @param {import("markdown-it").Token[]} tokens Array of markdown-it tokens.
|
||||
* @param {Object[]} tokens Array of markdown-it tokens.
|
||||
* @param {string[]} lines Lines of Markdown content.
|
||||
* @returns {void}
|
||||
*/
|
||||
|
@ -1613,18 +1613,18 @@ function annotateAndFreezeTokens(tokens, lines) {
|
|||
* @param {import("./markdownlint").Plugin[]} markdownItPlugins Additional plugins.
|
||||
* @param {string} content Markdown content.
|
||||
* @param {string[]} lines Lines of Markdown content.
|
||||
* @returns {import("markdown-it").Token[]} Array of markdown-it tokens.
|
||||
* @returns {import("../lib/markdownlint").MarkdownItToken} Array of markdown-it tokens.
|
||||
*/
|
||||
function getMarkdownItTokens(markdownItPlugins, content, lines) {
|
||||
const markdownit = __webpack_require__(/*! markdown-it */ "markdown-it");
|
||||
const md = markdownit({ "html": true });
|
||||
// const markdownItPlugins = options.markdownItPlugins || [];
|
||||
for (const plugin of markdownItPlugins) {
|
||||
// @ts-ignore
|
||||
md.use(...plugin);
|
||||
}
|
||||
const tokens = md.parse(content, {});
|
||||
annotateAndFreezeTokens(tokens, lines);
|
||||
// @ts-ignore
|
||||
return tokens;
|
||||
};
|
||||
|
||||
|
@ -4192,7 +4192,7 @@ module.exports = {
|
|||
for (const [ lineIndex, line ] of lines.entries()) {
|
||||
if (!ignoreBlockLineNumbers.has(lineIndex + 1)) {
|
||||
const match =
|
||||
/^(#+)([ \t]*)([^#]*?[^#\\])([ \t]*)((?:\\#)?)(#+)(\s*)$/.exec(line);
|
||||
/^(#+)([ \t]*)([^# \t\\]|[^# \t][^#]*?[^# \t\\])([ \t]*)((?:\\#)?)(#+)(\s*)$/.exec(line);
|
||||
if (match) {
|
||||
const [
|
||||
,
|
||||
|
|
|
@ -75,7 +75,6 @@ export default [
|
|||
"prefer-destructuring": "off",
|
||||
"prefer-named-capture-group": "off",
|
||||
"prefer-template": "off",
|
||||
"regexp/no-super-linear-backtracking": "off",
|
||||
"require-unicode-regexp": "off",
|
||||
"sort-imports": "off",
|
||||
"sort-keys": "off",
|
||||
|
|
|
@ -24,7 +24,7 @@ module.exports.nextLinesRe = nextLinesRe;
|
|||
|
||||
// Regular expression for matching common front matter (YAML and TOML)
|
||||
module.exports.frontMatterRe =
|
||||
/((^---\s*$[\s\S]+?^---\s*)|(^\+\+\+\s*$[\s\S]+?^(\+\+\+|\.\.\.)\s*)|(^\{\s*$[\s\S]+?^\}\s*))(\r\n|\r|\n|$)/m;
|
||||
/((^---[^\S\r\n\u2028\u2029]*$[\s\S]+?^---\s*)|(^\+\+\+[^\S\r\n\u2028\u2029]*$[\s\S]+?^(\+\+\+|\.\.\.)\s*)|(^\{[^\S\r\n\u2028\u2029]*$[\s\S]+?^\}\s*))(\r\n|\r|\n|$)/m;
|
||||
|
||||
// Regular expression for matching the start of inline disable/enable comments
|
||||
const inlineCommentStartRe =
|
||||
|
|
|
@ -92,7 +92,7 @@ function freezeToken(token) {
|
|||
/**
|
||||
* Annotate tokens with line/lineNumber and freeze them.
|
||||
*
|
||||
* @param {import("markdown-it").Token[]} tokens Array of markdown-it tokens.
|
||||
* @param {Object[]} tokens Array of markdown-it tokens.
|
||||
* @param {string[]} lines Lines of Markdown content.
|
||||
* @returns {void}
|
||||
*/
|
||||
|
@ -151,18 +151,18 @@ function annotateAndFreezeTokens(tokens, lines) {
|
|||
* @param {import("./markdownlint").Plugin[]} markdownItPlugins Additional plugins.
|
||||
* @param {string} content Markdown content.
|
||||
* @param {string[]} lines Lines of Markdown content.
|
||||
* @returns {import("markdown-it").Token[]} Array of markdown-it tokens.
|
||||
* @returns {import("../lib/markdownlint").MarkdownItToken} Array of markdown-it tokens.
|
||||
*/
|
||||
function getMarkdownItTokens(markdownItPlugins, content, lines) {
|
||||
const markdownit = require("markdown-it");
|
||||
const md = markdownit({ "html": true });
|
||||
// const markdownItPlugins = options.markdownItPlugins || [];
|
||||
for (const plugin of markdownItPlugins) {
|
||||
// @ts-ignore
|
||||
md.use(...plugin);
|
||||
}
|
||||
const tokens = md.parse(content, {});
|
||||
annotateAndFreezeTokens(tokens, lines);
|
||||
// @ts-ignore
|
||||
return tokens;
|
||||
};
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ module.exports = {
|
|||
for (const [ lineIndex, line ] of lines.entries()) {
|
||||
if (!ignoreBlockLineNumbers.has(lineIndex + 1)) {
|
||||
const match =
|
||||
/^(#+)([ \t]*)([^#]*?[^#\\])([ \t]*)((?:\\#)?)(#+)(\s*)$/.exec(line);
|
||||
/^(#+)([ \t]*)([^# \t\\]|[^# \t][^#]*?[^# \t\\])([ \t]*)((?:\\#)?)(#+)(\s*)$/.exec(line);
|
||||
if (match) {
|
||||
const [
|
||||
,
|
||||
|
|
|
@ -1034,8 +1034,7 @@ test("validateConfigExampleJson", (t) => {
|
|||
const ajv = new Ajv(ajvOptions);
|
||||
const validateSchema = ajv.compile(configSchema);
|
||||
t.is(
|
||||
// eslint-disable-next-line regexp/optimal-quantifier-concatenation
|
||||
configSchema.$id.replace(/^.*v(?<ver>\d+\.\d+\.\d+).*$/u, "$<ver>"),
|
||||
configSchema.$id.replace(/^.*\/v(?<ver>\d+\.\d+\.\d+)\/.*$/u, "$<ver>"),
|
||||
version
|
||||
);
|
||||
t.is(configSchema.$id, configSchema.properties.$schema.default);
|
||||
|
@ -1081,7 +1080,7 @@ test("allBuiltInRulesHaveValidUrl", (t) => {
|
|||
});
|
||||
|
||||
test("someCustomRulesHaveValidUrl", (t) => {
|
||||
t.plan(8);
|
||||
t.plan(9);
|
||||
for (const rule of customRules.all) {
|
||||
t.true(!rule.information ||
|
||||
(Object.getPrototypeOf(rule.information) === URL.prototype));
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
"use strict";
|
||||
|
||||
const anyBlockquote = require("./any-blockquote");
|
||||
module.exports.anyBlockquote = anyBlockquote;
|
||||
module.exports.anyBlockquote = anyBlockquote[1];
|
||||
|
||||
const everyNLines = require("./every-n-lines");
|
||||
module.exports.everyNLines = everyNLines;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue