MarkdownDeep-av.NET => CommonMark.

vnext
Paul Schneider 5 years ago
parent fe67a34e2d
commit 7407f08223
2 changed files with 22 additions and 61 deletions

@ -1,10 +1,11 @@
using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using MarkdownDeep;
using Microsoft.AspNet.Mvc.Rendering; using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.AspNet.Razor.TagHelpers; using Microsoft.AspNet.Razor.TagHelpers;
using CommonMark;
using CommonMark.Syntax;
using System.IO;
namespace Yavsc.TagHelpers namespace Yavsc.TagHelpers
{ {
@ -32,45 +33,6 @@ namespace Yavsc.TagHelpers
[HtmlAttributeName(MarkdownContentAttributeName)] [HtmlAttributeName(MarkdownContentAttributeName)]
public string MarkdownContent { get; set; } public string MarkdownContent { get; set; }
static Regex rxExtractLanguage = new Regex("^({{(.+)}}[\r\n])", RegexOptions.Compiled);
private static string FormatCodePrettyPrint(MarkdownDeep.Markdown m, string code)
{
// Try to extract the language from the first line
var match = rxExtractLanguage.Match(code);
string language = null;
if (match.Success)
{
// Save the language
var g = (Group)match.Groups[2];
language = g.ToString();
// Remove the first line
code = code.Substring(match.Groups[1].Length);
}
// If not specified, look for a link definition called "default_syntax" and
// grab the language from its title
if (language == null)
{
var d = m.GetLinkDefinition("default_syntax");
if (d != null)
language = d.title;
}
// Common replacements
if (language == "C#")
language = "csharp";
if (language == "C++")
language = "cpp";
// Wrap code in pre/code tags and add PrettyPrint attributes if necessary
if (string.IsNullOrEmpty(language))
return string.Format("<pre><code>{0}</code></pre>\n", code);
else
return string.Format("<pre class=\"prettyprint lang-{0}\"><code>{1}</code></pre>\n",
language.ToLowerInvariant(), code);
}
/// <summary> /// <summary>
@ -79,28 +41,27 @@ namespace Yavsc.TagHelpers
/// <param name="text">The Markdown that should be transformed.</param> /// <param name="text">The Markdown that should be transformed.</param>
/// <param name="urlBaseLocation">The url Base Location.</param> /// <param name="urlBaseLocation">The url Base Location.</param>
/// <returns>The HTML representation of the supplied Markdown.</returns> /// <returns>The HTML representation of the supplied Markdown.</returns>
public string Markdown(string text, string urlBaseLocation = "") public string Markdown(string commonMark, string urlBaseLocation = "")
{ {
// Transform the supplied text (Markdown) into HTML. // Transform the supplied text (Markdown) into HTML.
var markdownTransformer = GetMarkdownTransformer(); string actual;
markdownTransformer.UrlBaseLocation = urlBaseLocation; var settings = CommonMarkSettings.Default.Clone();
string html = markdownTransformer.Transform(text); settings.OutputFormat = OutputFormat.Html;
// Wrap the html in an MvcHtmlString otherwise it'll be HtmlEncoded and displayed to the user as HTML :( // settings.PrologueLineHandler = null;
return html; Block document;
}
// Act
internal Markdown GetMarkdownTransformer() using (var reader = new StringReader(commonMark))
using (var writer = new StringWriter())
{ {
var markdownTransformer = new Markdown(); var prologue = CommonMarkConverter.ProcessPrologue(reader, settings);
markdownTransformer.ExtraMode = true; document = CommonMarkConverter.ProcessStage1(reader, settings, prologue);
markdownTransformer.NoFollowLinks = true; CommonMarkConverter.ProcessStage2(document, settings);
markdownTransformer.SafeMode = false; CommonMarkConverter.ProcessStage3(document, writer, settings);
markdownTransformer.FormatCodeBlock = FormatCodePrettyPrint; actual = writer.ToString();
markdownTransformer.ExtractHeadBlocks = true; }
markdownTransformer.UserBreaks = true; return actual;
return markdownTransformer;
} }
public ModelExpression Content { get; set; } public ModelExpression Content { get; set; }
public async override Task ProcessAsync(TagHelperContext context, TagHelperOutput output) public async override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)

@ -75,6 +75,7 @@
"defaultNamespace": "Yavsc" "defaultNamespace": "Yavsc"
}, },
"dependencies": { "dependencies": {
"CommonMark": "0.11.0",
"EntityFramework.Commands": "7.0.0-rc1-final", "EntityFramework.Commands": "7.0.0-rc1-final",
"EntityFramework.Core": "7.0.0-rc1-final", "EntityFramework.Core": "7.0.0-rc1-final",
"EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final", "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
@ -82,7 +83,6 @@
"EntityFramework7.Npgsql": "3.1.0-rc1-3", "EntityFramework7.Npgsql": "3.1.0-rc1-3",
"EntityFramework7.Npgsql.Design": "3.1.0-rc1-5", "EntityFramework7.Npgsql.Design": "3.1.0-rc1-5",
"MailKit": "1.12.0", "MailKit": "1.12.0",
"MarkdownDeep-av.NET": "1.5.8",
"Microsoft.AspNet.Authentication.Cookies": "1.0.0-rc1-final", "Microsoft.AspNet.Authentication.Cookies": "1.0.0-rc1-final",
"Microsoft.AspNet.Authentication.Facebook": "1.0.0-rc1-final", "Microsoft.AspNet.Authentication.Facebook": "1.0.0-rc1-final",
"Microsoft.AspNet.Authentication.Twitter": "1.0.0-rc1-final", "Microsoft.AspNet.Authentication.Twitter": "1.0.0-rc1-final",

Loading…