Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement custom escaping rules #48

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions src/DotMarkdown/IMarkdownEscaperProvider.cs
Original file line number Diff line number Diff line change
@@ -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();
}
9 changes: 6 additions & 3 deletions src/DotMarkdown/MarkdownBaseWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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);
Expand Down
18 changes: 9 additions & 9 deletions src/DotMarkdown/MarkdownCharEscaper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
10 changes: 10 additions & 0 deletions src/DotMarkdown/MarkdownEscaperProvider.cs
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 5 additions & 1 deletion src/DotMarkdown/MarkdownWriterSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down