Some text[1]
Body/Reference:[1]: http://somelink.com/?target=_blank
Just make sure that the target attribute is passed first, if there are additional attributes in the link, copy/paste them to the end of the reference URL.
Also work as direct link:[Go to this page](http://somelink.com/?target=_blank)
You can do this via native javascript code like so:
var pattern = /a href=/g;var sanitizedMarkDownText = rawMarkDownText.replace(pattern,"a target='_blank' href=");
JSFiddle Code
In my project I'm doing this and it works fine:
[Link](https://example.org/ "title" target="_blank")
Link
But not all parsers let you do that.
There's no easy way to do it, and like @alex has noted you'll need to use JavaScript. His answer is the best solution but in order to optimize it, you might want to filter only to the post-content links.
<script>var links = document.querySelectorAll( '.post-content a' ); for (var i = 0, length = links.length; i < length; i++) { if (links[i].hostname != window.location.hostname) {links[i].target = '_blank';}}</script>
The code is compatible with IE8+ and you can add it to the bottom of your page. Note that you'll need to change the ".post-content a" to the class that you're using for your posts.
As seen here: http://blog.hubii.com/target-_blank-for-links-on-ghost/
If someone is looking for a global rmarkdown
(pandoc
) solution.
You could write your own Pandoc Lua Filter which adds target="_blank"
to all links:
links.lua
function Link(element)if string.sub(element.target, 1, 1) ~= "#"thenelement.attributes.target = "_blank"endreturn elementend
_output.yml
bookdown::gitbook:pandoc_args:- --lua-filter=links.lua
<base target="_blank">
in HeaderAn alternative solution would be to inject <base target="_blank">
in the HTML head
section using the includes
option:
links.html
<base target="_blank">
_output.yml
bookdown::gitbook:includes:in_header: links.html
Note: This solution may also open new tabs for hash (#
) pointers/URLs. I have not tested this solution with such URLs.
In Laravel I solved it this way:
$post->text= Str::replace('<a ', '<a target="_blank"', $post->text);
Not works for a specific link. Edit all links in the Markdown text. (In my case it's fine)
If you work with MkDocs, you might be interested in the Open in a new tab - plugin
This plugin adds JS to open outgoing links and PDFs in a new tab.
Install the plugin using pip from PyPI:
pip3 install mkdocs-open-in-new-tab
Just add the plugin:
plugins:- search- open-in-new-tab
More about the plugin.
I ran into this problem when trying to implement markdown using PHP.
Since the user generated links created with markdown need to open in a new tab but site links need to stay in tab I changed markdown to only generate links that open in a new tab. So not all links on the page link out, just the ones that use markdown.
In markdown I changed all the link output to be <a target='_blank' href="...">
which was easy enough using find/replace.
I do not agree that it's a better user experience to stay within one browser tab. If you want people to stay on your site, or come back to finish reading that article, send them off in a new tab.
Building on @davidmorrow's answer, throw this javascript into your site and turn just external links into links with target=_blank:
<script type="text/javascript" charset="utf-8">// Creating custom :external selector$.expr[':'].external = function(obj){return !obj.href.match(/^mailto\:/)&& (obj.hostname != location.hostname);};$(function(){// Add 'external' CSS class to all external links$('a:external').addClass('external');// turn target into target=_blank for elements w external class$(".external").attr('target','_blank');})</script>
You can add any attributes using {[attr]="[prop]"}
For example [Google] (http://www.google.com){target="_blank"}
For completed alex answered (Dec 13 '10)
A more smart injection target could be done with this code :
/** For all links in the current page...*/$(document.links).filter(function() {/** ...keep them without `target` already setted...*/return !this.target;}).filter(function() {/** ...and keep them are not on current domain...*/return this.hostname !== window.location.hostname ||/** ...or are not a web file (.pdf, .jpg, .png, .js, .mp4, etc.).*//\.(?!html?|php3?|aspx?)([a-z]{0,3}|[a-zt]{0,4})$/.test(this.pathname);/** For all link kept, add the `target="_blank"` attribute. */}).attr('target', '_blank');
You could change the regexp exceptions with adding more extension in (?!html?|php3?|aspx?)
group construct (understand this regexp here: https://regex101.com/r/sE6gT9/3).
and for a without jQuery version, check code below:
var links = document.links;for (var i = 0; i < links.length; i++) {if (!links[i].target) {if (links[i].hostname !== window.location.hostname || /\.(?!html?)([a-z]{0,3}|[a-zt]{0,4})$/.test(links[i].pathname)) {links[i].target = '_blank';} }}
If one would like to do this systematically for all external links, CSS is no option. However, one could run the following sed
command once the (X)HTML has been created from Markdown:
sed -i 's|href="http|target="_blank" href="http|g' index.html
This can be further automated by adding above sed
command to a makefile
. For details, see GNU make
or see how I have done that on my website.
If you just want to do this in a specific link, just use the inline attribute list syntax as others have answered, or just use HTML.
If you want to do this in all generated <a>
tags, depends on your Markdown compiler, maybe you need an extension of it.
I am doing this for my blog these days, which is generated by pelican
, which use Python-Markdown
. And I found an extension for Python-Markdown
Phuker/markdown_link_attr_modifier, it works well. Note that an old extension called newtab
seems not work in Python-Markdown 3.x.
React
+ Markdown
environment:I created a reusable component:
export type TargetBlankLinkProps = {label?: string;href?: string;};export const TargetBlankLink = ({label = "",href = "",}: TargetBlankLinkProps) => (<a href={href} target="__blank">{label}</a>);
And I use it wherever I need a link that open in a new window.
For "markdown-to-jsx" with MUI v5
This seem to work for me:
import Markdown from 'markdown-to-jsx';
...
const MarkdownLink = ({ children, ...props }) => (<Link {...props}>{children}</Link>);
...
<Markdownoptions={{forceBlock: true,overrides: {a: {component: MarkdownLink,props: {target: '_blank',},},},}}>{description}</Markdown>
This works for me: [Page Link](your url here "(target|_blank)")