Is there a way to render links in such a way that they will open in new window ? #489

Closed
opened 2026-01-29 14:38:00 +00:00 by claunia · 2 comments
Owner

Originally created by @macmarek on GitHub (Dec 2, 2021).

Hey,
I'm trying to make sure that all links generated by Markdig are opening in a new browser window. Is there a way to achieve this?

Example:
I'd like
[stackoverflow.com](https://stackoverflow.com/)

to be rendered as
<a href="https://stackoverflow.com/" target="_blank">stackoverflow.com</a>

instead of
<a href="https://stackoverflow.com/">stackoverflow.com</a>

Originally created by @macmarek on GitHub (Dec 2, 2021). Hey, I'm trying to make sure that all links generated by Markdig are opening in a new browser window. Is there a way to achieve this? Example: I'd like ```[stackoverflow.com](https://stackoverflow.com/)``` to be rendered as ```<a href="https://stackoverflow.com/" target="_blank">stackoverflow.com</a>``` instead of ```<a href="https://stackoverflow.com/">stackoverflow.com</a>```
Author
Owner

@MihaZupan commented on GitHub (Dec 2, 2021):

If you only care about auto links (links that just appear in the text and arent defined as [foo](url) or <url>), you can set the OpenInNewWindow flag:

var pipeline = new MarkdownPipelineBuilder()
    .UseAutoLinks(new AutoLinkOptions { OpenInNewWindow = true })
    .Build();

To get the same effect on all links, the easiest way is probably to post-process the document and add the attributes.

MarkdownDocument document = Markdown.Parse(markdown, pipeline);

foreach (LinkInline link in document.Descendants<LinkInline>())
{
    link.GetAttributes().AddPropertyIfNotExist("target", "_blank");
}

foreach (AutolinkInline link in document.Descendants<AutolinkInline>())
{
    link.GetAttributes().AddPropertyIfNotExist("target", "_blank");
}

string html = document.ToHtml(pipeline);
@MihaZupan commented on GitHub (Dec 2, 2021): If you only care about auto links (links that just appear in the text and arent defined as `[foo](url)` or `<url>`), you can set the `OpenInNewWindow` flag: ```c# var pipeline = new MarkdownPipelineBuilder() .UseAutoLinks(new AutoLinkOptions { OpenInNewWindow = true }) .Build(); ``` To get the same effect on all links, the easiest way is probably to post-process the document and add the attributes. ```c# MarkdownDocument document = Markdown.Parse(markdown, pipeline); foreach (LinkInline link in document.Descendants<LinkInline>()) { link.GetAttributes().AddPropertyIfNotExist("target", "_blank"); } foreach (AutolinkInline link in document.Descendants<AutolinkInline>()) { link.GetAttributes().AddPropertyIfNotExist("target", "_blank"); } string html = document.ToHtml(pipeline); ```
Author
Owner

@macmarek commented on GitHub (Dec 2, 2021):

Thanks a lot. Works like a charm!

@macmarek commented on GitHub (Dec 2, 2021): Thanks a lot. Works like a charm!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#489