MarkdownDeep-av.NET => CommonMark.
This commit is contained in:
@ -1,10 +1,11 @@
|
||||
|
||||
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using MarkdownDeep;
|
||||
using Microsoft.AspNet.Mvc.Rendering;
|
||||
using Microsoft.AspNet.Razor.TagHelpers;
|
||||
using CommonMark;
|
||||
using CommonMark.Syntax;
|
||||
using System.IO;
|
||||
|
||||
namespace Yavsc.TagHelpers
|
||||
{
|
||||
@ -32,45 +33,6 @@ namespace Yavsc.TagHelpers
|
||||
[HtmlAttributeName(MarkdownContentAttributeName)]
|
||||
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>
|
||||
@ -79,28 +41,27 @@ namespace Yavsc.TagHelpers
|
||||
/// <param name="text">The Markdown that should be transformed.</param>
|
||||
/// <param name="urlBaseLocation">The url Base Location.</param>
|
||||
/// <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.
|
||||
var markdownTransformer = GetMarkdownTransformer();
|
||||
markdownTransformer.UrlBaseLocation = urlBaseLocation;
|
||||
string html = markdownTransformer.Transform(text);
|
||||
// Wrap the html in an MvcHtmlString otherwise it'll be HtmlEncoded and displayed to the user as HTML :(
|
||||
return html;
|
||||
}
|
||||
string actual;
|
||||
var settings = CommonMarkSettings.Default.Clone();
|
||||
settings.OutputFormat = OutputFormat.Html;
|
||||
// settings.PrologueLineHandler = null;
|
||||
Block document;
|
||||
|
||||
internal Markdown GetMarkdownTransformer()
|
||||
{
|
||||
var markdownTransformer = new Markdown();
|
||||
markdownTransformer.ExtraMode = true;
|
||||
markdownTransformer.NoFollowLinks = true;
|
||||
markdownTransformer.SafeMode = false;
|
||||
markdownTransformer.FormatCodeBlock = FormatCodePrettyPrint;
|
||||
markdownTransformer.ExtractHeadBlocks = true;
|
||||
markdownTransformer.UserBreaks = true;
|
||||
return markdownTransformer;
|
||||
// Act
|
||||
using (var reader = new StringReader(commonMark))
|
||||
using (var writer = new StringWriter())
|
||||
{
|
||||
var prologue = CommonMarkConverter.ProcessPrologue(reader, settings);
|
||||
document = CommonMarkConverter.ProcessStage1(reader, settings, prologue);
|
||||
CommonMarkConverter.ProcessStage2(document, settings);
|
||||
CommonMarkConverter.ProcessStage3(document, writer, settings);
|
||||
actual = writer.ToString();
|
||||
}
|
||||
return actual;
|
||||
}
|
||||
|
||||
public ModelExpression Content { get; set; }
|
||||
|
||||
public async override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
|
||||
|
@ -75,6 +75,7 @@
|
||||
"defaultNamespace": "Yavsc"
|
||||
},
|
||||
"dependencies": {
|
||||
"CommonMark": "0.11.0",
|
||||
"EntityFramework.Commands": "7.0.0-rc1-final",
|
||||
"EntityFramework.Core": "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.Design": "3.1.0-rc1-5",
|
||||
"MailKit": "1.12.0",
|
||||
"MarkdownDeep-av.NET": "1.5.8",
|
||||
"Microsoft.AspNet.Authentication.Cookies": "1.0.0-rc1-final",
|
||||
"Microsoft.AspNet.Authentication.Facebook": "1.0.0-rc1-final",
|
||||
"Microsoft.AspNet.Authentication.Twitter": "1.0.0-rc1-final",
|
||||
|
Reference in New Issue
Block a user