Skip to content

Commit

Permalink
Add support for relative Urls for Video and Audio links.
Browse files Browse the repository at this point in the history
  • Loading branch information
RickStrahl committed Jul 11, 2023
1 parent bfe3800 commit 81bc58c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
10 changes: 10 additions & 0 deletions src/Markdig.Tests/TestMediaLinks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ public void TestBuiltInHosts(string markdown, string expected)
Assert.AreEqual(html, expected);
}

[TestCase("![static video relative path](./video.mp4)",
"<p><video width=\"500\" height=\"281\" controls=\"\"><source type=\"video/mp4\" src=\"./video.mp4\"></source></video></p>\n")]
[TestCase("![static audio relative path](./audio.mp3)",
"<p><audio width=\"500\" controls=\"\"><source type=\"audio/mpeg\" src=\"./audio.mp3\"></source></audio></p>\n")]
public void TestBuiltInHostsWithRelativePaths(string markdown, string expected)
{
string html = Markdown.ToHtml(markdown, GetPipeline());
Assert.AreEqual(html, expected);
}

private class TestHostProvider : IHostProvider
{
public string Class { get; } = "regex";
Expand Down
40 changes: 25 additions & 15 deletions src/Markdig/Extensions/MediaLinks/MediaLinkExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,32 @@ private bool TryLinkInlineRenderer(HtmlRenderer renderer, LinkInline linkInline)
return false;
}

var url = linkInline.Url;
bool isSchemaRelative = false;
// Only process absolute Uri
if (!Uri.TryCreate(linkInline.Url, UriKind.RelativeOrAbsolute, out Uri? uri) || !uri.IsAbsoluteUri)

// force // schema to an absolute url
if (url.StartsWith("//", StringComparison.Ordinal))
{
// see https://tools.ietf.org/html/rfc3986#section-4.2
// since relative uri doesn't support many properties, "http" is used as a placeholder here.
if (linkInline.Url.StartsWith("//", StringComparison.Ordinal) && Uri.TryCreate("http:" + linkInline.Url, UriKind.Absolute, out uri))
{
isSchemaRelative = true;
}
else
{
return false;
}
url = "https:" + url;
isSchemaRelative = true;
}

if (TryRenderIframeFromKnownProviders(uri, isSchemaRelative, renderer, linkInline))
// Make sure we have a valid absolute/relative url
if (!Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out Uri? uri)) // || !uri.IsAbsoluteUri)
{
return true;
return false;
}

// iFrame has to be absolute path
if (uri.IsAbsoluteUri)
{
if (TryRenderIframeFromKnownProviders(uri, isSchemaRelative, renderer, linkInline))
{
return true;
}
}

// audio/video has can have relative path
if (TryGuessAudioVideoFile(uri, isSchemaRelative, renderer, linkInline))
{
return true;
Expand All @@ -93,7 +98,12 @@ private static HtmlAttributes GetHtmlAttributes(LinkInline linkInline)

private bool TryGuessAudioVideoFile(Uri uri, bool isSchemaRelative, HtmlRenderer renderer, LinkInline linkInline)
{
var path = uri.GetComponents(UriComponents.Path, UriFormat.Unescaped);
string path;
if (uri.IsAbsoluteUri)
path = uri.GetComponents(UriComponents.Path, UriFormat.Unescaped);
else
path = uri.ToString();

// Otherwise try to detect if we have an audio/video from the file extension
var lastDot = path.LastIndexOf('.');
if (lastDot >= 0 &&
Expand Down

0 comments on commit 81bc58c

Please sign in to comment.