Skip to content

Commit

Permalink
Don't escape link URL text
Browse files Browse the repository at this point in the history
FIX: Fix a bug where URL text in links and images was overzealously
escaped.

Closes #61
  • Loading branch information
marijnh committed Jan 4, 2022
1 parent a57cb7a commit 8e456de
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/to_markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand All @@ -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) },
Expand Down
10 changes: 10 additions & 0 deletions test/test-parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 *<https://prosemirror.net>*",
doc(p("Link to ", em(link({href: "https://prosemirror.net"}, "https://prosemirror.net"))))))
Expand Down

0 comments on commit 8e456de

Please sign in to comment.