Razor TagHelper's method of converting Markdown to HTML_Practical tips

韦小宝
Release: 2017-12-16 09:23:55
Original
1770 people have browsed it

The editor below will share with you an article on Razor TagHelper's method of converting Markdown to HTML. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor and take a look.

Markdown is a markup language that can be written using ordinary texteditor. Through simple markup syntax, it can make ordinary text content have a certain Format.

Purpose

Markdown’s syntax is concise and clear, easy to learn, and its functions are stronger than plain text, so many people use it to write blogs . The world's most popular blog platform WordPress and large CMS such as Joomla and Drupal can support Markdown very well. Blog platforms that fully use Markdown editors include Ghost and Typecho.

is used to write documentation and is saved under the directory of the software with the file name of "README.MD".

In addition, now that we have a god-level editor like RStudio, we can also quickly convert Markdown into speech PPT, Word product documents, LaTex papers, and even complete the minimum with a very small amount of code. Prototypes available. In the field of data science, Markdown has been established as a scientific research norm, greatly advancing the historical process of dynamic reproducibility research.

TagHelper

Write a Razor TagHelper to convert Markdown to HTML. You need to useCommonMark. NETthisClass 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(); } } }
Copy after login


How to use

First of all, in _ViewImports .cshtml add this TagHelper, like this


@addTagHelper *, ZKEACMS.Message
Copy after login


Then you can use it directly


@item.CommentContent
Copy after login


The above method of converting Markdown to HTML with Razor TagHelper is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support PHP Chinese. net.

Related recommendations:

Detailed introduction to ASP.NET Core Razor page routing

Detailed introduction to ASP.NET Core Razor page routing

Solutions to Razor issues in Asp.net MVC

The above is the detailed content of Razor TagHelper's method of converting Markdown to HTML_Practical tips. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!