From 63454d245761cee16302919f8daa35971a1f062f Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Mon, 10 Feb 2020 16:06:38 -0500 Subject: [PATCH] Fix GFM Example 116 Only code fences made from backticks cannot have other backticks in the same line. Fences with tildes can have both tildes and backticks. This allows the tilde case to work correctly. Also test for detection of these fences as they break or do not break paragraphs. --- src/rules.js | 4 +-- test/specs/commonmark/commonmark.0.29.json | 3 +-- test/specs/gfm/commonmark.0.29.json | 3 +-- .../specs/new/fences_breaking_paragraphs.html | 14 ++++++++++ test/specs/new/fences_breaking_paragraphs.md | 27 +++++++++++++++++++ 5 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 test/specs/new/fences_breaking_paragraphs.html create mode 100644 test/specs/new/fences_breaking_paragraphs.md diff --git a/src/rules.js b/src/rules.js index a290fa3396..0aac8a89be 100644 --- a/src/rules.js +++ b/src/rules.js @@ -10,7 +10,7 @@ const { const block = { newline: /^\n+/, code: /^( {4}[^\n]+\n*)+/, - fences: /^ {0,3}(`{3,}|~{3,})([^`~\n]*)\n(?:|([\s\S]*?)\n)(?: {0,3}\1[~`]* *(?:\n+|$)|$)/, + fences: /^ {0,3}(`{3,}(?=[^`\n]*\n)|~{3,})([^\n]*)\n(?:|([\s\S]*?)\n)(?: {0,3}\1[~`]* *(?:\n+|$)|$)/, hr: /^ {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)/, heading: /^ {0,3}(#{1,6}) +([^\n]*?)(?: +#+)? *(?:\n+|$)/, blockquote: /^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/, @@ -72,7 +72,7 @@ block.paragraph = edit(block._paragraph) .replace('heading', ' {0,3}#{1,6} +') .replace('|lheading', '') // setex headings don't interrupt commonmark paragraphs .replace('blockquote', ' {0,3}>') - .replace('fences', ' {0,3}(?:`{3,}|~{3,})[^`\\n]*\\n') + .replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n') .replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt .replace('html', ')|<(?:script|pre|style|!--)') .replace('tag', block._tag) // pars can be interrupted by type (6) html blocks diff --git a/test/specs/commonmark/commonmark.0.29.json b/test/specs/commonmark/commonmark.0.29.json index bbe9640a07..e15f83c1ba 100644 --- a/test/specs/commonmark/commonmark.0.29.json +++ b/test/specs/commonmark/commonmark.0.29.json @@ -930,8 +930,7 @@ "example": 116, "start_line": 1996, "end_line": 2003, - "section": "Fenced code blocks", - "shouldFail": true + "section": "Fenced code blocks" }, { "markdown": "```\n``` aaa\n```\n", diff --git a/test/specs/gfm/commonmark.0.29.json b/test/specs/gfm/commonmark.0.29.json index d2b21484c1..5dc9d47d8c 100644 --- a/test/specs/gfm/commonmark.0.29.json +++ b/test/specs/gfm/commonmark.0.29.json @@ -930,8 +930,7 @@ "example": 116, "start_line": 1996, "end_line": 2003, - "section": "Fenced code blocks", - "shouldFail": true + "section": "Fenced code blocks" }, { "markdown": "```\n``` aaa\n```\n", diff --git a/test/specs/new/fences_breaking_paragraphs.html b/test/specs/new/fences_breaking_paragraphs.html new file mode 100644 index 0000000000..372c0aa254 --- /dev/null +++ b/test/specs/new/fences_breaking_paragraphs.html @@ -0,0 +1,14 @@ +

A paragraph

+
Here is code in
+backtick fences
+

B paragraph

+
Here is code in
+tilde fences
+

C paragraph

+
Alternative
+tilde fences
+

D paragraph ```~D` Invalid use of backtick fences

+

+This will be read as
+part of a codeblock
+that ends with the file
diff --git a/test/specs/new/fences_breaking_paragraphs.md b/test/specs/new/fences_breaking_paragraphs.md new file mode 100644 index 0000000000..fbd7e2a00e --- /dev/null +++ b/test/specs/new/fences_breaking_paragraphs.md @@ -0,0 +1,27 @@ +A paragraph +```A +Here is code in +backtick fences +``` + +B paragraph +~~~B +Here is code in +tilde fences +~~~ + +C paragraph +~~~`C~ +Alternative +tilde fences +~~~ + +D paragraph +```~D` +Invalid use of +backtick fences +``` + +This will be read as +part of a codeblock +that ends with the file