From 8e456de723a64a05de6d6dbe110b24d395eff316 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Tue, 4 Jan 2022 19:36:22 +0100 Subject: [PATCH] Don't escape link URL text FIX: Fix a bug where URL text in links and images was overzealously escaped. Closes https://github.com/ProseMirror/prosemirror-markdown/issues/61 --- src/to_markdown.js | 6 +++--- test/test-parse.js | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/to_markdown.js b/src/to_markdown.js index 447e47b5..ae8c458e 100644 --- a/src/to_markdown.js +++ b/src/to_markdown.js @@ -95,8 +95,8 @@ export const defaultMarkdownSerializer = new MarkdownSerializer({ }, image(state, node) { - state.write("![" + state.esc(node.attrs.alt || "") + "](" + state.esc(node.attrs.src) + - (node.attrs.title ? " " + state.quote(node.attrs.title) : "") + ")") + state.write("![" + state.esc(node.attrs.alt || "") + "](" + node.attrs.src + + (node.attrs.title ? ' "' + node.attrs.title.replace(/"/g, '\\"') + '"' : "") + ")") }, hard_break(state, node, parent, index) { for (let i = index + 1; i < parent.childCount; i++) @@ -117,7 +117,7 @@ export const defaultMarkdownSerializer = new MarkdownSerializer({ }, close(state, mark, parent, index) { return isPlainURL(mark, parent, index, -1) ? ">" - : "](" + state.esc(mark.attrs.href) + (mark.attrs.title ? " " + state.quote(mark.attrs.title) : "") + ")" + : "](" + mark.attrs.href + (mark.attrs.title ? ' "' + mark.attrs.title.replace(/"/g, '\\"') + '"' : "") + ")" } }, code: {open(_state, _mark, parent, index) { return backticksFor(parent.child(index), -1) }, diff --git a/test/test-parse.js b/test/test-parse.js index c762d3ca..14c2ffdc 100644 --- a/test/test-parse.js +++ b/test/test-parse.js @@ -95,6 +95,16 @@ describe("markdown", () => { doc(p(link({href: "foo.html"}, "foo.html")))) }) + it("can handle link titles", () => { + same('[a](x.html "title \\"quoted\\"")', + doc(p(link({href: "x.html", title: 'title "quoted"'}, "a")))) + }) + + it("doesn't escape underscores in link", () => { + same('[link](http://foo.com/a_b_c)', + doc(p(link({href: "http://foo.com/a_b_c"}, "link")))) + }) + it("parses emphasized urls", () => same("Link to **", doc(p("Link to ", em(link({href: "https://prosemirror.net"}, "https://prosemirror.net"))))))