Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert Markdown Syntax into Html in CommonMark use

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In this issue, the editor will bring you about how to convert Markdown grammar into Html in the use of CommonMark. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Markdown writing

Since I started blogging in 2016, my writing style has been changing and improving, to be exact, because it is becoming more and more efficient.

At first, it was very bad to write something on CSDN, write something on their editor and then adjust the format, then write again, and when you encounter a picture, you have to insert the picture, adjust the size and position of the picture, etc., and continue to write.

It's very inefficient.

Later, when I learned about Markdown, I switched to MD to write things, which is much faster. Later on the basis of Markdown slowly optimize to find their own way of writing.

Usually when I finish writing with MD grammar, I get a bunch of text with MD symbols, hereinafter referred to as MD text.

Then it will be converted to the corresponding Html markup text through the tool and copied to the blog edit box, which is the last article displayed.

At this point, I don't know if you have noticed, suppose we can convert the MD text into the corresponding Html text through the code, so that as long as we can get the MD text of the article, we will get the final display of the article.

If we want to do a website that automatically produces articles, there is only one step left, how to automatically get MD text, and then we can keep turning it into the corresponding article, and then publish it. If it is all content-based sites, of course, there is no way to keep access to MD text, after all, content-based, a lot of things are not fixed.

But what if the website is a download station? Download station is mainly to provide file download, the content is only a brief introduction to the file, basically Baidu encyclopedia content, the whole article has been fixed, there are certain rules, so it is possible to continue to get MD text.

The above is all about the importance of CommonMark.

CommonMark

It's easy to use. Here I will mainly introduce how to operate some of these tags. You can use examples to customize various other tags. Here I give an example of the customization of two tags: pictures and hyperlinks.

Because the Html that CommonMark converts to by default will not take the size and location of the image. The same is true of the hyperlink. By default, clicking the link will jump to the hyperlink page on this page. I hope that after clicking the hyperlink, it will open another web page in the browser. This will be better for the retention rate of the website.

Maven dependence

Com.atlassian.commonmarkcommonmark0.11.0

Custom tag attribute action class

Static class PAttributeProvider implements AttributeProvider {@ Overridepublic void setAttributes (Node node, String tagName, Map attributes) {if (node instanceof Image) {attributes.put ("style", "width:150px;height:200px;position:relative;left:50%;margin-left:-100px;");} if (node instanceof Link) {attributes.put ("target", "_ blank");}

As above, I manipulated two tags: Image and Link, adding some attributes to the tag. Here I am just an example, do not have to follow this operation, do not have to operate the label is fine.

The purpose of this step is to show that there is such a method.

Convert markdown to html

Parser parser = Parser.builder () .build (); Node document = parser.parse (md); HtmlRenderer renderer = HtmlRenderer.builder () .attributeProviderFactory (new AttributeProviderFactory () {@ Overridepublic AttributeProvider create (AttributeProviderContext attributeProviderContext) {return new PAttributeProvider ();}}) .build (); String mdHtml = renderer.render (document)

Ok, has been converted to html, the method is that simple to use.

The above is the editor for you to share the use of CommonMark how to convert Markdown syntax into Html, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report