From 82ce6d0d6328a1f172b10e699677d6a1ff05fdde Mon Sep 17 00:00:00 2001 From: Dom Christie Date: Sun, 27 Jan 2019 15:12:41 +0000 Subject: [PATCH 1/2] Fetch attributes via getAttribute for broader compatibility --- src/commonmark-rules.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/commonmark-rules.js b/src/commonmark-rules.js index bf736183..588114a5 100644 --- a/src/commonmark-rules.js +++ b/src/commonmark-rules.js @@ -109,7 +109,7 @@ rules.fencedCodeBlock = { }, replacement: function (content, node, options) { - var className = node.firstChild.className || '' + var className = node.firstChild.getAttribute('class') || '' var language = (className.match(/language-(\S+)/) || [null, ''])[1] var code = node.firstChild.textContent @@ -153,7 +153,7 @@ rules.inlineLink = { replacement: function (content, node) { var href = node.getAttribute('href') - var title = node.title ? ' "' + node.title + '"' : '' + var title = node.getAttribute('title') ? ' "' + node.getAttribute('title') + '"' : '' return '[' + content + '](' + href + title + ')' } } @@ -251,9 +251,9 @@ rules.image = { filter: 'img', replacement: function (content, node) { - var alt = node.alt || '' + var alt = node.getAttribute('alt') || '' var src = node.getAttribute('src') || '' - var title = node.title || '' + var title = node.getAttribute('title') || '' var titlePart = title ? ' "' + title + '"' : '' return src ? '![' + alt + ']' + '(' + src + titlePart + ')' : '' } From 22c7fc94cd4a4a85293b0b7cc5adbc5fba3d9282 Mon Sep 17 00:00:00 2001 From: Dom Christie Date: Mon, 11 May 2020 15:13:58 -0700 Subject: [PATCH 2/2] Prevent newlines in attributes outputting new paragraphs --- src/commonmark-rules.js | 14 ++++++++++---- test/index.html | 31 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/commonmark-rules.js b/src/commonmark-rules.js index 588114a5..5bc99b28 100644 --- a/src/commonmark-rules.js +++ b/src/commonmark-rules.js @@ -153,7 +153,8 @@ rules.inlineLink = { replacement: function (content, node) { var href = node.getAttribute('href') - var title = node.getAttribute('title') ? ' "' + node.getAttribute('title') + '"' : '' + var title = cleanAttribute(node.getAttribute('title')) + if (title) title = ' "' + title + '"' return '[' + content + '](' + href + title + ')' } } @@ -169,7 +170,8 @@ rules.referenceLink = { replacement: function (content, node, options) { var href = node.getAttribute('href') - var title = node.title ? ' "' + node.title + '"' : '' + var title = cleanAttribute(node.getAttribute('title')) + if (title) title = ' "' + title + '"' var replacement var reference @@ -251,12 +253,16 @@ rules.image = { filter: 'img', replacement: function (content, node) { - var alt = node.getAttribute('alt') || '' + var alt = cleanAttribute(node.getAttribute('alt')) var src = node.getAttribute('src') || '' - var title = node.getAttribute('title') || '' + var title = cleanAttribute(node.getAttribute('title')) var titlePart = title ? ' "' + title + '"' : '' return src ? '![' + alt + ']' + '(' + src + titlePart + ')' : '' } } +function cleanAttribute (attribute) { + return attribute ? attribute.replace(/(\n+\s*)+/g, '\n') : '' +} + export default rules diff --git a/test/index.html b/test/index.html index 67118a90..4dd70aa0 100644 --- a/test/index.html +++ b/test/index.html @@ -176,6 +176,29 @@

 
 
+
+
img with
+    alt
+
![img with
+alt](logo.png)
+
+ +
+
img with
+    
+    alt
+
![img with
+alt](logo.png)
+
+ +
+
+
![](logo.png "the
+title")
+
+
[An anchor](http://example.com)
@@ -186,6 +209,14 @@
[An anchor](http://example.com "Title for link")
+
+ +
[An anchor](http://example.com "Title for
+link")
+
+
Anchor without a title