-
-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into emoji-customization
- Loading branch information
Showing
9 changed files
with
512 additions
and
325 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,87 @@ | ||
using Markdig.Extensions.MediaLinks; | ||
using NUnit.Framework; | ||
using System; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Markdig.Tests | ||
{ | ||
[TestFixture] | ||
public class TestMediaLinks | ||
{ | ||
private MarkdownPipeline GetPipeline(MediaOptions options = null) | ||
{ | ||
return new MarkdownPipelineBuilder() | ||
.UseMediaLinks(options) | ||
.Build(); | ||
} | ||
|
||
[Test] | ||
[TestCase("![static mp4](https://sample.com/video.mp4)", "<p><video width=\"500\" height=\"281\" controls=\"\"><source type=\"video/mp4\" src=\"https://sample.com/video.mp4\"></source></video></p>\n")] | ||
[TestCase("![static mp4](//sample.com/video.mp4)", "<p><video width=\"500\" height=\"281\" controls=\"\"><source type=\"video/mp4\" src=\"//sample.com/video.mp4\"></source></video></p>\n")] | ||
[TestCase(@"![youtube.com](https://www.youtube.com/watch?v=mswPy5bt3TQ)", "<p><iframe src=\"https://www.youtube.com/embed/mswPy5bt3TQ\" width=\"500\" height=\"281\" class=\"youtube\" frameborder=\"0\" allowfullscreen=\"\"></iframe></p>\n")] | ||
[TestCase("![yandex.ru](https://music.yandex.ru/album/411845/track/4402274)", "<p><iframe src=\"https://music.yandex.ru/iframe/#track/4402274/411845/\" width=\"500\" height=\"281\" class=\"yandex\" frameborder=\"0\"></iframe></p>\n")] | ||
[TestCase("![vimeo](https://vimeo.com/8607834)", "<p><iframe src=\"https://player.vimeo.com/video/8607834\" width=\"500\" height=\"281\" class=\"vimeo\" frameborder=\"0\" allowfullscreen=\"\"></iframe></p>\n")] | ||
[TestCase("![ok.ru](https://ok.ru/video/26870090463)", "<p><iframe src=\"https://ok.ru/videoembed/26870090463\" width=\"500\" height=\"281\" class=\"odnoklassniki\" frameborder=\"0\" allowfullscreen=\"\"></iframe></p>\n")] | ||
[TestCase("![ok.ru](//ok.ru/video/26870090463)", "<p><iframe src=\"https://ok.ru/videoembed/26870090463\" width=\"500\" height=\"281\" class=\"odnoklassniki\" frameborder=\"0\" allowfullscreen=\"\"></iframe></p>\n")] | ||
public void TestBuiltInHosts(string markdown, string expected) | ||
{ | ||
string html = Markdown.ToHtml(markdown, GetPipeline()); | ||
Assert.AreEqual(html, expected); | ||
} | ||
|
||
private class TestHostProvider : IHostProvider | ||
{ | ||
public string Class { get; } = "regex"; | ||
public bool AllowFullScreen { get; } | ||
|
||
public bool TryHandle(Uri mediaUri, bool isSchemaRelative, out string iframeUrl) | ||
{ | ||
iframeUrl = null; | ||
var uri = isSchemaRelative ? "//" + mediaUri.GetComponents(UriComponents.AbsoluteUri & ~UriComponents.Scheme, UriFormat.UriEscaped) : mediaUri.ToString(); | ||
if (!matcher.IsMatch(uri)) | ||
return false; | ||
iframeUrl = matcher.Replace(uri, replacement); | ||
return true; | ||
} | ||
|
||
private Regex matcher; | ||
private string replacement; | ||
|
||
public TestHostProvider(string provider, string replace) | ||
{ | ||
matcher = new Regex(provider); | ||
replacement = replace; | ||
} | ||
} | ||
|
||
[Test] | ||
[TestCase("![p1](https://sample.com/video.mp4)", "<p><iframe src=\"https://example.com/video.mp4\" width=\"500\" height=\"281\" class=\"regex\" frameborder=\"0\"></iframe></p>\n", @"^https?://sample.com/(.+)$", @"https://example.com/$1")] | ||
[TestCase("![p1](//sample.com/video.mp4)", "<p><iframe src=\"https://example.com/video.mp4\" width=\"500\" height=\"281\" class=\"regex\" frameborder=\"0\"></iframe></p>\n", @"^//sample.com/(.+)$", @"https://example.com/$1")] | ||
[TestCase("![p1](https://sample.com/video.mp4)", "<p><iframe src=\"https://example.com/video.mp4?token=aaabbb\" width=\"500\" height=\"281\" class=\"regex\" frameborder=\"0\"></iframe></p>\n", @"^https?://sample.com/(.+)$", @"https://example.com/$1?token=aaabbb")] | ||
public void TestCustomHostProvider(string markdown, string expected, string provider, string replace) | ||
{ | ||
string html = Markdown.ToHtml(markdown, GetPipeline(new MediaOptions | ||
{ | ||
Hosts = | ||
{ | ||
new TestHostProvider(provider, replace), | ||
} | ||
})); | ||
Assert.AreEqual(html, expected); | ||
} | ||
|
||
[Test] | ||
[TestCase("![static mp4](//sample.com/video.mp4)", "<p><video width=\"500\" height=\"281\" controls=\"\"><source type=\"video/mp4\" src=\"//sample.com/video.mp4\"></source></video></p>\n", "")] | ||
[TestCase(@"![youtube.com](https://www.youtube.com/watch?v=mswPy5bt3TQ)", "<p><iframe src=\"https://www.youtube.com/embed/mswPy5bt3TQ\" width=\"500\" height=\"281\" class=\"youtube\" frameborder=\"0\" allowfullscreen=\"\"></iframe></p>\n", "")] | ||
[TestCase("![static mp4](//sample.com/video.mp4)", "<p><video width=\"500\" height=\"281\" controls=\"\" class=\"k\"><source type=\"video/mp4\" src=\"//sample.com/video.mp4\"></source></video></p>\n", "k")] | ||
[TestCase(@"![youtube.com](https://www.youtube.com/watch?v=mswPy5bt3TQ)", "<p><iframe src=\"https://www.youtube.com/embed/mswPy5bt3TQ\" width=\"500\" height=\"281\" class=\"k youtube\" frameborder=\"0\" allowfullscreen=\"\"></iframe></p>\n", "k")] | ||
public void TestCustomClass(string markdown, string expected, string klass) | ||
{ | ||
string html = Markdown.ToHtml(markdown, GetPipeline(new MediaOptions | ||
{ | ||
Class = klass, | ||
})); | ||
Assert.AreEqual(html, expected); | ||
} | ||
} | ||
} |
Oops, something went wrong.