Skip to content

Commit 82441fa

Browse files
committed
Reimplement MD036/no-emphasis-as-heading using micromark tokens.
1 parent 6f6348a commit 82441fa

File tree

3 files changed

+66
-84
lines changed

3 files changed

+66
-84
lines changed

demo/markdownlint-browser.js

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,8 +1559,8 @@ function inHtmlFlow(token) {
15591559
* Determines a list of Micromark tokens matches and returns a subset.
15601560
*
15611561
* @param {Token[]} tokens Micromark tokens.
1562-
* @param {string[]} matchTypes Types to match.
1563-
* @param {string[]} [resultTypes] Types to return.
1562+
* @param {TokenType[]} matchTypes Types to match.
1563+
* @param {TokenType[]} [resultTypes] Types to return.
15641564
* @returns {Token[] | null} Matching tokens.
15651565
*/
15661566
function matchAndGetTokensByType(tokens, matchTypes, resultTypes) {
@@ -5315,57 +5315,48 @@ module.exports = {
53155315

53165316

53175317
const { addErrorContext, allPunctuation } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
5318+
const { filterByTypes, matchAndGetTokensByType } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
5319+
5320+
/** @typedef {import("../helpers/micromark.cjs").TokenType} TokenType */
5321+
/** @type {Map<TokenType, TokenType[]>} */
5322+
const emphasisAndChildrenTypes = new Map([
5323+
[ "emphasis", [ "emphasisSequence", "emphasisText", "emphasisSequence" ] ],
5324+
[ "strong", [ "strongSequence", "strongText", "strongSequence" ] ]
5325+
]);
53185326

53195327
// eslint-disable-next-line jsdoc/valid-types
53205328
/** @type import("./markdownlint").Rule */
53215329
module.exports = {
53225330
"names": [ "MD036", "no-emphasis-as-heading" ],
53235331
"description": "Emphasis used instead of a heading",
53245332
"tags": [ "headings", "emphasis" ],
5325-
"parser": "markdownit",
5333+
"parser": "micromark",
53265334
"function": function MD036(params, onError) {
53275335
let punctuation = params.config.punctuation;
5328-
punctuation =
5329-
String((punctuation === undefined) ? allPunctuation : punctuation);
5330-
const re = new RegExp("[" + punctuation + "]$");
5331-
// eslint-disable-next-line jsdoc/require-jsdoc
5332-
function base(token) {
5333-
if (token.type === "paragraph_open") {
5334-
return function inParagraph(t) {
5335-
// Always paragraph_open/inline/paragraph_close,
5336-
const children = t.children.filter(function notEmptyText(child) {
5337-
return (child.type !== "text") || (child.content !== "");
5338-
});
5339-
if ((children.length === 3) &&
5340-
((children[0].type === "strong_open") ||
5341-
(children[0].type === "em_open")) &&
5342-
(children[1].type === "text") &&
5343-
!re.test(children[1].content)) {
5344-
addErrorContext(onError, t.lineNumber,
5345-
children[1].content);
5346-
}
5347-
return base;
5348-
};
5349-
} else if (token.type === "blockquote_open") {
5350-
return function inBlockquote(t) {
5351-
if (t.type !== "blockquote_close") {
5352-
return inBlockquote;
5353-
}
5354-
return base;
5355-
};
5356-
} else if (token.type === "list_item_open") {
5357-
return function inListItem(t) {
5358-
if (t.type !== "list_item_close") {
5359-
return inListItem;
5336+
punctuation = String((punctuation === undefined) ? allPunctuation : punctuation);
5337+
const punctuationRe = new RegExp("[" + punctuation + "]$");
5338+
const paragraphTokens =
5339+
filterByTypes(params.parsers.micromark.tokens, [ "paragraph" ]).
5340+
filter((token) =>
5341+
(token.parent?.type === "content") && !token.parent?.parent && (token.children.length === 1)
5342+
);
5343+
for (const paragraphToken of paragraphTokens) {
5344+
const childToken = paragraphToken.children[0];
5345+
for (const [ emphasisType, emphasisChildrenTypes ] of emphasisAndChildrenTypes) {
5346+
if (childToken.type === emphasisType) {
5347+
const matchingTokens = matchAndGetTokensByType(childToken.children, emphasisChildrenTypes);
5348+
if (matchingTokens) {
5349+
const textToken = matchingTokens[1];
5350+
if (
5351+
(textToken.children.length === 1) &&
5352+
(textToken.children[0].type === "data") &&
5353+
!punctuationRe.test(textToken.text)
5354+
) {
5355+
addErrorContext(onError, textToken.startLine, textToken.text);
5356+
}
53605357
}
5361-
return base;
5362-
};
5358+
}
53635359
}
5364-
return base;
5365-
}
5366-
let state = base;
5367-
for (const token of params.parsers.markdownit.tokens) {
5368-
state = state(token);
53695360
}
53705361
}
53715362
};

helpers/micromark.cjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,8 @@ function inHtmlFlow(token) {
381381
* Determines a list of Micromark tokens matches and returns a subset.
382382
*
383383
* @param {Token[]} tokens Micromark tokens.
384-
* @param {string[]} matchTypes Types to match.
385-
* @param {string[]} [resultTypes] Types to return.
384+
* @param {TokenType[]} matchTypes Types to match.
385+
* @param {TokenType[]} [resultTypes] Types to return.
386386
* @returns {Token[] | null} Matching tokens.
387387
*/
388388
function matchAndGetTokensByType(tokens, matchTypes, resultTypes) {

lib/md036.js

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,48 @@
33
"use strict";
44

55
const { addErrorContext, allPunctuation } = require("../helpers");
6+
const { filterByTypes, matchAndGetTokensByType } = require("../helpers/micromark.cjs");
7+
8+
/** @typedef {import("../helpers/micromark.cjs").TokenType} TokenType */
9+
/** @type {Map<TokenType, TokenType[]>} */
10+
const emphasisAndChildrenTypes = new Map([
11+
[ "emphasis", [ "emphasisSequence", "emphasisText", "emphasisSequence" ] ],
12+
[ "strong", [ "strongSequence", "strongText", "strongSequence" ] ]
13+
]);
614

715
// eslint-disable-next-line jsdoc/valid-types
816
/** @type import("./markdownlint").Rule */
917
module.exports = {
1018
"names": [ "MD036", "no-emphasis-as-heading" ],
1119
"description": "Emphasis used instead of a heading",
1220
"tags": [ "headings", "emphasis" ],
13-
"parser": "markdownit",
21+
"parser": "micromark",
1422
"function": function MD036(params, onError) {
1523
let punctuation = params.config.punctuation;
16-
punctuation =
17-
String((punctuation === undefined) ? allPunctuation : punctuation);
18-
const re = new RegExp("[" + punctuation + "]$");
19-
// eslint-disable-next-line jsdoc/require-jsdoc
20-
function base(token) {
21-
if (token.type === "paragraph_open") {
22-
return function inParagraph(t) {
23-
// Always paragraph_open/inline/paragraph_close,
24-
const children = t.children.filter(function notEmptyText(child) {
25-
return (child.type !== "text") || (child.content !== "");
26-
});
27-
if ((children.length === 3) &&
28-
((children[0].type === "strong_open") ||
29-
(children[0].type === "em_open")) &&
30-
(children[1].type === "text") &&
31-
!re.test(children[1].content)) {
32-
addErrorContext(onError, t.lineNumber,
33-
children[1].content);
34-
}
35-
return base;
36-
};
37-
} else if (token.type === "blockquote_open") {
38-
return function inBlockquote(t) {
39-
if (t.type !== "blockquote_close") {
40-
return inBlockquote;
24+
punctuation = String((punctuation === undefined) ? allPunctuation : punctuation);
25+
const punctuationRe = new RegExp("[" + punctuation + "]$");
26+
const paragraphTokens =
27+
filterByTypes(params.parsers.micromark.tokens, [ "paragraph" ]).
28+
filter((token) =>
29+
(token.parent?.type === "content") && !token.parent?.parent && (token.children.length === 1)
30+
);
31+
for (const paragraphToken of paragraphTokens) {
32+
const childToken = paragraphToken.children[0];
33+
for (const [ emphasisType, emphasisChildrenTypes ] of emphasisAndChildrenTypes) {
34+
if (childToken.type === emphasisType) {
35+
const matchingTokens = matchAndGetTokensByType(childToken.children, emphasisChildrenTypes);
36+
if (matchingTokens) {
37+
const textToken = matchingTokens[1];
38+
if (
39+
(textToken.children.length === 1) &&
40+
(textToken.children[0].type === "data") &&
41+
!punctuationRe.test(textToken.text)
42+
) {
43+
addErrorContext(onError, textToken.startLine, textToken.text);
44+
}
4145
}
42-
return base;
43-
};
44-
} else if (token.type === "list_item_open") {
45-
return function inListItem(t) {
46-
if (t.type !== "list_item_close") {
47-
return inListItem;
48-
}
49-
return base;
50-
};
46+
}
5147
}
52-
return base;
53-
}
54-
let state = base;
55-
for (const token of params.parsers.markdownit.tokens) {
56-
state = state(token);
5748
}
5849
}
5950
};

0 commit comments

Comments
 (0)