-
-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #197 from markheath/relative-urls
Convert relative URLs to absolute
- Loading branch information
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + "\"")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters