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.
-
2Try googleing. google.com/search?q=markdown+jsSangbok Lee– Sangbok Lee2017-02-11 07:34:30 +00:00Commented Feb 11, 2017 at 7:34
-
2A parser translates an input text into a structured representation of its various syntactic component 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– Thilo2017-02-11 07:37:34 +00:00Commented Feb 11, 2017 at 7:37
2 Answers
A long but incomplete list of Markdown "Parsers" can be found here: https://github.com/markdown/markdown.github.com/wiki/Implementations
However, to call those all "parsers" is a bit of a misnomer. They do "parse" the Markdown, but they also render/compile 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 compiler, written in JavaScript.
It is honest about its function. It both parses and then compiles/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.
Comments
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.