Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 3 additions & 6 deletions src/Markdig/Extensions/AutoLinks/AutoLinkOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

using Markdig.Parsers;

namespace Markdig.Extensions.AutoLinks;

public class AutoLinkOptions
public class AutoLinkOptions : LinkOptions
{
public AutoLinkOptions()
{
Expand All @@ -13,11 +15,6 @@ public AutoLinkOptions()

public string ValidPreviousCharacters { get; set; }

/// <summary>
/// Should the link open in a new window when clicked (false by default)
/// </summary>
public bool OpenInNewWindow { get; set; }

/// <summary>
/// Should a www link be prefixed with https:// instead of http:// (false by default)
/// </summary>
Expand Down
8 changes: 2 additions & 6 deletions src/Markdig/Extensions/JiraLinks/JiraLinkOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
// See the license.txt file in the project root for more information.

using Markdig.Helpers;
using Markdig.Parsers;

namespace Markdig.Extensions.JiraLinks;

/// <summary>
/// Available options for replacing JIRA links
/// </summary>
public class JiraLinkOptions
public class JiraLinkOptions : LinkOptions
{
/// <summary>
/// The base Url (e.g. `https://mycompany.atlassian.net`)
Expand All @@ -21,11 +22,6 @@ public class JiraLinkOptions
/// </summary>
public string BasePath { get; set; }

/// <summary>
/// Should the link open in a new window when clicked
/// </summary>
public bool OpenInNewWindow { get; set; }

public JiraLinkOptions(string baseUrl)
{
OpenInNewWindow = true; //default
Expand Down
2 changes: 1 addition & 1 deletion src/Markdig/MarkdownExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ public static MarkdownPipelineBuilder DisableHtml(this MarkdownPipelineBuilder p
var inlineParser = pipeline.InlineParsers.Find<AutolinkInlineParser>();
if (inlineParser != null)
{
inlineParser.EnableHtmlParsing = false;
inlineParser.Options.EnableHtmlParsing = false;
}
return pipeline;
}
Expand Down
25 changes: 18 additions & 7 deletions src/Markdig/Parsers/Inlines/AutolinkInlineParser.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 @@ -14,19 +15,21 @@ namespace Markdig.Parsers.Inlines;
/// <seealso cref="InlineParser" />
public class AutolinkInlineParser : InlineParser
{
public AutolinkInlineParser() : this(new AutolinkOptions())
{

}

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

/// <summary>
/// Gets or sets a value indicating whether to enable HTML parsing. Default is <c>true</c>
/// </summary>
public bool EnableHtmlParsing { get; set; }
public readonly AutolinkOptions Options;

public override bool Match(InlineProcessor processor, ref StringSlice slice)
{
Expand All @@ -42,8 +45,12 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
Line = line,
Column = column
};
if (Options.OpenInNewWindow)
{
processor.Inline.GetAttributes().AddPropertyIfNotExist("target", "_blank");
}
}
else if (EnableHtmlParsing)
else if (Options.EnableHtmlParsing)
{
slice = saved;
if (!HtmlHelper.TryParseHtmlTag(ref slice, out string? htmlTag))
Expand All @@ -57,6 +64,10 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
Line = line,
Column = column
};
if (Options.OpenInNewWindow)
{
processor.Inline.GetAttributes().AddPropertyIfNotExist("target", "_blank");
}
}
else
{
Expand Down
13 changes: 13 additions & 0 deletions src/Markdig/Parsers/Inlines/AutolinkOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

namespace Markdig.Parsers.Inlines;

public class AutolinkOptions : LinkOptions
{
/// <summary>
/// Gets or sets a value indicating whether to enable HTML parsing. Default is <c>true</c>
/// </summary>
public bool EnableHtmlParsing { get; set; } = true;
}
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 LinkOptions())
{

}

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

public readonly LinkOptions 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
19 changes: 19 additions & 0 deletions src/Markdig/Parsers/LinkOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Markdig.Parsers;

public class LinkOptions
{
/// <summary>
/// Should the link open in a new window when clicked (false by default)
/// </summary>
public bool OpenInNewWindow { get; set; }
}