How to Create Links With 'Target="_Blank"' in Markdown

Can I create links with 'target= _blank ' in Markdown?

As far as the Markdown syntax is concerned, if you want to get that detailed, you'll just have to use HTML.

<a href="http://example.com/" target="_blank">Hello, world!</a>

Most Markdown engines I've seen allow plain old HTML, just for situations like this where a generic text markup system just won't cut it. (The StackOverflow engine, for example.) They then run the entire output through an HTML whitelist filter, regardless, since even a Markdown-only document can easily contain XSS attacks. As such, if you or your users want to create _blank links, then they probably still can.

If that's a feature you're going to be using often, it might make sense to create your own syntax, but it's generally not a vital feature. If I want to launch that link in a new window, I'll ctrl-click it myself, thanks.

Is it possible to create links with 'target=“_blank”' in GitHub Flavored Markdown?

Looking at this answer here it seems that this is not possible, as Github does not include the target attribute even in plain HTML.

As for what I believe is the reasoning, I found this issue on github while browsing, which says that when target="_blank" is used without rel="noreferrer noopener", it can present a severe security vulnerability.

open link in new tab with github markdown using target= _blank

Well it seems that the simple answer is "It's not possible". Github does not include the target attribute even if you use plain HTML, so it's not a in the final HTML Anchor tag. Annoying, but OK, users can just do a CTRL+click (on Windows and Linux) or CMD+click (on MacOS) on the link, the get the same behavior.

markdown link opening in new tab

The kramdown syntax:

[link name](url_link){:target="_blank"}

can be parsed into HTML using the kramdown online editor:
https://kramdown.herokuapp.com/
Then you can paste the HTML syntax into your markdown document.

I used it because I already had quite a few kramdown references, and wanted to avoid retyping them in HTML.

Markdown open a new window link

There is no such feature in markdown, however you can always use HTML inside markdown:

<a href="http://example.com/" target="_blank">example</a>

Can I create links with 'target= _blank ' in hugo posts content using Markdown?

You need to create a new file at /layouts/_default/_markup/ called render-link.html.

In that file, you can then customise it to something like:

<a href="{{ .Destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}{{ if strings.HasPrefix .Destination  "http" }} target="_blank" rel="noopener"{{ end }}>{{ .Text | safeHTML }}</a>

Here is what would happen:

  • [link1](../something/ title="title") => <a href="../something/" title="title">link1</a>
  • [link2](https://example.com) => <a href="https://example.com">link2</a>

It will only add it to URLs with "http" and "https".

The documentation for render-hooks is available here: https://gohugo.io/templates/render-hooks/.

Target _blank in all Link

Put this in your <head>:

<base target="_blank">

It will make all URLs on a page open in a new page, unless target is specified.

This is a HTML5-only feature, I learned it from Google's io-2012-slides slide package.

How do I add target= _blank to a link within a specified div?

/* here are two different ways to do this */
//using jquery:
$(document).ready(function(){
$('#link_other a').attr('target', '_blank');
});

// not using jquery
window.onload = function(){
var anchors = document.getElementById('link_other').getElementsByTagName('a');
for (var i=0; i<anchors.length; i++){
anchors[i].setAttribute('target', '_blank');
}
}
// jquery is prettier. :-)

You could also add a title tag to notify the user that you are doing this, to warn them, because as has been pointed out, it's not what users expect:

$('#link_other a').attr('target', '_blank').attr('title','This link will open in a new window.');


Related Topics



Leave a reply



Submit