Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What would be the correct way to make links open in external window? #144

Closed
katowulf opened this issue Mar 28, 2013 · 6 comments
Closed

Comments

@katowulf
Copy link

Hi gents,

I'm attempting to coerce marked (great lib! thanks for sharing!) into making all links in the markdown content open in external windows.

What would be best practice for this? Can I use the lexer to manipulate the tokens in some way that would accomplish this before they go into the parser?

Cheers,

@lepture
Copy link
Contributor

lepture commented Mar 29, 2013

Have a look at my pull request: #129

The renderer feature would be a good choice to do such thing. It didn't has a link renderer, I implemented block level renderer for now and will add the span level api when this feature merged.

@Mithgol
Copy link
Contributor

Mithgol commented Mar 29, 2013

Maybe you should use jQuery and just $(yourElement).find('a').attr('target','_blank') after you put your HTML-encoded Markdown where necessary.

@sudhirj
Copy link

sudhirj commented Mar 29, 2013

If you're open to modifying your version of marked, you can just redefine the template that it uses for autolinking (a GFM feature). Right now, the line is here: https://github.com/chjj/marked/blob/master/lib/marked.js#L578

@katowulf
Copy link
Author

@lepture thanks, very interesting

@Mithgol certainly the first thing that came to mind, if a bit inelegant

@sudhirj thanks; that's a great insight; and if not that, I could at least use that knowledge together with the lexer/parser access to do something spiffy.

@icoxfog417
Copy link

I implemented it as below.
The concept is make object for rendering the link by Renderer.prototype.link function.

var renderer = new marked.Renderer();
(function(){
    anchorRender = {
        options:{
            sanitize:true
        },
        render: marked.Renderer.prototype.link
    };
    renderer.link = function(href, title, text){
        var anchor = anchorRender.render(href, title, text);
        return anchor.replace("<a","<a target='_blank' ");
    }
})();
marked.setOptions({
  renderer: renderer
})

@mrcoles
Copy link

mrcoles commented Apr 17, 2015

Thanks for the code block @icoxfog417 — I looked it over a little more and also saw an example from the docs for overriding renderer methods, and the code can more simply be written as such:

var renderer = new marked.Renderer();
renderer.link = function(href, title, text) {
    var link = marked.Renderer.prototype.link.call(this, href, title, text);
    return link.replace("<a","<a target='_blank' ");
};

marked.setOptions({
    renderer: renderer
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants