From a4a2dd8f852a7dc566c7f2a0f171134e1e9a7534 Mon Sep 17 00:00:00 2001 From: Jacob Weisz Date: Mon, 22 Jan 2018 12:01:54 -0600 Subject: [PATCH 1/2] Add linksInNewTab config option All credit due to @drmuey --- README.md | 10 +++++++++- lib/marked.js | 6 ++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 94ab0b9081..f784d0b2d7 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,8 @@ marked.setOptions({ sanitize: false, smartLists: true, smartypants: false, - xhtml: false + xhtml: false, + linksInNewTab: false }); console.log(marked('I am using __markdown__.')); @@ -282,6 +283,13 @@ Default: `false` Self-close the tags for void elements (<br/>, <img/>, etc.) with a "/" as required by XHTML. +### linksInNewTab + +Type: `boolean` +Default: `false` + +Opens links in new tabs. + ## Access to lexer and parser You also have direct access to the lexer and parser if you so desire. diff --git a/lib/marked.js b/lib/marked.js index f1d66ed4f9..61d3587ad8 100644 --- a/lib/marked.js +++ b/lib/marked.js @@ -899,6 +899,11 @@ Renderer.prototype.link = function(href, title, text) { if (title) { out += ' title="' + title + '"'; } + + if (this.options.linksInNewTab) { + out += ' target="_blank"'; + } + out += '>' + text + ''; return out; }; @@ -1331,6 +1336,7 @@ marked.defaults = { headerPrefix: '', renderer: new Renderer, xhtml: false, + linksInNewTab: false, baseUrl: null }; From 1fce83949260d23a9389f30788cc9f5610591c3b Mon Sep 17 00:00:00 2001 From: Jacob Weisz Date: Mon, 22 Jan 2018 12:47:47 -0600 Subject: [PATCH 2/2] Add test for linksInNewTab option --- test/new/linksinnewtab.html | 1 + test/new/linksinnewtab.md | 4 ++++ 2 files changed, 5 insertions(+) create mode 100644 test/new/linksinnewtab.html create mode 100644 test/new/linksinnewtab.md diff --git a/test/new/linksinnewtab.html b/test/new/linksinnewtab.html new file mode 100644 index 0000000000..2624add7f1 --- /dev/null +++ b/test/new/linksinnewtab.html @@ -0,0 +1 @@ +

Google

\ No newline at end of file diff --git a/test/new/linksinnewtab.md b/test/new/linksinnewtab.md new file mode 100644 index 0000000000..781cdcc1cc --- /dev/null +++ b/test/new/linksinnewtab.md @@ -0,0 +1,4 @@ +--- +linksInNewTab: true +--- +[Google](https://www.google.com) \ No newline at end of file