Skip to content

Commit c80fc89

Browse files
committed
add options for link inline
1 parent 1b04599 commit c80fc89

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Markdig.Parsers.Inlines;
8+
9+
public class LinkInlineOptions
10+
{
11+
/// <summary>
12+
/// Should the link open in a new window when clicked (false by default)
13+
/// </summary>
14+
public bool OpenInNewWindow { get; set; }
15+
}

src/Markdig/Parsers/Inlines/LinkInlineParser.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the license.txt file in the project root for more information.
44

55
using Markdig.Helpers;
6+
using Markdig.Renderers.Html;
67
using Markdig.Syntax;
78
using Markdig.Syntax.Inlines;
89

@@ -17,11 +18,22 @@ public class LinkInlineParser : InlineParser
1718
/// <summary>
1819
/// Initializes a new instance of the <see cref="LinkInlineParser"/> class.
1920
/// </summary>
20-
public LinkInlineParser()
21+
public LinkInlineParser() : this(new LinkInlineOptions())
2122
{
23+
24+
}
25+
26+
/// <summary>
27+
/// Initializes a new instance of the <see cref="LinkInlineParser"/> class.
28+
/// </summary>
29+
public LinkInlineParser(LinkInlineOptions options)
30+
{
31+
Options = options ?? throw new ArgumentNullException(nameof(options));
2232
OpeningCharacters = ['[', ']', '!'];
2333
}
2434

35+
public readonly LinkInlineOptions Options;
36+
2537
public override bool Match(InlineProcessor processor, ref StringSlice slice)
2638
{
2739
// The following methods are inspired by the "An algorithm for parsing nested emphasis and links"
@@ -169,6 +181,11 @@ private bool ProcessLinkReference(
169181
linkInline.LocalLabel = localLabel;
170182
}
171183

184+
if (Options.OpenInNewWindow)
185+
{
186+
linkInline.GetAttributes().AddPropertyIfNotExist("target", "_blank");
187+
}
188+
172189
link = linkInline;
173190
}
174191

0 commit comments

Comments
 (0)