diff --git a/CHANGELOG.md b/CHANGELOG.md index c1c8d15..0744574 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ - Added `renderInline()` and `parseInline()` methods. - Added abbreviations support. - Fixed problem with tables, having single column. -- Changed internal api, related to Ruler use. +- Changed internal api (ruler, inline, block classes). - Removed typographer chain (rules moved to `core`). - Removed all typographer options. Quote chars defs moved to `options.quotes`. diff --git a/lib/parser_block.js b/lib/parser_block.js index 4e8ef7a..2c58849 100644 --- a/lib/parser_block.js +++ b/lib/parser_block.js @@ -5,7 +5,7 @@ var Ruler = require('./ruler'); -var State = require('./rules_block/state_block'); +var StateBlock = require('./rules_block/state_block'); var _rules = [ @@ -88,7 +88,7 @@ var TABS_SCAN_RE = /[\n\t]/g; var NEWLINES_RE = /\r[\n\u0085]|[\u2424\u2028\u0085]/g; var SPACES_RE = /\u00a0/g; -ParserBlock.prototype.parse = function (src, options, env) { +ParserBlock.prototype.parse = function (src, options, env, outTokens) { var state, lineStart = 0, lastTabPos = 0; if (!src) { return []; } @@ -115,28 +115,25 @@ ParserBlock.prototype.parse = function (src, options, env) { } if (env.inlineMode) { - return [ - { - type: 'inline', - content: src.replace(/\n/g, ' ').trim(), - level: 0, - lines: [ 0, 1 ], - children: [] - } - ]; - } - - state = new State( - src, - this, - [], - options, - env - ); + outTokens.push({ + type: 'inline', + content: src.replace(/\n/g, ' ').trim(), + level: 0, + lines: [ 0, 1 ], + children: [] + }); - this.tokenize(state, state.line, state.lineMax); + } else { + state = new StateBlock( + src, + this, + options, + env, + outTokens + ); - return state.tokens; + this.tokenize(state, state.line, state.lineMax); + } }; diff --git a/lib/parser_inline.js b/lib/parser_inline.js index 6906a43..ad99182 100644 --- a/lib/parser_inline.js +++ b/lib/parser_inline.js @@ -111,17 +111,15 @@ ParserInline.prototype.tokenize = function (state) { if (state.pending) { state.pushPending(); } - - return state.tokens; }; // Parse input string. // -ParserInline.prototype.parse = function (str, options, env) { - var state = new StateInline(str, this, options, env); +ParserInline.prototype.parse = function (str, options, env, outTokens) { + var state = new StateInline(str, this, options, env, outTokens); - return this.tokenize(state); + this.tokenize(state); }; diff --git a/lib/rules_block/state_block.js b/lib/rules_block/state_block.js index 39e8e4e..3dabd50 100644 --- a/lib/rules_block/state_block.js +++ b/lib/rules_block/state_block.js @@ -3,7 +3,7 @@ 'use strict'; -function StateBlock(src, parser, tokens, options, env) { +function StateBlock(src, parser, options, env, tokens) { var ch, s, start, pos, len, indent, indent_found; this.src = src; diff --git a/lib/rules_core/abbr.js b/lib/rules_core/abbr.js index 1ce2976..9ec9467 100644 --- a/lib/rules_core/abbr.js +++ b/lib/rules_core/abbr.js @@ -8,7 +8,7 @@ var StateInline = require('../rules_inline/state_inline'); var parseLinkLabel = require('../helpers/parse_link_label'); -function parseAbbr(str, parser, options, env) { +function parseAbbr(str, parserInline, options, env) { var state, labelEnd, pos, max, label, title; if (str.charCodeAt(0) !== 0x2A/* * */) { return -1; } @@ -16,7 +16,7 @@ function parseAbbr(str, parser, options, env) { if (str.indexOf(']:') === -1) { return -1; } - state = new StateInline(str, parser, options, env); + state = new StateInline(str, parserInline, options, env, []); labelEnd = parseLinkLabel(state, 1); if (labelEnd < 0 || str.charCodeAt(labelEnd + 1) !== 0x3A/* : */) { return -1; } diff --git a/lib/rules_core/block.js b/lib/rules_core/block.js index 01769bc..78134eb 100644 --- a/lib/rules_core/block.js +++ b/lib/rules_core/block.js @@ -1,6 +1,5 @@ 'use strict'; module.exports = function block(state) { - var tokens = state.block.parse(state.src, state.options, state.env); - state.tokens = state.tokens.concat(tokens); + state.block.parse(state.src, state.options, state.env, state.tokens); }; diff --git a/lib/rules_core/inline.js b/lib/rules_core/inline.js index a019de9..8b2280b 100644 --- a/lib/rules_core/inline.js +++ b/lib/rules_core/inline.js @@ -7,7 +7,7 @@ module.exports = function inline(state) { for (i = 0, l = tokens.length; i < l; i++) { tok = tokens[i]; if (tok.type === 'inline') { - tok.children = state.inline.parse(tok.content, state.options, state.env); + state.inline.parse(tok.content, state.options, state.env, tok.children); } } }; diff --git a/lib/rules_core/references.js b/lib/rules_core/references.js index eaa3313..a01336e 100644 --- a/lib/rules_core/references.js +++ b/lib/rules_core/references.js @@ -15,7 +15,7 @@ function parseReference(str, parser, options, env) { if (str.indexOf(']:') === -1) { return -1; } - state = new StateInline(str, parser, options, env); + state = new StateInline(str, parser, options, env, []); labelEnd = parseLinkLabel(state, 0); if (labelEnd < 0 || str.charCodeAt(labelEnd + 1) !== 0x3A/* : */) { return -1; } diff --git a/lib/rules_inline/state_inline.js b/lib/rules_inline/state_inline.js index 5a85bb0..9d134ad 100644 --- a/lib/rules_inline/state_inline.js +++ b/lib/rules_inline/state_inline.js @@ -3,12 +3,12 @@ 'use strict'; -function StateInline(src, parser, options, env) { +function StateInline(src, parserInline, options, env, outTokens) { this.src = src; this.env = env; this.options = options; - this.parser = parser; - this.tokens = []; + this.parser = parserInline; + this.tokens = outTokens; this.pos = 0; this.posMax = this.src.length; this.level = 0;