JavaScript parsing and rendering Markdown

There are big brothers once said:

Any application that can be written in JavaScript, will eventually be written in JavaScript.
-- Jeff Atwood

Markdown is already a very popular language the mark, JavaScript parsing and rendering can make it play a greater role in the front-end sector.
For example, to write Markdown blog, for online preview through JavaScript, which is what I'm doing.
JavaScript parser implemented in other languages there is a incomparable advantages, may resolve through the front and rear end with Node.js can achieve the same code, to ensure that the result is exactly the same.
By extension of Markdown syntax, we can do more things, such as using Markdown quickly build a page, or even quickly implement complex interactions through specific syntax, fast embedded in a complex assembly, and so on.

The more popular Markdown parsing library has (according to the number Star arranged from more to less):

I use a bit respectively, chops and a little bit of their implementation, the following several aspects I will compare their advantages and disadvantages.

TL;DR

  • Marked: fastest, poor scalability, Chinese support is good;
  • Showdown: scalability, Chinese support is good;
  • markdown-it: scalability, Chinese support is not good.

Specific analysis

Marked

  1. Design:
    Marked code implements a word, bad.
    This should be an old project, and feel there is no design, all the code in one file, there is no component-based, modular, full screen are regular expressions. This project if the bug occurs, I would not want to troubleshoot.
    Marked entire resolution process are at a big function, the basic can not be extended. Of course, he exposes the analytic method, we can own it implement a replacement, but in that case, I would like this library doing?
  2. ANALYSIS PRINCIPLE:
    As already said, the full screen is a regular expression. Yes, Marked is through regular matches to resolve the Markdown. Reason, use the regular syntax to resolve it is not very reliable, but fortunately Markdown is used to write the document, usually do not appear too complex edge case, so from the results will be accepted (Chinese support good).
    Regular analytical benefits:
    • Development speed. (I will not say maintenance = =!)
    • Good performance. After all, you can optimize the leveraging browser does regular characters resolved a lot faster than you by.
  3. Performance:
    Although the code readability of this library is not very good, but from a different perspective, which in turn is not necessarily a bad thing. First, the code based on ES5, without compiling, no components, modular, reducing the overhead caused by a number of readability, and use a variety of clever but useless, to enhance performance is certainly helpful. From the benchmark point of view, Marked speed is indeed far ahead.

Summary: poor scalability, regular analytic (Chinese support good), good performance.

Showdown

  1. Code implementation:
    This project has a modular concept and the introduction of the extension mechanism, could be rapidly expanded.
    Showdown defines a set of subParser, different syntax can be resolved separately, but also defines a number of life cycle events can be triggered at certain times for some of the acts, the logic is clear, we can easily register their subParser or extension, to extend the new syntax.
    The only thing to worry about is that it's very flexible resolution mechanism to achieve a subParser If there is a problem, it may lead to resolve other subParser is also affected.
  2. ANALYSIS PRINCIPLE:
    Showdown is through regular expressions to parse, but it made a distinction based on different syntax, each subParser is responsible for parsing a syntax, regular expressions and more clear. Using regular analytical merits and Marked similar, do not say. In addition, he's matching rules also apply to the Chinese, the Chinese support is good.
  3. Performance:
    Showdown although also use regular parsing Markdown, but because of its modularity, functional split, the introduction of the life cycle, resulting in decreased efficiency of the resolution, speed is of far less Marked. But the rapid development of today's hardware, C terminal does not care so much about performance, scalability more important.

Summary: scalability, regular analytic (Chinese support good), poor performance.

markdown-it

  1. Code implementation:
    functional split, modular, readable, well documented, well-designed set of ideas, but also provides a comprehensive extension development program.
  2. Analytical principle:
    in strict accordance with CommonMark specification, character by character resolution, accuracy is very reliable.
    However, CommonMark specification designers do not seem to take into account the presence of Chinese and other languages do not use a space-separated, so this is very bad in all languages. The relevant issue is already a lot, but developers have been saying CommonMark specification is so designed, it does not yet support.
    And the details of implementation specifications are basically hard-coded and can not be modified by extending the way, it became the largest currently use this library obstacle.
  3. Performance:
    this course will lose some performance, so it is not as good as Marked parsing speed, but according to official benchmark, probably do some optimization, the performance gap is not large.

Summary: scalability, character by character parsing (Chinese support is not good), better performance.

in conclusion

  • If we do not expand, just want to quickly get a Markdown parsing and rendering tools, then Marked is a good choice.
  • If we want to customize, extend components and custom syntax, then Showdown is a good choice.
  • As for markdown-it, such as its support for the friendly Chinese say it.

Reproduced in: https: //www.jianshu.com/p/549cace91e22

Guess you like

Origin blog.csdn.net/weixin_34320724/article/details/91214588