Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Markdig/Parsers/Inlines/LinkInlineOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Markdig.Parsers.Inlines;

public class LinkInlineOptions
{
/// <summary>
/// Should the link open in a new window when clicked (false by default)
/// </summary>
public bool OpenInNewWindow { get; set; }
}
19 changes: 18 additions & 1 deletion src/Markdig/Parsers/Inlines/LinkInlineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the license.txt file in the project root for more information.

using Markdig.Helpers;
using Markdig.Renderers.Html;
using Markdig.Syntax;
using Markdig.Syntax.Inlines;

Expand All @@ -17,11 +18,22 @@ public class LinkInlineParser : InlineParser
/// <summary>
/// Initializes a new instance of the <see cref="LinkInlineParser"/> class.
/// </summary>
public LinkInlineParser()
public LinkInlineParser() : this(new LinkInlineOptions())
{

}

/// <summary>
/// Initializes a new instance of the <see cref="LinkInlineParser"/> class.
/// </summary>
public LinkInlineParser(LinkInlineOptions options)
{
Options = options ?? throw new ArgumentNullException(nameof(options));
OpeningCharacters = ['[', ']', '!'];
}

public readonly LinkInlineOptions Options;

public override bool Match(InlineProcessor processor, ref StringSlice slice)
{
// The following methods are inspired by the "An algorithm for parsing nested emphasis and links"
Expand Down Expand Up @@ -169,6 +181,11 @@ private bool ProcessLinkReference(
linkInline.LocalLabel = localLabel;
}

if (Options.OpenInNewWindow)
{
linkInline.GetAttributes().AddPropertyIfNotExist("target", "_blank");
}

link = linkInline;
}

Expand Down