2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
2019-06-06 22:21:31 -07:00
|
|
|
const { addErrorContext, allPunctuation } = require("../helpers");
|
2024-08-24 22:05:16 -07:00
|
|
|
const { matchAndGetTokensByType } = require("../helpers/micromark.cjs");
|
|
|
|
|
const { filterByTypesCached } = require("./cache");
|
2024-03-19 21:20:44 -07:00
|
|
|
|
|
|
|
|
/** @typedef {import("../helpers/micromark.cjs").TokenType} TokenType */
|
|
|
|
|
/** @type {Map<TokenType, TokenType[]>} */
|
|
|
|
|
const emphasisAndChildrenTypes = new Map([
|
|
|
|
|
[ "emphasis", [ "emphasisSequence", "emphasisText", "emphasisSequence" ] ],
|
|
|
|
|
[ "strong", [ "strongSequence", "strongText", "strongSequence" ] ]
|
|
|
|
|
]);
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2024-02-27 20:42:09 -08:00
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
|
|
|
/** @type import("./markdownlint").Rule */
|
2018-01-21 21:44:25 -08:00
|
|
|
module.exports = {
|
2023-11-09 20:05:30 -08:00
|
|
|
"names": [ "MD036", "no-emphasis-as-heading" ],
|
2018-03-19 23:39:42 +01:00
|
|
|
"description": "Emphasis used instead of a heading",
|
2023-11-09 20:05:30 -08:00
|
|
|
"tags": [ "headings", "emphasis" ],
|
2024-03-19 21:20:44 -07:00
|
|
|
"parser": "micromark",
|
2018-01-21 21:44:25 -08:00
|
|
|
"function": function MD036(params, onError) {
|
2020-01-25 18:40:39 -08:00
|
|
|
let punctuation = params.config.punctuation;
|
2024-03-19 21:20:44 -07:00
|
|
|
punctuation = String((punctuation === undefined) ? allPunctuation : punctuation);
|
|
|
|
|
const punctuationRe = new RegExp("[" + punctuation + "]$");
|
|
|
|
|
const paragraphTokens =
|
2024-08-24 22:05:16 -07:00
|
|
|
filterByTypesCached([ "paragraph" ]).
|
2024-03-19 21:20:44 -07:00
|
|
|
filter((token) =>
|
|
|
|
|
(token.parent?.type === "content") && !token.parent?.parent && (token.children.length === 1)
|
|
|
|
|
);
|
|
|
|
|
for (const paragraphToken of paragraphTokens) {
|
|
|
|
|
const childToken = paragraphToken.children[0];
|
|
|
|
|
for (const [ emphasisType, emphasisChildrenTypes ] of emphasisAndChildrenTypes) {
|
|
|
|
|
if (childToken.type === emphasisType) {
|
|
|
|
|
const matchingTokens = matchAndGetTokensByType(childToken.children, emphasisChildrenTypes);
|
|
|
|
|
if (matchingTokens) {
|
|
|
|
|
const textToken = matchingTokens[1];
|
|
|
|
|
if (
|
|
|
|
|
(textToken.children.length === 1) &&
|
|
|
|
|
(textToken.children[0].type === "data") &&
|
|
|
|
|
!punctuationRe.test(textToken.text)
|
|
|
|
|
) {
|
|
|
|
|
addErrorContext(onError, textToken.startLine, textToken.text);
|
|
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2024-03-19 21:20:44 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2022-06-08 22:10:27 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
|
};
|