target _blank for absolute urls. #266

Closed
opened 2026-01-29 14:32:14 +00:00 by claunia · 5 comments
Owner

Originally created by @TerribleDev on GitHub (Jan 21, 2019).

Hi, first off thanks for making this awesome package.

I'm using this for a blog engine I am writing. I have relative and absolute urls. I'd like my absolute urls to render with target="_blank" so they open in a new page.

I know this is a pluggable system, is there some docs for writing plugins? Side note, do you think this would be valuable to contribute back? Would you be interested in such an extension in this repo?

Originally created by @TerribleDev on GitHub (Jan 21, 2019). Hi, first off thanks for making this awesome package. I'm using this for a blog engine I am writing. I have relative and absolute urls. I'd like my absolute urls to render with target="_blank" so they open in a new page. I know this is a pluggable system, is there some docs for writing plugins? Side note, do you think this would be valuable to contribute back? Would you be interested in such an extension in this repo?
Author
Owner

@MihaZupan commented on GitHub (Jan 21, 2019):

I think the easiest way to go about doing this is to parse to the abstract syntax tree (Parse instead of ToHtml), go over all children and call SetAttributes for links with absolute urls.
Markdown object types you're probably interested in would be LinkInline and AutoLinkInline.

@MihaZupan commented on GitHub (Jan 21, 2019): I think the easiest way to go about doing this is to parse to the abstract syntax tree (Parse instead of ToHtml), go over all children and call SetAttributes for links with absolute urls. Markdown object types you're probably interested in would be LinkInline and AutoLinkInline.
Author
Owner

@TerribleDev commented on GitHub (Jan 22, 2019):

Hi @MihaZupan thanks so much for your comment.

I understand I need to walk over the AST and add a property. After looking around at the extensions that already exist I came up with this gist which seems to work.

Does that look like the right way to do this?

@TerribleDev commented on GitHub (Jan 22, 2019): Hi @MihaZupan thanks so much for your comment. I understand I need to walk over the AST and add a property. After looking around at the extensions that already exist I came up with [this gist](https://gist.github.com/TerribleDev/6278f98da75e7e96d467c6c72ba61e7a) which seems to work. Does that look like the right way to do this?
Author
Owner

@MihaZupan commented on GitHub (Jan 22, 2019):

Yep, that looks okay. I've shortened it a bit: gist.

Note that I also added AutoLinkInline, so you handle <link> cases as well.
With that, all versions should be handeled:

link
[Foo](link)
[Foo][Bar]
[Bar]
<link>

[Bar]: link
@MihaZupan commented on GitHub (Jan 22, 2019): Yep, that looks okay. I've shortened it a bit: [gist](https://gist.github.com/MihaZupan/d80fb29cae58028d413464e0c925623c). Note that I also added AutoLinkInline, so you handle `<link>` cases as well. With that, all versions should be handeled: ``` link [Foo](link) [Foo][Bar] [Bar] <link> [Bar]: link ```
Author
Owner

@MihaZupan commented on GitHub (Jan 22, 2019):

What I was referring to in the previous comment was editing the AST directly, not through an extension:

var document = Markdown.Parse(markdown, pipeline);
foreach (var descendant in document.Descendants())
{
    string url;
    if (descendant is AutolinkInline autoLink) url = autoLink.Url;
    else if (descendant is LinkInline linkInline) url = linkInline.Url;
    else continue;
    if (url != null && Uri.TryCreate(url, UriKind.Absolute, out _))
        descendant.GetAttributes().AddPropertyIfNotExist("target", "_blank");
}

var writer = new StringWriter();
var renderer = new HtmlRenderer(writer);
pipeline.Setup(renderer);
renderer.Render(document);

string html = writer.ToString();

That's up to personal preference tho

@MihaZupan commented on GitHub (Jan 22, 2019): What I was referring to in the previous comment was editing the AST directly, not through an extension: ```csharp var document = Markdown.Parse(markdown, pipeline); foreach (var descendant in document.Descendants()) { string url; if (descendant is AutolinkInline autoLink) url = autoLink.Url; else if (descendant is LinkInline linkInline) url = linkInline.Url; else continue; if (url != null && Uri.TryCreate(url, UriKind.Absolute, out _)) descendant.GetAttributes().AddPropertyIfNotExist("target", "_blank"); } var writer = new StringWriter(); var renderer = new HtmlRenderer(writer); pipeline.Setup(renderer); renderer.Render(document); string html = writer.ToString(); ``` That's up to personal preference tho
Author
Owner

@TerribleDev commented on GitHub (Jan 27, 2019):

oh ok thanks again for the info!

@TerribleDev commented on GitHub (Jan 27, 2019): oh ok thanks again for the info!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#266