Skip to content

Commit 4dc0be8

Browse files
authored
add options for link inline (#894)
* add options for link inline * create LinkOptions and associate it with all four parsers * set EnableHtmlParsing to true by default
1 parent 0e9e80e commit 4dc0be8

File tree

7 files changed

+74
-21
lines changed

7 files changed

+74
-21
lines changed

src/Markdig/Extensions/AutoLinks/AutoLinkOptions.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
// This file is licensed under the BSD-Clause 2 license.
33
// See the license.txt file in the project root for more information.
44

5+
using Markdig.Parsers;
6+
57
namespace Markdig.Extensions.AutoLinks;
68

7-
public class AutoLinkOptions
9+
public class AutoLinkOptions : LinkOptions
810
{
911
public AutoLinkOptions()
1012
{
@@ -13,11 +15,6 @@ public AutoLinkOptions()
1315

1416
public string ValidPreviousCharacters { get; set; }
1517

16-
/// <summary>
17-
/// Should the link open in a new window when clicked (false by default)
18-
/// </summary>
19-
public bool OpenInNewWindow { get; set; }
20-
2118
/// <summary>
2219
/// Should a www link be prefixed with https:// instead of http:// (false by default)
2320
/// </summary>

src/Markdig/Extensions/JiraLinks/JiraLinkOptions.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
// See the license.txt file in the project root for more information.
44

55
using Markdig.Helpers;
6+
using Markdig.Parsers;
67

78
namespace Markdig.Extensions.JiraLinks;
89

910
/// <summary>
1011
/// Available options for replacing JIRA links
1112
/// </summary>
12-
public class JiraLinkOptions
13+
public class JiraLinkOptions : LinkOptions
1314
{
1415
/// <summary>
1516
/// The base Url (e.g. `https://mycompany.atlassian.net`)
@@ -21,11 +22,6 @@ public class JiraLinkOptions
2122
/// </summary>
2223
public string BasePath { get; set; }
2324

24-
/// <summary>
25-
/// Should the link open in a new window when clicked
26-
/// </summary>
27-
public bool OpenInNewWindow { get; set; }
28-
2925
public JiraLinkOptions(string baseUrl)
3026
{
3127
OpenInNewWindow = true; //default

src/Markdig/MarkdownExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ public static MarkdownPipelineBuilder DisableHtml(this MarkdownPipelineBuilder p
538538
var inlineParser = pipeline.InlineParsers.Find<AutolinkInlineParser>();
539539
if (inlineParser != null)
540540
{
541-
inlineParser.EnableHtmlParsing = false;
541+
inlineParser.Options.EnableHtmlParsing = false;
542542
}
543543
return pipeline;
544544
}

src/Markdig/Parsers/Inlines/AutolinkInlineParser.cs

Lines changed: 18 additions & 7 deletions
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

@@ -14,19 +15,21 @@ namespace Markdig.Parsers.Inlines;
1415
/// <seealso cref="InlineParser" />
1516
public class AutolinkInlineParser : InlineParser
1617
{
18+
public AutolinkInlineParser() : this(new AutolinkOptions())
19+
{
20+
21+
}
22+
1723
/// <summary>
1824
/// Initializes a new instance of the <see cref="AutolinkInlineParser"/> class.
1925
/// </summary>
20-
public AutolinkInlineParser()
26+
public AutolinkInlineParser(AutolinkOptions options)
2127
{
28+
Options = options ?? throw new ArgumentNullException(nameof(options));
2229
OpeningCharacters = ['<'];
23-
EnableHtmlParsing = true;
2430
}
2531

26-
/// <summary>
27-
/// Gets or sets a value indicating whether to enable HTML parsing. Default is <c>true</c>
28-
/// </summary>
29-
public bool EnableHtmlParsing { get; set; }
32+
public readonly AutolinkOptions Options;
3033

3134
public override bool Match(InlineProcessor processor, ref StringSlice slice)
3235
{
@@ -42,8 +45,12 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
4245
Line = line,
4346
Column = column
4447
};
48+
if (Options.OpenInNewWindow)
49+
{
50+
processor.Inline.GetAttributes().AddPropertyIfNotExist("target", "_blank");
51+
}
4552
}
46-
else if (EnableHtmlParsing)
53+
else if (Options.EnableHtmlParsing)
4754
{
4855
slice = saved;
4956
if (!HtmlHelper.TryParseHtmlTag(ref slice, out string? htmlTag))
@@ -57,6 +64,10 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
5764
Line = line,
5865
Column = column
5966
};
67+
if (Options.OpenInNewWindow)
68+
{
69+
processor.Inline.GetAttributes().AddPropertyIfNotExist("target", "_blank");
70+
}
6071
}
6172
else
6273
{
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
2+
// This file is licensed under the BSD-Clause 2 license.
3+
// See the license.txt file in the project root for more information.
4+
5+
namespace Markdig.Parsers.Inlines;
6+
7+
public class AutolinkOptions : LinkOptions
8+
{
9+
/// <summary>
10+
/// Gets or sets a value indicating whether to enable HTML parsing. Default is <c>true</c>
11+
/// </summary>
12+
public bool EnableHtmlParsing { get; set; } = true;
13+
}

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 LinkOptions())
2122
{
23+
24+
}
25+
26+
/// <summary>
27+
/// Initializes a new instance of the <see cref="LinkInlineParser"/> class.
28+
/// </summary>
29+
public LinkInlineParser(LinkOptions options)
30+
{
31+
Options = options ?? throw new ArgumentNullException(nameof(options));
2232
OpeningCharacters = ['[', ']', '!'];
2333
}
2434

35+
public readonly LinkOptions 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

src/Markdig/Parsers/LinkOptions.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
2+
// This file is licensed under the BSD-Clause 2 license.
3+
// See the license.txt file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace Markdig.Parsers;
12+
13+
public class LinkOptions
14+
{
15+
/// <summary>
16+
/// Should the link open in a new window when clicked (false by default)
17+
/// </summary>
18+
public bool OpenInNewWindow { get; set; }
19+
}

0 commit comments

Comments
 (0)