From 608f875fc29f4ea9b6bded03f857d116ef065c42 Mon Sep 17 00:00:00 2001 From: David Anson Date: Wed, 8 May 2019 22:06:17 -0700 Subject: [PATCH] Update MD033/no-inline-html to better handle inline code spans (fixes #182). --- lib/md033.js | 5 +++-- test/detailed-results-html-tags.md | 20 +++++++++++++++++--- test/detailed-results-html-tags.results.json | 19 ++++++++++++++----- test/inline_html-allowed_elements.md | 12 ++++++++++++ 4 files changed, 46 insertions(+), 10 deletions(-) diff --git a/lib/md033.js b/lib/md033.js index a50614c30..331c70a38 100644 --- a/lib/md033.js +++ b/lib/md033.js @@ -6,8 +6,9 @@ const { addError, bareUrlRe, forEachLine, unescapeMarkdown } = require("../helpers"); const { lineMetadata } = require("./cache"); -const htmlElementRe = /<(([\w+.-]+)(?:[^>]*)?)>/g; +const htmlElementRe = /<(([\w-]+)(?:[\s/][^>]*)?)>/g; const linkDestinationRe = /]\(\s*$/; +const inlineCodeRe = /^[^`]*(`+[^`]+`+[^`]+)*`+[^`]*$/; // See https://spec.commonmark.org/0.29/#autolinks const emailAddressRe = // eslint-disable-next-line max-len @@ -29,7 +30,7 @@ module.exports = { !tag.endsWith("\\>") && !bareUrlRe.test(content) && !emailAddressRe.test(content)) { const prefix = line.substring(0, match.index); - if (!linkDestinationRe.test(prefix) && + if (!linkDestinationRe.test(prefix) && !inlineCodeRe.test(prefix) && !unescapeMarkdown(prefix + "<", "_").endsWith("_")) { addError(onError, lineIndex + 1, "Element: " + element, null, [ match.index + 1, tag.length ]); diff --git a/test/detailed-results-html-tags.md b/test/detailed-results-html-tags.md index a3ffcdab0..86159a0ba 100644 --- a/test/detailed-results-html-tags.md +++ b/test/detailed-results-html-tags.md @@ -75,6 +75,18 @@ text inline inline text Text +`` + +Text ```` text + +Text `` text ```` text `````` text + +Text `` text inline text + +Text ``text text`` text + +Text + Text inline text text text @@ -82,10 +94,12 @@ Text is an email autolink. -Text - Another email autolink: . Text -But is not an email autolink. + is an HTML element. + +But is not an autolink or HTML element. + +Text diff --git a/test/detailed-results-html-tags.results.json b/test/detailed-results-html-tags.results.json index 2968521fc..bef477973 100644 --- a/test/detailed-results-html-tags.results.json +++ b/test/detailed-results-html-tags.results.json @@ -135,7 +135,16 @@ "errorRange": [ 8, 4 ] }, { - "lineNumber": 78, + "lineNumber": 84, + "ruleNames": [ "MD033", "no-inline-html" ], + "ruleDescription": "Inline HTML", + "ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md033", + "errorDetail": "Element: em", + "errorContext": null, + "errorRange": [ 18, 4 ] + }, + { + "lineNumber": 90, "ruleNames": [ "MD033", "no-inline-html" ], "ruleDescription": "Inline HTML", "ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md033", @@ -144,7 +153,7 @@ "errorRange": [ 6, 18 ] }, { - "lineNumber": 79, + "lineNumber": 91, "ruleNames": [ "MD033", "no-inline-html" ], "ruleDescription": "Inline HTML", "ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md033", @@ -153,12 +162,12 @@ "errorRange": [ 6, 20 ] }, { - "lineNumber": 91, + "lineNumber": 101, "ruleNames": [ "MD033", "no-inline-html" ], "ruleDescription": "Inline HTML", "ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md033", - "errorDetail": "Element: foo.bar.baz", + "errorDetail": "Element: foo-bar-baz", "errorContext": null, - "errorRange": [ 5, 13 ] + "errorRange": [ 1, 13 ] } ] diff --git a/test/inline_html-allowed_elements.md b/test/inline_html-allowed_elements.md index e243c7a0d..defaf1e39 100644 --- a/test/inline_html-allowed_elements.md +++ b/test/inline_html-allowed_elements.md @@ -14,8 +14,20 @@
{MD033} +
{MD033} +
{MD033} +
{MD033} + +
{MD033} + +
{MD033} + +
{MD033} + +
{MD033} +

This is allowed.