Razor TagHelper implements Markdown to HTML and taghelpermarkdown
Markdown is a markup language that can be written in a common text editor. Through simple markup syntax, Markdown can make the common text content have a certain format.
Purpose
Markdown has simple syntax, easy to learn, and more powerful functions than plain text, so many people use it to write blogs. The world's most popular blog platforms WordPress and large CMS such as Joomla and Drupal all support Markdown well. The blog platforms fully adopt the Markdown Editor include Ghost and Typecho.
It is used to write instructions and save the file name "README. MD" in the Software Directory.
In addition, because we now have a God-level editor like RStudio, we can also quickly convert Markdown into a presentation PPT, Word product documentation, LaTex paper, or even a very small amount of code to complete the minimum available prototype. In the field of data science, Markdown has been established as a scientific research specification, greatly promoting the historical process of dynamic repeatability research.
TagHelper
Write a Razor TagHelper to convert Markdown to HTML. here we need to use the CommonMark. NET class library.
namespace ZKEACMS.Message.TagHelps{ [HtmlTargetElement("markdown", TagStructure = TagStructure.NormalOrSelfClosing)] [HtmlTargetElement(Attributes = "markdown")] public class MarkdownTagHelper : TagHelper { public ModelExpression Content { get; set; } public async override Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { if (output.TagName == "markdown") { output.TagName = null; } output.Attributes.RemoveAll("markdown"); var content = await GetContent(output); var markdown = WebUtility.HtmlEncode(WebUtility.HtmlDecode(content)); var html = CommonMarkConverter.Convert(markdown); output.Content.SetHtmlContent(html ?? ""); } private async Task GetContent(TagHelperOutput output) { if (Content == null) return (await output.GetChildContentAsync()).GetContent(); return Content.Model?.ToString(); } }}
Usage
First_ ViewImports. cshtmlAdd this TagHelper, like this
@addTagHelper *, ZKEACMS.Message
Then you can use it directly.
<markdown>@item.CommentContent</markdown>
Address: http://www.zkea.net/codesnippet/detail/post-79