最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is a markdown parser? How to use it while building a markdown editor? - Stack Overflow

programmeradmin0浏览0评论

I have started to build a markdown editor using javascript from scratch. First I studied the syntax of markdown and what is it about. Then someone asked me to use a markdown parser. I dont really understand it does and how to use it after many searches. Any help would be beneficial. Thanks in advance.

I have started to build a markdown editor using javascript from scratch. First I studied the syntax of markdown and what is it about. Then someone asked me to use a markdown parser. I dont really understand it does and how to use it after many searches. Any help would be beneficial. Thanks in advance.

Share Improve this question asked Feb 11, 2017 at 7:31 sugandh goyalsugandh goyal 3232 gold badges6 silver badges14 bronze badges 2
  • 2 Try googleing. google./search?q=markdown+js – Sangbok Lee Commented Feb 11, 2017 at 7:34
  • 2 A parser translates an input text into a structured representation of its various syntactic ponent parts. For example, an HTML or XML parser can produce a DOM, which is a tree structure of all the nodes in the input document. A markdown parser would do something similar. – Thilo Commented Feb 11, 2017 at 7:37
Add a ment  | 

2 Answers 2

Reset to default 4

A long but inplete list of Markdown "Parsers" can be found here: https://github./markdown/markdown.github./wiki/Implementations

However, to call those all "parsers" is a bit of a misnomer. They do "parse" the Markdown, but they also render/pile the Markdown into something else, usually HTML. As this question is tagged [javascript], we'll use a JavaScript library as an example. The Marked library has the following tag line:

A full-featured markdown parser and piler, written in JavaScript.

It is honest about its function. It both parses and then piles/renders the output as HTML. In fact, the simple use case given in the documentation is:

var marked = require('marked');
console.log(marked('I am using __markdown__.'));
// Outputs: <p>I am using <strong>markdown</strong>.</p>

You pass in a Markdown text string and it returns a string of HTML. But when you read through the documentation, you find a section for Pro level use which explains that "[y]ou also have direct access to the lexer and parser if you so desire." Note the example:

$ node
require('marked').lexer('> i am using marked.')
[ { type: 'blockquote_start' },
  { type: 'paragraph',
    text: 'i am using marked.' },
  { type: 'blockquote_end' },
  links: {} ]

Given a Markdown text string, the "lexer" returns a list of tokens. It is now up to you to determine how those tokens are used.

How to use that to build a Markdown editor is beyond the scope of this forum, I'm afraid.

This is not really a question about a programming, but you seem to be nice, and it might help other people...

A markdown parser is a library (a or some scripts) that are going to parse, in this case, markdown. Markdown is often transformed into HTML.

So, a markdown parser transforms markdown into html.

So, with a markdown parser, you'd just have to do something like this:

 html = parseMarkdown(markdown_code)

And you're done. You don't have to parse the markdown yourself.

发布评论

评论列表(0)

  1. 暂无评论