Razor Taghelper implement markdown to HTML

Source: Internet
Author: User

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)] [HtmlTargetEleme NT (Attributes = "markdown")] public class Markdowntaghelper:taghelper {public modelexpression Content {GE T 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) ret Urn (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>

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

Razor Taghelper implement markdown to HTML

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.