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");
|
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" ],
|
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;
|
|
|
|
punctuation =
|
|
|
|
String((punctuation === undefined) ? allPunctuation : punctuation);
|
2018-04-27 22:05:34 -07:00
|
|
|
const re = new RegExp("[" + punctuation + "]$");
|
2020-01-23 19:42:46 -08:00
|
|
|
// eslint-disable-next-line jsdoc/require-jsdoc
|
2018-01-21 21:44:25 -08:00
|
|
|
function base(token) {
|
|
|
|
if (token.type === "paragraph_open") {
|
|
|
|
return function inParagraph(t) {
|
|
|
|
// Always paragraph_open/inline/paragraph_close,
|
2018-04-27 22:05:34 -07:00
|
|
|
const children = t.children.filter(function notEmptyText(child) {
|
2018-01-21 21:44:25 -08:00
|
|
|
return (child.type !== "text") || (child.content !== "");
|
|
|
|
});
|
|
|
|
if ((children.length === 3) &&
|
|
|
|
((children[0].type === "strong_open") ||
|
|
|
|
(children[0].type === "em_open")) &&
|
|
|
|
(children[1].type === "text") &&
|
|
|
|
!re.test(children[1].content)) {
|
2019-04-13 11:18:57 -07:00
|
|
|
addErrorContext(onError, t.lineNumber,
|
2018-01-21 21:44:25 -08:00
|
|
|
children[1].content);
|
|
|
|
}
|
|
|
|
return base;
|
|
|
|
};
|
|
|
|
} else if (token.type === "blockquote_open") {
|
|
|
|
return function inBlockquote(t) {
|
|
|
|
if (t.type !== "blockquote_close") {
|
|
|
|
return inBlockquote;
|
|
|
|
}
|
|
|
|
return base;
|
|
|
|
};
|
|
|
|
} else if (token.type === "list_item_open") {
|
|
|
|
return function inListItem(t) {
|
|
|
|
if (t.type !== "list_item_close") {
|
|
|
|
return inListItem;
|
|
|
|
}
|
|
|
|
return base;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return base;
|
|
|
|
}
|
2018-04-27 22:05:34 -07:00
|
|
|
let state = base;
|
2023-02-18 21:41:07 -08:00
|
|
|
for (const token of params.parsers.markdownit.tokens) {
|
2018-01-21 21:44:25 -08:00
|
|
|
state = state(token);
|
2022-06-08 22:10:27 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
};
|