Rename helpers.bareUrlRe to urlRe, improve it a little, add tests

This commit is contained in:
David Anson 2022-12-15 13:54:54 -08:00
parent 6e38259a4a
commit 2e2937081e
5 changed files with 103 additions and 16 deletions

View file

@ -47,8 +47,9 @@ module.exports.inlineCommentStartRe = inlineCommentStartRe;
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;
module.exports.urlRe =
// eslint-disable-next-line max-len
/(?:http|ftp)s?:\/\/(?:[^\s()<>\]"'`]|\([^\s<>\]"'`]*\))*\b(?:[-#/]|\([^\s<>\]"'`]*\))*/ig;
module.exports.listItemMarkerRe = /^([\s>]*)(?:[*+-]|\d+[.)])\s+/;
module.exports.orderedListItemMarkerRe = /^[\s>]*0*(\d+)[.)]/;
// Regular expression for all instances of emphasis markers
@ -3755,7 +3756,7 @@ module.exports = {
"use strict";
// @ts-check
const { addErrorContext, bareUrlRe, withinAnyRange } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { addErrorContext, urlRe, withinAnyRange } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { codeBlockAndSpanRanges, htmlElementRanges, referenceLinkImageData } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
const htmlLinkRe = /<a(?:|\s[^>]+)>[^<>]*<\/a\s*>/ig;
module.exports = {
@ -3779,7 +3780,7 @@ module.exports = {
while ((match = htmlLinkRe.exec(line)) !== null) {
lineExclusions.push([lineIndex, match.index, match[0].length]);
}
while ((match = bareUrlRe.exec(line)) !== null) {
while ((match = urlRe.exec(line)) !== null) {
const [bareUrl] = match;
const matchIndex = match.index;
const bareUrlLength = bareUrl.length;
@ -4436,7 +4437,7 @@ module.exports = {
"use strict";
// @ts-check
const { addErrorDetailIf, bareUrlRe, escapeForRegExp, forEachLine, forEachLink, withinAnyRange, linkReferenceDefinitionRe } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { addErrorDetailIf, escapeForRegExp, forEachLine, forEachLink, linkReferenceDefinitionRe, urlRe, withinAnyRange } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { codeBlockAndSpanRanges, htmlElementRanges, lineMetadata } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
module.exports = {
"names": ["MD044", "proper-names"],
@ -4457,7 +4458,7 @@ module.exports = {
}
else {
let match = null;
while ((match = bareUrlRe.exec(line)) !== null) {
while ((match = urlRe.exec(line)) !== null) {
exclusions.push([lineIndex, match.index, match[0].length]);
}
forEachLink(line, (index, _, text, destination) => {

View file

@ -23,8 +23,9 @@ 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;
module.exports.urlRe =
// eslint-disable-next-line max-len
/(?:http|ftp)s?:\/\/(?:[^\s()<>\]"'`]|\([^\s<>\]"'`]*\))*\b(?:[-#/]|\([^\s<>\]"'`]*\))*/ig;
module.exports.listItemMarkerRe = /^([\s>]*)(?:[*+-]|\d+[.)])\s+/;
module.exports.orderedListItemMarkerRe = /^[\s>]*0*(\d+)[.)]/;

View file

