Skip to content

Commit

Permalink
Merge pull request #201 from markheath/relative-urls-2
Browse files Browse the repository at this point in the history
Optional link rewriter func for HtmlRenderer
  • Loading branch information
xoofx authored Jan 25, 2018
2 parents 4192a00 + 5f4b049 commit eedfc3c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
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

0 comments on commit eedfc3c

Please sign in to comment.