aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-09-26 22:42:46 +0100
committerMichael Williamson <mike@zwobble.org>2026-09-26 22:42:46 +0100
commit8e6e755f793a02cf8986da1f8efb1c0c12d8d7a7 (patch)
treeb894fc137177f086317ed079af925b59a03204a4
parent9463f5c6ba7161477a6be98e2620044ad2c967db (diff)
Escape markdown image alt text and src
-rw-r--r--lib/writers/markdown-writer.js2
-rw-r--r--test/writers/markdown-writer.tests.js16
2 files changed, 15 insertions, 3 deletions
diff --git a/lib/writers/markdown-writer.js b/lib/writers/markdown-writer.js
index 02961fe..399acac 100644
--- a/lib/writers/markdown-writer.js
+++ b/lib/writers/markdown-writer.js
@@ -29,7 +29,7 @@ function markdownImage(attributes) {
var src = attributes.src || "";
var altText = attributes.alt || "";
if (src || altText) {
- return {start: "![" + altText + "](" + src + ")"};
+ return {start: "![" + escapeMarkdown(altText) + "](" + escapeMarkdown(src) + ")"};
} else {
return {};
}
diff --git a/test/writers/markdown-writer.tests.js b/test/writers/markdown-writer.tests.js
index d66cc79..578e6ff 100644
--- a/test/writers/markdown-writer.tests.js
+++ b/test/writers/markdown-writer.tests.js
@@ -117,13 +117,25 @@ test('links have anchors before opening square bracket', function() {
test('can generate images', function() {
var writer = mdWriter.writer();
writer.selfClosing("img", {"src": "http://example.com/image.jpg", "alt": "Alt Text"});
- return assert.equal(writer.asString(), "![Alt Text](http://example.com/image.jpg)");
+ return assert.equal(writer.asString(), "![Alt Text](http://example\\.com/image\\.jpg)");
+});
+
+test('image alt text is escaped', function() {
+ var writer = mdWriter.writer();
+ writer.selfClosing("img", {"src": "http://example.com/image.jpg", "alt": "Alt Text]Oops"});
+ return assert.equal(writer.asString(), "![Alt Text\\]Oops](http://example\\.com/image\\.jpg)");
+});
+
+test('image src is escaped', function() {
+ var writer = mdWriter.writer();
+ writer.selfClosing("img", {"src": "http://example.com/image.jpg)Oops", "alt": "Alt Text"});
+ return assert.equal(writer.asString(), "![Alt Text](http://example\\.com/image\\.jpg\\)Oops)");
});
test('can generate images with missing alt attribute', function() {
var writer = mdWriter.writer();
writer.selfClosing("img", {"src": "http://example.com/image.jpg"});
- return assert.equal(writer.asString(), "![](http://example.com/image.jpg)");
+ return assert.equal(writer.asString(), "![](http://example\\.com/image\\.jpg)");
});
test('can generate images with missing src attribute', function() {