Skip to content

Commit

Permalink
Merge pull request #197 from markheath/relative-urls
Browse files Browse the repository at this point in the history
Convert relative URLs to absolute
  • Loading branch information
xoofx authored Jan 17, 2018
2 parents f64ac47 + 96c4690 commit 42683e0
Show file tree
Hide file tree
Showing 3 changed files with 59 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="TestRelativeUrlReplacement.cs" />
<Compile Include="TestSourcePosition.cs" />
<Compile Include="TestStringSliceList.cs" />
<Compile Include="TestPlayParser.cs" />
Expand Down
48 changes: 48 additions & 0 deletions src/Markdig.Tests/TestRelativeUrlReplacement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.IO;
using Markdig.Parsers;
using Markdig.Renderers;
using NUnit.Framework;

namespace Markdig.Tests
{
public class TestRelativeUrlReplacement
{
[Test]
public void ReplacesRelativeLinks()
{
TestSpec("https://example.com", "Link: [hello](/relative.jpg)", "https://example.com/relative.jpg");
TestSpec("https://example.com", "Link: [hello](relative.jpg)", "https://example.com/relative.jpg");
TestSpec("https://example.com/", "Link: [hello](/relative.jpg?a=b)", "https://example.com/relative.jpg?a=b");
TestSpec("https://example.com/", "Link: [hello](relative.jpg#x)", "https://example.com/relative.jpg#x");
TestSpec(null, "Link: [hello](relative.jpg)", "relative.jpg");
TestSpec(null, "Link: [hello](/relative.jpg)", "/relative.jpg");
TestSpec("https://example.com", "Link: [hello](/relative.jpg)", "https://example.com/relative.jpg");
}

[Test]
public void ReplacesRelativeImageSources()
{
TestSpec("https://example.com", "Image: ![alt text](/image.jpg)", "https://example.com/image.jpg");
TestSpec("https://example.com", "Image: ![alt text](image.jpg \"title\")", "https://example.com/image.jpg");
TestSpec(null, "Image: ![alt text](/image.jpg)", "/image.jpg");
}

public static void TestSpec(string baseUrl, string markdown, string expectedLink)
{
var pipeline = new MarkdownPipelineBuilder().Build();

var writer = new StringWriter();
var renderer = new HtmlRenderer(writer);
if (baseUrl != null)
renderer.BaseUrl = new Uri(baseUrl);
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 @@ -75,6 +75,11 @@ public HtmlRenderer(TextWriter writer) : base(writer)

public bool UseNonAsciiNoEscape { get; set; }

/// <summary>
/// Gets a value to use as the base url for all relative links
/// </summary>
public Uri BaseUrl { get; set; }

/// <summary>
/// Writes the content escaped for HTML.
/// </summary>
Expand Down Expand Up @@ -192,6 +197,11 @@ public HtmlRenderer WriteEscapeUrl(string content)
if (content == null)
return this;

if (BaseUrl != null && !Uri.TryCreate(content, UriKind.Absolute, out Uri _))
{
content = new Uri(BaseUrl, content).AbsoluteUri;
}

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

Expand Down

0 comments on commit 42683e0

Please sign in to comment.