Browse Source

Don't modify env in inline mode

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
0c26266918
  1. 19
      lib/index.js
  2. 29
      lib/parser_block.js
  3. 4
      lib/rules_core/abbr.js
  4. 14
      lib/rules_core/block.js
  5. 4
      lib/rules_core/references.js
  6. 7
      test/misc.js

19
lib/index.js

@ -20,9 +20,10 @@ var config = {
function StateCore(self, src, env) { function StateCore(self, src, env) {
this.src = src; this.src = src;
this.env = env; this.env = env || {};
this.options = self.options; this.options = self.options;
this.tokens = []; this.tokens = [];
this.inlineMode = false;
this.inline = self.inline; this.inline = self.inline;
this.block = self.block; this.block = self.block;
@ -97,12 +98,7 @@ Remarkable.prototype.use = function (plugin, opts) {
// definitions data. // definitions data.
// //
Remarkable.prototype.parse = function (src, env) { Remarkable.prototype.parse = function (src, env) {
var state; var state = new StateCore(this, src, env);
env = env || {};
env.inlineMode = false;
state = new StateCore(this, src, env);
this.core.process(state); this.core.process(state);
@ -121,13 +117,9 @@ Remarkable.prototype.render = function (src, env) {
// Parse content as single string // Parse content as single string
// //
Remarkable.prototype.parseInline = function (src, env) { Remarkable.prototype.parseInline = function (src, env) {
var state; var state = new StateCore(this, src, env);
env = env || {};
env.inlineMode = true;
state = new StateCore(this, src, env);
state.inlineMode = true;
this.core.process(state); this.core.process(state);
return state.tokens; return state.tokens;
@ -137,7 +129,6 @@ Remarkable.prototype.parseInline = function (src, env) {
// //
Remarkable.prototype.renderInline = function (src, env) { Remarkable.prototype.renderInline = function (src, env) {
env = env || {}; env = env || {};
env.inlineMode = true;
return this.renderer.render(this.parseInline(src, env), this.options, env); return this.renderer.render(this.parseInline(src, env), this.options, env);
}; };

29
lib/parser_block.js

@ -114,26 +114,15 @@ ParserBlock.prototype.parse = function (src, options, env, outTokens) {
}); });
} }
if (env.inlineMode) { state = new StateBlock(
outTokens.push({ src,
type: 'inline', this,
content: src.replace(/\n/g, ' ').trim(), options,
level: 0, env,
lines: [ 0, 1 ], outTokens
children: [] );
});
this.tokenize(state, state.line, state.lineMax);
} else {
state = new StateBlock(
src,
this,
options,
env,
outTokens
);
this.tokenize(state, state.line, state.lineMax);
}
}; };

4
lib/rules_core/abbr.js

@ -40,6 +40,10 @@ function parseAbbr(str, parserInline, options, env) {
module.exports = function abbr(state) { module.exports = function abbr(state) {
var tokens = state.tokens, i, l, content, pos; var tokens = state.tokens, i, l, content, pos;
if (state.inlineMode) {
return;
}
// Parse inlines // Parse inlines
for (i = 1, l = tokens.length - 1; i < l; i++) { for (i = 1, l = tokens.length - 1; i < l; i++) {
if (tokens[i - 1].type === 'paragraph_open' && if (tokens[i - 1].type === 'paragraph_open' &&

14
lib/rules_core/block.js

@ -1,5 +1,17 @@
'use strict'; 'use strict';
module.exports = function block(state) { module.exports = function block(state) {
state.block.parse(state.src, state.options, state.env, state.tokens);
if (state.inlineMode) {
state.tokens.push({
type: 'inline',
content: state.src.replace(/\n/g, ' ').trim(),
level: 0,
lines: [ 0, 1 ],
children: []
});
} else {
state.block.parse(state.src, state.options, state.env, state.tokens);
}
}; };

4
lib/rules_core/references.js

@ -69,6 +69,10 @@ module.exports = function references(state) {
state.env.references = state.env.references || {}; state.env.references = state.env.references || {};
if (state.inlineMode) {
return;
}
// Scan definitions in paragraph inlines // Scan definitions in paragraph inlines
for (i = 1, l = tokens.length - 1; i < l; i++) { for (i = 1, l = tokens.length - 1; i < l; i++) {
if (tokens[i].type === 'inline' && if (tokens[i].type === 'inline' &&

7
test/misc.js

@ -51,12 +51,12 @@ describe('API', function () {
}); });
it('configure coverage', function () { it('configure coverage', function () {
var md = new Remarkable(); var md = new Remarkable('full');
// conditions coverage // conditions coverage
md.configure({}); md.configure({});
md.render('123'); assert.strictEqual(md.render('123'), '<p>123</p>\n');
}); });
it('plugin', function () { it('plugin', function () {
@ -115,7 +115,6 @@ describe('API', function () {
assert.strictEqual(md.render('![]()'), '<p><img src="" alt=""></p>\n'); assert.strictEqual(md.render('![]()'), '<p><img src="" alt=""></p>\n');
assert.strictEqual(md.render('a \\\nb'), '<p>a <br>\nb</p>\n'); assert.strictEqual(md.render('a \\\nb'), '<p>a <br>\nb</p>\n');
}); });
}); });
@ -135,7 +134,7 @@ describe('Misc', function () {
}); });
it('Should parse inlines only', function () { it('Should parse inlines only', function () {
var md = new Remarkable(); var md = new Remarkable('full');
assert.strictEqual(md.renderInline('a *b* c'), 'a <em>b</em> c'); assert.strictEqual(md.renderInline('a *b* c'), 'a <em>b</em> c');
}); });

Loading…
Cancel
Save