Razor Taghelper Implementation of Markdown to HTML method _ practical Tips

Source: Internet
Author: User
Tags tagname
The following small series for you to share a razor taghelper to achieve markdown to HTML method, with a good reference value, I hope to be helpful to everyone. Let's take a look at it with a little knitting.

Markdown is a markup language that can be written using an ordinary text editor, which can have a certain format for normal text content with simple markup syntax.

Use

Markdown's syntax is concise, easy to learn, and more powerful than plain text, so a lot of people use it to write blogs. The world's most popular blog platform WordPress and large cms such as Joomla, Drupal can be very good support markdown. The blogging platform that uses the Markdown editor completely has ghost and Typecho.

Used to write documentation, and to "README." The file name of MD "is stored under the software directory.

In addition, now that we have a divine editor like Rstudio, we can quickly convert Markdown to PowerPoint presentations, Word product documentation, latex papers and even minimal usable prototypes with very small amounts of code. In the field of data science, Markdown has been established as a scientific research norm, which has greatly advanced the historical process of dynamic repeatability research.

Taghelper

Write a razor taghelper to implement markdown to HTML, which needs to be used to Commonmark. NET this 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 ();  } }}


How to use

The first thing to do is to join this taghelper in _viewimports.cshtml, like this


@addTagHelper *, Zkeacms. Message


And then you can use it directly.


<markdown> @item .commentcontent</markdown>


Above this article Razor Taghelper realize Markdown to HTML method is small to share all the content of everyone, hope to give you a reference, also hope that we support a lot of topic.alibabacloud.com.

Related recommendations:

Detailed description of the ASP. NET Core Razor page Routing

For more information about ASP. NET Core Razor page Routing

Solutions for razor problems in ASP.

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.