2

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.

2
  • 2
    Try googleing. google.com/search?q=markdown+js Commented Feb 11, 2017 at 7:34
  • 2
    A 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. Commented Feb 11, 2017 at 7:37

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

3 Comments

I don't think transforming into HTML is strictly what a parser does. The transformation would use the output of the parser to produce HTML. The same parser could be used to manipulate the Markdown in other ways. Of course, simple implementations may not really distinguish between these two steps (but then, they are not really parsers, either, I think).
yep, I agree, but in this case, I've never seen a parser which convert markdown to an object... :D
@Thilo, actually the original reference implementation of Markdown was just a series of regex substitutions. The original document was modified in-place and gradually changed from Markdown to HTML. While some more recent implementations do generate a AST (or similar) there are always hacks to make it work as the original behavior/syntax was developed without such an underlying design in mind.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.