Markdown parser, done right. 100% CommonMark support, extensions, syntax plugins & high speed
https://markdown-it.github.io/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.0 KiB
58 lines
1.0 KiB
// Main perser class
|
|
|
|
'use strict';
|
|
|
|
|
|
var State = require('./state');
|
|
var Renderer = require('./renderer');
|
|
var LexerBlock = require('./lexer_block');
|
|
var LexerInline = require('./lexer_inline');
|
|
|
|
|
|
// Main class
|
|
//
|
|
function Parser(options) {
|
|
this.options = {};
|
|
this.state = null;
|
|
|
|
this.lexerInline = new LexerInline();
|
|
this.lexerBlock = new LexerBlock();
|
|
this.renderer = new Renderer();
|
|
|
|
if (options) { this.set(options); }
|
|
}
|
|
|
|
|
|
Parser.prototype.set = function (options) {
|
|
Object.keys(options).forEach(function (key) {
|
|
this.options[key] = options[key];
|
|
}, this);
|
|
};
|
|
|
|
|
|
Parser.prototype.render = function (src) {
|
|
var state;
|
|
|
|
if (!src) { return ''; }
|
|
|
|
state = new State(
|
|
src,
|
|
this.lexerBlock,
|
|
this.lexerInline,
|
|
this.renderer,
|
|
this.options
|
|
);
|
|
|
|
// TODO: skip leading empty lines
|
|
|
|
state.lexerBlock.tokenize(state, state.line, state.lineMax);
|
|
|
|
// TODO: ??? eat empty paragraphs from tail
|
|
|
|
//console.log(state.tokens)
|
|
|
|
return this.renderer.render(state);
|
|
};
|
|
|
|
|
|
module.exports = Parser;
|
|
|