Razor TagHelper implements Markdown to HTML and taghelpermarkdown

Source: Internet
Author: User

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.


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.


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();        }    }}

First_ ViewImports. cshtmlAdd this TagHelper, like this

@addTagHelper *, ZKEACMS.Message

Then you can use it directly.


Address: http://www.zkea.net/codesnippet/detail/post-79

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.