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

Optional link rewriter func for HtmlRenderer #201

Merged
merged 2 commits into from
Jan 25, 2018
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
1 change: 1 addition & 0 deletions src/Markdig.Tests/Markdig.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<Compile Include="TestOrderedList.cs" />
<Compile Include="TestPlainText.cs" />
<Compile Include="TestPragmaLines.cs" />
<Compile Include="TestLinkRewriter.cs" />
<Compile Include="TestRelativeUrlReplacement.cs" />
<Compile Include="TestSourcePosition.cs" />
<Compile Include="TestStringSliceList.cs" />
Expand Down
44 changes: 44 additions & 0 deletions src/Markdig.Tests/TestLinkRewriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.IO;
using Markdig.Parsers;
using Markdig.Renderers;
using NUnit.Framework;

namespace Markdig.Tests
{
public class TestLinkRewriter
{
[Test]
public void ReplacesRelativeLinks()
{
TestSpec(s => "abc" + s, "Link: [hello](/relative.jpg)", "abc/relative.jpg");
TestSpec(s => s + "xyz", "Link: [hello](relative.jpg)", "relative.jpgxyz");
TestSpec(null, "Link: [hello](relative.jpg)", "relative.jpg");
TestSpec(null, "Link: [hello](/relative.jpg)", "/relative.jpg");
}

[Test]
public void ReplacesRelativeImageSources()
{
TestSpec(s => "abc" + s, "Image: ![alt text](/image.jpg)", "abc/image.jpg");
TestSpec(s => "abc" + s, "Image: ![alt text](image.jpg \"title\")", "abcimage.jpg");
TestSpec(null, "Image: ![alt text](/image.jpg)", "/image.jpg");
}

public static void TestSpec(Func<string,string> linkRewriter, string markdown, string expectedLink)
{
var pipeline = new MarkdownPipelineBuilder().Build();

var writer = new StringWriter();
var renderer = new HtmlRenderer(writer);
renderer.LinkRewriter = linkRewriter;
pipeline.Setup(renderer);

var document = MarkdownParser.Parse(markdown, pipeline);
renderer.Render(document);
writer.Flush();

Assert.That(writer.ToString(), Contains.Substring("=\"" + expectedLink + "\""));
}
}
}
10 changes: 10 additions & 0 deletions src/Markdig/Renderers/HtmlRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public HtmlRenderer(TextWriter writer) : base(writer)
/// </summary>
public Uri BaseUrl { get; set; }

/// <summary>
/// Allows links to be rewritten
/// </summary>
public Func<string,string> LinkRewriter { get; set; }

/// <summary>
/// Writes the content escaped for HTML.
/// </summary>
Expand Down Expand Up @@ -202,6 +207,11 @@ public HtmlRenderer WriteEscapeUrl(string content)
content = new Uri(BaseUrl, content).AbsoluteUri;
}

if (LinkRewriter != null)
{
content = LinkRewriter(content);
}

int previousPosition = 0;
int length = content.Length;

Expand Down