Retour à MarkdownDeep-Av
CommonMarks souffre des différence suivantes : * pas de support des barrés * pas de tables De plus, le support des vidéos et fichiers audio restaient à implémenter.
This commit is contained in:
@ -1,11 +1,10 @@
|
||||
|
||||
|
||||
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
|
||||
{
|
||||
@ -33,6 +32,45 @@ 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>
|
||||
@ -41,28 +79,28 @@ 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 commonMark, string urlBaseLocation = "")
|
||||
public string Markdown(string text, string urlBaseLocation = "")
|
||||
{
|
||||
// Transform the supplied text (Markdown) into HTML.
|
||||
string actual;
|
||||
var settings = CommonMarkSettings.Default.Clone();
|
||||
settings.OutputFormat = OutputFormat.Html;
|
||||
settings.AdditionalFeatures |= CommonMarkAdditionalFeatures.StrikethroughTilde;
|
||||
|
||||
Block document;
|
||||
|
||||
// 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;
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public ModelExpression Content { get; set; }
|
||||
|
||||
public async override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
|
||||
|
@ -75,7 +75,7 @@
|
||||
"defaultNamespace": "Yavsc"
|
||||
},
|
||||
"dependencies": {
|
||||
"ya.CommonMark.NET": "0.11.0",
|
||||
"MarkdownDeep-av.NET": "1.5.26",
|
||||
"EntityFramework.Commands": "7.0.0-rc1-final",
|
||||
"EntityFramework.Core": "7.0.0-rc1-final",
|
||||
"EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
|
||||
|
Reference in New Issue
Block a user