diff --git a/src/DotMarkdown/IMarkdownEscaperProvider.cs b/src/DotMarkdown/IMarkdownEscaperProvider.cs new file mode 100644 index 00000000..dbf99239 --- /dev/null +++ b/src/DotMarkdown/IMarkdownEscaperProvider.cs @@ -0,0 +1,8 @@ +// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +namespace DotMarkdown; + +public interface IMarkdownEscaperProvider +{ + MarkdownCharEscaper GetDefaultEscaper(); +} diff --git a/src/DotMarkdown/MarkdownBaseWriter.cs b/src/DotMarkdown/MarkdownBaseWriter.cs index 5904cfbe..5b51250f 100644 --- a/src/DotMarkdown/MarkdownBaseWriter.cs +++ b/src/DotMarkdown/MarkdownBaseWriter.cs @@ -31,7 +31,10 @@ internal abstract class MarkdownBaseWriter : MarkdownWriter protected MarkdownBaseWriter(MarkdownWriterSettings? settings = null) { - Settings = settings ?? MarkdownWriterSettings.Default; + settings ??= MarkdownWriterSettings.Default; + + Settings = settings; + Escaper = settings.EscaperProvider.GetDefaultEscaper(); _codeFenceRegex = Format.CodeFenceStyle switch { @@ -80,7 +83,7 @@ public override WriteState WriteState protected internal abstract int Length { get; set; } - protected MarkdownCharEscaper Escaper { get; set; } = MarkdownCharEscaper.Default; + protected MarkdownCharEscaper Escaper { get; set; } private TableColumnInfo CurrentColumn => _tableColumns![_tableColumnIndex]; @@ -704,7 +707,7 @@ public override void WriteEndLink(string url, string? title = null) ThrowIfCannotWriteEnd(State.Link); - Escaper = MarkdownCharEscaper.Default; + Escaper = Settings.EscaperProvider.GetDefaultEscaper(); WriteRaw("]"); WriteRaw("("); WriteString(url, MarkdownCharEscaper.LinkUrl); diff --git a/src/DotMarkdown/MarkdownCharEscaper.cs b/src/DotMarkdown/MarkdownCharEscaper.cs index 5e520adb..1e5083df 100644 --- a/src/DotMarkdown/MarkdownCharEscaper.cs +++ b/src/DotMarkdown/MarkdownCharEscaper.cs @@ -2,23 +2,23 @@ namespace DotMarkdown; -internal abstract class MarkdownCharEscaper +public abstract class MarkdownCharEscaper { - public static char DefaultEscapingChar { get; } = '\\'; + internal static char DefaultEscapingChar { get; } = '\\'; - public static MarkdownCharEscaper Default { get; } = new DefaultMarkdownEscaper(); + internal static MarkdownCharEscaper Default { get; } = new DefaultMarkdownEscaper(); - public static MarkdownCharEscaper LinkText { get; } = new LinkTextMarkdownEscaper(); + internal static MarkdownCharEscaper LinkText { get; } = new LinkTextMarkdownEscaper(); - public static MarkdownCharEscaper LinkUrl { get; } = new LinkUrlMarkdownEscaper(); + internal static MarkdownCharEscaper LinkUrl { get; } = new LinkUrlMarkdownEscaper(); - public static MarkdownCharEscaper LinkTitle { get; } = new LinkTitleMarkdownEscaper(); + internal static MarkdownCharEscaper LinkTitle { get; } = new LinkTitleMarkdownEscaper(); - public static MarkdownCharEscaper AngleBrackets { get; } = new AngleBracketsMarkdownEscaper(); + internal static MarkdownCharEscaper AngleBrackets { get; } = new AngleBracketsMarkdownEscaper(); - public static MarkdownCharEscaper InlineCodeInsideTable { get; } = new InlineCodeInsideTableMarkdownEscaper(); + internal static MarkdownCharEscaper InlineCodeInsideTable { get; } = new InlineCodeInsideTableMarkdownEscaper(); - public static MarkdownCharEscaper NoEscape { get; } = new NoEscapeMarkdownEscaper(); + internal static MarkdownCharEscaper NoEscape { get; } = new NoEscapeMarkdownEscaper(); public abstract bool ShouldBeEscaped(char value); diff --git a/src/DotMarkdown/MarkdownEscaperProvider.cs b/src/DotMarkdown/MarkdownEscaperProvider.cs new file mode 100644 index 00000000..187cd635 --- /dev/null +++ b/src/DotMarkdown/MarkdownEscaperProvider.cs @@ -0,0 +1,10 @@ +// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +namespace DotMarkdown; + +internal sealed class MarkdownEscaperProvider : IMarkdownEscaperProvider +{ + public static IMarkdownEscaperProvider Default { get; } = new MarkdownEscaperProvider(); + + public MarkdownCharEscaper GetDefaultEscaper() => MarkdownCharEscaper.Default; +} diff --git a/src/DotMarkdown/MarkdownWriterSettings.cs b/src/DotMarkdown/MarkdownWriterSettings.cs index 8da7fca1..8607c304 100644 --- a/src/DotMarkdown/MarkdownWriterSettings.cs +++ b/src/DotMarkdown/MarkdownWriterSettings.cs @@ -12,12 +12,14 @@ public MarkdownWriterSettings( MarkdownFormat? format = null, string? newLineChars = null, NewLineHandling newLineHandling = NewLineHandling.Replace, - bool closeOutput = false) + bool closeOutput = false, + IMarkdownEscaperProvider? escaper = null) { Format = format ?? MarkdownFormat.Default; NewLineChars = newLineChars ?? Environment.NewLine; NewLineHandling = newLineHandling; CloseOutput = closeOutput; + EscaperProvider = escaper ?? MarkdownEscaperProvider.Default; } public static MarkdownWriterSettings Default { get; } = new(); @@ -34,6 +36,8 @@ public MarkdownWriterSettings( public bool CloseOutput { get; } + public IMarkdownEscaperProvider EscaperProvider { get; } + public MarkdownWriterSettings WithFormat(MarkdownFormat format) { return new MarkdownWriterSettings(format, NewLineChars, NewLineHandling, CloseOutput);