2019-10-08 21:10:02 -07:00
|
|
|
// @ts-check
|
|
|
|
|
2024-11-28 20:36:44 -08:00
|
|
|
import { addErrorDetailIf } from "../helpers/helpers.cjs";
|
|
|
|
import { getDescendantsByType } from "../helpers/micromark-helpers.cjs";
|
|
|
|
import { filterByTypesCached } from "./cache.mjs";
|
2019-10-08 21:10:02 -07:00
|
|
|
|
2024-10-06 20:59:09 -07:00
|
|
|
/**
|
|
|
|
* Return the string representation of a fence markup character.
|
|
|
|
*
|
|
|
|
* @param {string} markup Fence string.
|
|
|
|
* @returns {"tilde" | "backtick"} String representation.
|
|
|
|
*/
|
|
|
|
function fencedCodeBlockStyleFor(markup) {
|
|
|
|
switch (markup[0]) {
|
|
|
|
case "~":
|
|
|
|
return "tilde";
|
|
|
|
default:
|
|
|
|
return "backtick";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-12-03 19:58:28 -08:00
|
|
|
/** @type {import("markdownlint").Rule} */
|
2024-11-28 20:36:44 -08:00
|
|
|
export default {
|
2019-10-08 21:10:02 -07:00
|
|
|
"names": [ "MD048", "code-fence-style" ],
|
|
|
|
"description": "Code fence style",
|
|
|
|
"tags": [ "code" ],
|
2024-03-09 16:17:50 -08:00
|
|
|
"parser": "micromark",
|
2019-10-08 21:10:02 -07:00
|
|
|
"function": function MD048(params, onError) {
|
2020-01-25 18:40:39 -08:00
|
|
|
const style = String(params.config.style || "consistent");
|
2019-10-08 21:10:02 -07:00
|
|
|
let expectedStyle = style;
|
2024-08-24 22:05:16 -07:00
|
|
|
const codeFenceds = filterByTypesCached([ "codeFenced" ]);
|
2024-02-19 20:42:12 -08:00
|
|
|
for (const codeFenced of codeFenceds) {
|
2024-09-16 20:50:54 -07:00
|
|
|
const codeFencedFenceSequence =
|
|
|
|
getDescendantsByType(codeFenced, [ "codeFencedFence", "codeFencedFenceSequence" ])[0];
|
|
|
|
const { startLine, text } = codeFencedFenceSequence;
|
|
|
|
if (expectedStyle === "consistent") {
|
|
|
|
expectedStyle = fencedCodeBlockStyleFor(text);
|
2022-06-08 22:10:27 -07:00
|
|
|
}
|
2024-09-16 20:50:54 -07:00
|
|
|
addErrorDetailIf(
|
|
|
|
onError,
|
|
|
|
startLine,
|
|
|
|
expectedStyle,
|
|
|
|
fencedCodeBlockStyleFor(text)
|
|
|
|
);
|
2022-06-08 22:10:27 -07:00
|
|
|
}
|
2019-10-08 21:10:02 -07:00
|
|
|
}
|
|
|
|
};
|