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.