Skip to content

Commit 20e88fe

Browse files
authored
fix: handle CR in rules to follow CommonMark spec (#493)
* fix: add CR to the line ending pattern * wip: add test * wip: add more tests * wip: mark error prone patterns * wip * wip * wip * wip: `no-html` * wip * wip: simplify regex * wip * wip * wip * wip
1 parent 0d01b19 commit 20e88fe

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

src/rules/no-html.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
* @author Nicholas C. Zakas
44
*/
55

6+
//-----------------------------------------------------------------------------
7+
// Imports
8+
//-----------------------------------------------------------------------------
9+
10+
import { lineEndingPattern } from "../util.js";
11+
612
//-----------------------------------------------------------------------------
713
// Type Definitions
814
//-----------------------------------------------------------------------------
@@ -20,7 +26,6 @@
2026

2127
const htmlTagPattern =
2228
/<(?<tagName>[a-z0-9]+(?:-[a-z0-9]+)*)(?:\s(?:[^>"']|"[^"]*"|'[^']*')*)?>/giu;
23-
const lineEndingPattern = /\r\n?|\n/u;
2429

2530
//-----------------------------------------------------------------------------
2631
// Rule Definition

src/rules/no-reference-like-urls.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { normalizeIdentifier } from "micromark-util-normalize-identifier";
2727

2828
/** Pattern to match both inline links: `[text](url)` and images: `![alt](url)`, with optional title */
2929
const linkOrImagePattern =
30-
/(?<imageBang>!)?\[(?<label>(?:\\.|[^()\\]|\([\s\S]*\))*?)\]\((?<destination>[ \t]*(?:\r\n?|\n)?(?<![ \t])[ \t]*(?:<[^>]*>|[^ \t()]+))(?:[ \t]*(?:\r\n?|\n)?(?<![ \t])[ \t]*(?<title>"[^"]*"|'[^']*'|\([^)]*\)))?[ \t]*(?:\r\n?|\n)?(?<![ \t])[ \t]*\)$/u;
30+
/\[(?<label>(?:\\.|[^()\\]|\([\s\S]*\))*?)\]\((?<destination>[ \t]*\r?\n?(?<![ \t])[ \t]*(?:<[^>]*>|[^ \t()]+))(?:[ \t]*\r?\n?(?<![ \t])[ \t]*(?:"[^"]*"|'[^']*'|\([^)]*\)))?[ \t]*\r?\n?(?<![ \t])[ \t]*\)$/u;
3131

3232
//-----------------------------------------------------------------------------
3333
// Rule Definition
@@ -75,17 +75,9 @@ export default {
7575

7676
const match = linkOrImagePattern.exec(text);
7777
if (match !== null) {
78-
const {
79-
imageBang,
80-
label,
81-
destination,
82-
title: titleRaw,
83-
} = match.groups;
84-
const title = titleRaw?.slice(1, -1);
85-
86-
const isImage = !!imageBang;
87-
const type = isImage ? "image" : "link";
88-
const prefix = isImage ? "!" : "";
78+
const { label, destination } = match.groups;
79+
const { type, title } = node;
80+
const prefix = type === "image" ? "!" : "";
8981
const url =
9082
normalizeIdentifier(destination).toLowerCase();
9183

src/rules/no-reversed-media-syntax.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
/** Matches reversed link/image syntax like `(text)[url]`, ignoring escaped characters like `\(text\)[url]`. */
2424
const reversedPattern =
25-
/(?<=(?<!\\)(?:\\{2})*)\((?<label>(?:\\.|[^()\\]|\([\s\S]*\))*)\)\[(?<url>(?:\\.|[^\]\\\n])*)\](?!\()/gu;
25+
/(?<=(?<!\\)(?:\\{2})*)\((?<label>(?:\\.|[^()\\]|\([\s\S]*\))*)\)\[(?<url>(?:\\.|[^\]\\\r\n])*)\](?!\()/gu;
2626

2727
/**
2828
* Checks if a match is within any skip range

tests/rules/no-reversed-media-syntax.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ ruleTester.run("no-reversed-media-syntax", rule, {
3131
"![foo](bar)",
3232
"![foo](#bar)",
3333
"![foo](http://bar.com)",
34+
"(foo)[bar\r\n]",
35+
"(foo)[bar\r]",
36+
"(foo)[bar\n]",
3437
"(foo)[bar](http://bar.com)",
3538
" myObj.getFiles(test)[0]",
3639
dedent`

0 commit comments

Comments
 (0)