Update MD044/proper-names to add html_elements parameter (fixes #435).

This commit is contained in:
David Anson 2022-04-25 21:50:33 -07:00
parent 8afec14376
commit 0f845e9ba1
15 changed files with 186 additions and 14 deletions

View file

@ -44,7 +44,8 @@ var inlineCommentStartRe =
/(<!--\s*markdownlint-(disable|enable|capture|restore|disable-file|enable-file|disable-next-line|configure-file))(?:\s|-->)/ig;
module.exports.inlineCommentStartRe = inlineCommentStartRe;
// Regular expression for matching HTML elements
module.exports.htmlElementRe = /<(([A-Za-z][A-Za-z0-9-]*)(?:\s[^>]*)?)\/?>/g;
var htmlElementRe = /<(([A-Za-z][A-Za-z0-9-]*)(?:\s[^>]*)?)\/?>/g;
module.exports.htmlElementRe = htmlElementRe;
// Regular expressions for range matching
module.exports.bareUrlRe = /(?:http|ftp)s?:\/\/[^\s\]"']*(?:\/|[^\s\]"'\W])/ig;
module.exports.listItemMarkerRe = /^([\s>]*)(?:[*+-]|\d+[.)])\s+/;
@ -598,6 +599,24 @@ module.exports.codeBlockAndSpanRanges = function (params, lineMetadata) {
});
return exclusions;
};
/**
* Returns an array of HTML element ranges.
*
* @param {Object} params RuleParams instance.
* @param {Object} lineMetadata Line metadata object.
* @returns {number[][]} Array of ranges (lineIndex, columnIndex, length).
*/
module.exports.htmlElementRanges = function (params, lineMetadata) {
var exclusions = [];
forEachLine(lineMetadata, function (line, lineIndex, inCode) {
var match = null;
// eslint-disable-next-line no-unmodified-loop-condition
while (!inCode && ((match = htmlElementRe.exec(line)) !== null)) {
exclusions.push([lineIndex, match.index, match[0].length]);
}
});
return exclusions;
};
/**
* Determines whether the specified range overlaps another range.
*
@ -1041,6 +1060,13 @@ module.exports.flattenedLists = function (value) {
}
return flattenedLists;
};
var htmlElementRanges = null;
module.exports.htmlElementRanges = function (value) {
if (value) {
htmlElementRanges = value;
}
return htmlElementRanges;
};
var lineMetadata = null;
module.exports.lineMetadata = function (value) {
if (value) {
@ -1051,6 +1077,7 @@ module.exports.lineMetadata = function (value) {
module.exports.clear = function () {
codeBlockAndSpanRanges = null;
flattenedLists = null;
htmlElementRanges = null;
lineMetadata = null;
};
@ -1547,9 +1574,11 @@ function lintContent(ruleList, name, content, md, config, frontMatter, handleRul
lines: lines,
frontMatterLines: frontMatterLines
});
cache.lineMetadata(helpers.getLineMetadata(paramsBase));
var lineMetadata = helpers.getLineMetadata(paramsBase);
cache.lineMetadata(lineMetadata);
cache.codeBlockAndSpanRanges(helpers.codeBlockAndSpanRanges(paramsBase, lineMetadata));
cache.htmlElementRanges(helpers.htmlElementRanges(paramsBase, lineMetadata));
cache.flattenedLists(helpers.flattenLists(paramsBase.tokens));
cache.codeBlockAndSpanRanges(helpers.codeBlockAndSpanRanges(paramsBase, cache.lineMetadata()));
// Function to run for each rule
var results = [];
// eslint-disable-next-line jsdoc/require-jsdoc
@ -4157,7 +4186,7 @@ module.exports = {
// @ts-check
var _a = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"), addErrorDetailIf = _a.addErrorDetailIf, bareUrlRe = _a.bareUrlRe, escapeForRegExp = _a.escapeForRegExp, forEachLine = _a.forEachLine, forEachLink = _a.forEachLink, overlapsAnyRange = _a.overlapsAnyRange, linkReferenceRe = _a.linkReferenceRe;
var _b = __webpack_require__(/*! ./cache */ "../lib/cache.js"), codeBlockAndSpanRanges = _b.codeBlockAndSpanRanges, lineMetadata = _b.lineMetadata;
var _b = __webpack_require__(/*! ./cache */ "../lib/cache.js"), codeBlockAndSpanRanges = _b.codeBlockAndSpanRanges, htmlElementRanges = _b.htmlElementRanges, lineMetadata = _b.lineMetadata;
module.exports = {
"names": ["MD044", "proper-names"],
"description": "Proper names should have the correct capitalization",
@ -4168,6 +4197,8 @@ module.exports = {
names.sort(function (a, b) { return (b.length - a.length) || a.localeCompare(b); });
var codeBlocks = params.config.code_blocks;
var includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks;
var htmlElements = params.config.html_elements;
var includeHtmlElements = (htmlElements === undefined) ? true : !!htmlElements;
var exclusions = [];
forEachLine(lineMetadata(), function (line, lineIndex) {
if (linkReferenceRe.test(line)) {
@ -4188,6 +4219,9 @@ module.exports = {
if (!includeCodeBlocks) {
exclusions.push.apply(exclusions, codeBlockAndSpanRanges());
}
if (!includeHtmlElements) {
exclusions.push.apply(exclusions, htmlElementRanges());
}
var _loop_1 = function (name) {
var escapedName = escapeForRegExp(name);
var startNamePattern = /^\W/.test(name) ? "" : "\\b_*";

View file

@ -1752,7 +1752,7 @@ Tags: spelling
Aliases: proper-names
Parameters: names, code_blocks (string array; default `null`, boolean; default `true`)
Parameters: names, code_blocks, html_elements (string array; default `null`, boolean; default `true`, boolean; default `true`)
Fixable: Most violations can be fixed by tooling
@ -1771,7 +1771,9 @@ the proper capitalization, specify the desired letter case in the `names` array:
```
Set the `code_blocks` parameter to `false` to disable this rule for code blocks
and spans.
and spans. Set the `html_elements` parameter to `false` to disable this rule
for HTML elements and attributes (such as when using a proper name as part of
a path for `a`/`href` or `img`/`src`).
Rationale: Incorrect capitalization of proper names is usually a mistake.

View file

@ -19,7 +19,8 @@ const inlineCommentStartRe =
module.exports.inlineCommentStartRe = inlineCommentStartRe;
// Regular expression for matching HTML elements
module.exports.htmlElementRe = /<(([A-Za-z][A-Za-z0-9-]*)(?:\s[^>]*)?)\/?>/g;
const htmlElementRe = /<(([A-Za-z][A-Za-z0-9-]*)(?:\s[^>]*)?)\/?>/g;
module.exports.htmlElementRe = htmlElementRe;
// Regular expressions for range matching
module.exports.bareUrlRe = /(?:http|ftp)s?:\/\/[^\s\]"']*(?:\/|[^\s\]"'\W])/ig;
@ -603,6 +604,25 @@ module.exports.codeBlockAndSpanRanges = (params, lineMetadata) => {
return exclusions;
};
/**
* Returns an array of HTML element ranges.
*
* @param {Object} params RuleParams instance.
* @param {Object} lineMetadata Line metadata object.
* @returns {number[][]} Array of ranges (lineIndex, columnIndex, length).
*/
module.exports.htmlElementRanges = (params, lineMetadata) => {
const exclusions = [];
forEachLine(lineMetadata, (line, lineIndex, inCode) => {
let match = null;
// eslint-disable-next-line no-unmodified-loop-condition
while (!inCode && ((match = htmlElementRe.exec(line)) !== null)) {
exclusions.push([ lineIndex, match.index, match[0].length ]);
}
});
return exclusions;
};
/**
* Determines whether the specified range overlaps another range.
*

View file

@ -18,6 +18,14 @@ module.exports.flattenedLists = (value) => {
return flattenedLists;
};
let htmlElementRanges = null;
module.exports.htmlElementRanges = (value) => {
if (value) {
htmlElementRanges = value;
}
return htmlElementRanges;
};
let lineMetadata = null;
module.exports.lineMetadata = (value) => {
if (value) {
@ -29,5 +37,6 @@ module.exports.lineMetadata = (value) => {
module.exports.clear = () => {
codeBlockAndSpanRanges = null;
flattenedLists = null;
htmlElementRanges = null;
lineMetadata = null;
};

View file

@ -495,11 +495,15 @@ function lintContent(
lines,
frontMatterLines
});
cache.lineMetadata(helpers.getLineMetadata(paramsBase));
cache.flattenedLists(helpers.flattenLists(paramsBase.tokens));
const lineMetadata = helpers.getLineMetadata(paramsBase);
cache.lineMetadata(lineMetadata);
cache.codeBlockAndSpanRanges(
helpers.codeBlockAndSpanRanges(paramsBase, cache.lineMetadata())
helpers.codeBlockAndSpanRanges(paramsBase, lineMetadata)
);
cache.htmlElementRanges(
helpers.htmlElementRanges(paramsBase, lineMetadata)
);
cache.flattenedLists(helpers.flattenLists(paramsBase.tokens));
// Function to run for each rule
let results = [];
// eslint-disable-next-line jsdoc/require-jsdoc

View file

@ -4,7 +4,8 @@
const { addErrorDetailIf, bareUrlRe, escapeForRegExp, forEachLine,
forEachLink, overlapsAnyRange, linkReferenceRe } = require("../helpers");
const { codeBlockAndSpanRanges, lineMetadata } = require("./cache");
const { codeBlockAndSpanRanges, htmlElementRanges, lineMetadata } =
require("./cache");
module.exports = {
"names": [ "MD044", "proper-names" ],
@ -15,7 +16,11 @@ module.exports = {
names = Array.isArray(names) ? names : [];
names.sort((a, b) => (b.length - a.length) || a.localeCompare(b));
const codeBlocks = params.config.code_blocks;
const includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks;
const includeCodeBlocks =
(codeBlocks === undefined) ? true : !!codeBlocks;
const htmlElements = params.config.html_elements;
const includeHtmlElements =
(htmlElements === undefined) ? true : !!htmlElements;
const exclusions = [];
forEachLine(lineMetadata(), (line, lineIndex) => {
if (linkReferenceRe.test(line)) {
@ -37,6 +42,9 @@ module.exports = {
if (!includeCodeBlocks) {
exclusions.push(...codeBlockAndSpanRanges());
}
if (!includeHtmlElements) {
exclusions.push(...htmlElementRanges());
}
for (const name of names) {
const escapedName = escapeForRegExp(name);
const startNamePattern = /^\W/.test(name) ? "" : "\\b_*";

View file

@ -231,7 +231,9 @@
// List of proper names
"names": [],
// Include code blocks
"code_blocks": true
"code_blocks": true,
// Include HTML elements
"html_elements": true
},
// MD045/no-alt-text - Images should have alternate text (alt text)

View file

@ -211,6 +211,8 @@ MD044:
names: []
# Include code blocks
code_blocks: true
# Include HTML elements
html_elements: true
# MD045/no-alt-text - Images should have alternate text (alt text)
MD045: true

View file

@ -391,6 +391,11 @@ rules.forEach(function forRule(rule) {
"description": "Include code blocks",
"type": "boolean",
"default": true
},
"html_elements": {
"description": "Include HTML elements",
"type": "boolean",
"default": true
}
};
break;

View file

@ -759,6 +759,11 @@
"description": "Include code blocks",
"type": "boolean",
"default": true
},
"html_elements": {
"description": "Include HTML elements",
"type": "boolean",
"default": true
}
},
"additionalProperties": false

View file

@ -1283,3 +1283,43 @@ test("forEachLink", (t) => {
t.is(matches.length, 0, "Missing match");
}
});
test("htmlElementRanges", (t) => {
t.plan(1);
const params = {
"lines": [
"# Heading",
"",
"Text text text",
"text <a id='id'/> text",
"text text text",
"",
"<p>",
"Text <em>text</em> text",
"</p>",
"",
"```",
"<br/>",
"```",
"",
"Text `<br/>` text",
"text <br/> text"
],
"tokens": [
{
"type": "code_block",
"map": [ 10, 12 ]
}
]
};
const expected = [
[ 3, 5, 12 ],
[ 6, 0, 3 ],
[ 7, 5, 4 ],
[ 14, 6, 5 ],
[ 15, 5, 5 ]
];
const lineMetadata = helpers.getLineMetadata(params);
const actual = helpers.htmlElementRanges(params, lineMetadata);
t.deepEqual(actual, expected);
});

View file

@ -0,0 +1,10 @@
{
"default": true,
"MD033": false,
"MD044": {
"names": [
"JavaScript"
],
"html_elements": false
}
}

View file

@ -0,0 +1,21 @@
# Proper Names No HTML
Okay text JavaScript.
Bad text javascript. {MD044}
Bad code `javascript`. {MD044}
<img src="img/javascript/image.png">
<script type="text/javascript">
javascript {MD044}
</script>
<a id="javascript">
<a id="javascript"/>
<javascript/>
<code>javascript</code>

View file

@ -1,5 +1,6 @@
{
"default": true,
"MD033": false,
"MD044": {
"names": [
"markdownlint",

View file

@ -40,7 +40,7 @@ Code in `javascript` {MD044}
Execute `via the node.js engine` {MD044}
HTML <u>javascript</u> {MD033} {MD044}
HTML <u>javascript</u> {MD044}
* Use NPM {MD044}
@ -84,3 +84,12 @@ Text referencing multiplecase name.
Text referencing MultipleCase name.
Text referencing MULTIPLECASE name. {MD044}
Text referencing mULTIPLEcASE name.
<img src="img/javascript/image.png" error="{MD044}">
<script type="text/javascript">
{MD044:90}
javascript {MD044}
</script>
<a error="{MD044}" id="javascript"/>