@ -2,7 +2,7 @@
"use strict";
const { addErrorContext, bareUrlRe, withinAnyRange } = require("../helpers");
const { addErrorContext, urlRe, withinAnyRange } = require("../helpers");
const { codeBlockAndSpanRanges, htmlElementRanges, referenceLinkImageData } =
require("./cache");
@ -28,7 +28,7 @@ module.exports = {
while ((match = htmlLinkRe.exec(line)) !== null) {
lineExclusions.push([ lineIndex, match.index, match[0].length ]);
}
while ((match = bareUrlRe.exec(line)) !== null) {
while ((match = urlRe.exec(line)) !== null) {
const [ bareUrl ] = match;
const matchIndex = match.index;
const bareUrlLength = bareUrl.length;

View file

@ -2,9 +2,8 @@
"use strict";
const { addErrorDetailIf, bareUrlRe, escapeForRegExp, forEachLine,
forEachLink, withinAnyRange, linkReferenceDefinitionRe } =
require("../helpers");
const { addErrorDetailIf, escapeForRegExp, forEachLine, forEachLink,
linkReferenceDefinitionRe, urlRe, withinAnyRange } = require("../helpers");
const { codeBlockAndSpanRanges, htmlElementRanges, lineMetadata } =
require("./cache");
@ -28,7 +27,7 @@ module.exports = {
exclusions.push([ lineIndex, 0, line.length ]);
} else {
let match = null;
while ((match = bareUrlRe.exec(line)) !== null) {
while ((match = urlRe.exec(line)) !== null) {
exclusions.push([ lineIndex, match.index, match[0].length ]);
}
forEachLink(line, (index, _, text, destination) => {

View file

@ -228,8 +228,9 @@ bar`
test("isBlankLine", (t) => {
t.plan(33);
// @ts-ignore
t.true(helpers.isBlankLine(null), "[null]");
const blankLines = [
null,
"",
" ",
" ",
@ -254,7 +255,7 @@ test("isBlankLine", (t) => {
"text --> --> <!--text--> <!--text--> <!-- <!-- text"
];
for (const line of blankLines) {
t.true(helpers.isBlankLine(line), line || "");
t.true(helpers.isBlankLine(line), line);
}
const nonBlankLines = [
"text",
@ -384,6 +385,7 @@ test("forEachInlineCodeSpan", (t) => {
for (const testCase of testCases) {
const { input, expecteds } = testCase;
helpers.forEachInlineCodeSpan(input, (code, line, column, ticks) => {
// @ts-ignore
const [ expectedCode, expectedLine, expectedColumn, expectedTicks ] =
expecteds.shift();
t.is(code, expectedCode, input);
@ -1306,3 +1308,87 @@ test("expandTildePath", (t) => {
);
t.is(helpers.expandTildePath("~/dir/file", null), "~/dir/file");
});
test("urlRe", (t) => {
t.plan(1);
const input = `
Text ftp://example.com text
Text ftps://example.com text
Text http://example.com text
Text https://example.com text
Text https://example.com/ text
Text https://example.com/path text
Text https://example.com/path/ text
Text https://example.com/path/file.txt text
Text https://example.com/path/file.txt?query=string text
Text https://example.com/path/file.txt#hash text
Text https://example.com/path/file.txt?query=string#hash text
Text https://example.com/path# text
Text https://example.com/path- text
Text https://example.com/path() text
Text https://example.com/path(path) text
Text https://example.com/path(path)path text
Text https://example.com/path-(path) text
Text (https://example.com/path) text
Text <https://example.com/path> text
Text >https://example.com/path< text
Text [https://example.com/path] text
Text "https://example.com/path" text
Text 'https://example.com/path' text
Text \`https://example.com/path\` text
Text [link](https://example.com/path) text
Text [link]( https://example.com/path ) text
Text <code>https://example.com/path</code> text
Text <a href="https://example.com/path">link</a> text
Text <a href="https://example.com/path">https://example.com/path</a> text
Text *https://example.com* text
Text **https://example.com** text
Text _https://example.com_ text
Text __https://example.com__ text
Text https://example.com, text
Text https://example.com. Text
Text https://example.com? Text
Text https://example.com! Text
`;
const expected = `
Text text
Text text
Text text
Text text
Text text
Text text
Text text
Text text
Text text
Text text
Text text
Text text
Text text
Text text
Text text
Text text
Text text
Text () text
Text <> text
Text >< text
Text [] text
Text "" text
Text '' text
Text \`\` text
Text [link]() text
Text [link]( ) text
Text <code></code> text
Text <a href="">link</a> text
Text <a href=""></a> text
Text ** text
Text **** text
Text _ text
Text __ text
Text , text
Text . Text
Text ? Text
Text ! Text
`;
const actual = input.replace(helpers.urlRe, "");
t.is(actual, expected);
});