Browse Source

Added inlineRender & inlineParse methods

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
615d8cdb8a
  1. 2
      CHANGELOG.md
  2. 36
      lib/index.js
  3. 12
      lib/parser_block.js
  4. 8
      lib/rules_core/references.js
  5. 12
      test/misc.js

2
CHANGELOG.md

@ -2,11 +2,13 @@
------------------ ------------------
- Added `core` chain, to better organize code and improve pluggability. - Added `core` chain, to better organize code and improve pluggability.
- Added `renderInline()` and `parseInline()` methods.
- Fixed problem with tables, having single column. - Fixed problem with tables, having single column.
- Changed internal api, related to Ruler use. - Changed internal api, related to Ruler use.
- Removed typographer chain (rules moved to `core`) - Removed typographer chain (rules moved to `core`)
- Removed all typographer options. Quote chars defs moved to `options.quotes`. - Removed all typographer options. Quote chars defs moved to `options.quotes`.
1.3.0 / 2014-10-29 1.3.0 / 2014-10-29
------------------ ------------------

36
lib/index.js

@ -97,7 +97,12 @@ Remarkable.prototype.use = function (plugin, opts) {
// definitions data. // definitions data.
// //
Remarkable.prototype.parse = function (src, env) { Remarkable.prototype.parse = function (src, env) {
var state = new StateCore(this, src, env); var state;
env = env || {};
env.inlineMode = false;
state = new StateCore(this, src, env);
this.core.process(state); this.core.process(state);
@ -106,11 +111,36 @@ Remarkable.prototype.parse = function (src, env) {
// Main method that does all magic :) // Main method that does all magic :)
// //
Remarkable.prototype.render = function (src) { Remarkable.prototype.render = function (src, env) {
var env = { references: {} }; env = env || {};
return this.renderer.render(this.parse(src, env), this.options, env); return this.renderer.render(this.parse(src, env), this.options, env);
}; };
// Parse content as single string
//
Remarkable.prototype.parseInline = function (src, env) {
var state;
env = env || {};
env.inlineMode = true;
state = new StateCore(this, src, env);
this.core.process(state);
return state.tokens;
};
// Render single string, without wrapping it to paragraphs
//
Remarkable.prototype.renderInline = function (src, env) {
env = env || {};
env.inlineMode = true;
return this.renderer.render(this.parseInline(src, env), this.options, env);
};
module.exports = Remarkable; module.exports = Remarkable;

12
lib/parser_block.js

@ -119,6 +119,18 @@ 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( state = new State(
src, src,
this, this,

8
lib/rules_core/references.js

@ -5,10 +5,12 @@ var parseRef = require('../parser_ref');
module.exports = function references(state) { module.exports = function references(state) {
var tokens = state.tokens, i, l, content, pos; var tokens = state.tokens, i, l, content, pos;
// Parse inlines state.env.references = state.env.references || {};
// 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 - 1].type === 'paragraph_open' && if (tokens[i].type === 'inline' &&
tokens[i].type === 'inline' && tokens[i - 1].type === 'paragraph_open' &&
tokens[i + 1].type === 'paragraph_close') { tokens[i + 1].type === 'paragraph_close') {
content = tokens[i].content; content = tokens[i].content;

12
test/misc.js

@ -8,14 +8,14 @@ var Remarkable = require('../');
describe('Utils', function () { describe('Utils', function () {
it('utils.fromCodePoint', function () { it('fromCodePoint', function () {
var fromCodePoint = require('../lib/common/utils').fromCodePoint; var fromCodePoint = require('../lib/common/utils').fromCodePoint;
assert.strictEqual(fromCodePoint(0x20), ' '); assert.strictEqual(fromCodePoint(0x20), ' ');
assert.strictEqual(fromCodePoint(0x1F601), '😁'); assert.strictEqual(fromCodePoint(0x1F601), '😁');
}); });
it('utils.isValidEntityCode', function () { it('isValidEntityCode', function () {
var isValidEntityCode = require('../lib/common/utils').isValidEntityCode; var isValidEntityCode = require('../lib/common/utils').isValidEntityCode;
assert.strictEqual(isValidEntityCode(0x20), true); assert.strictEqual(isValidEntityCode(0x20), true);
@ -29,7 +29,7 @@ describe('Utils', function () {
assert.strictEqual(isValidEntityCode(0x7F), false); assert.strictEqual(isValidEntityCode(0x7F), false);
}); });
it('utils.assign', function () { it('assign', function () {
var assign = require('../lib/common/utils').assign; var assign = require('../lib/common/utils').assign;
assert.deepEqual(assign({ a: 1 }, null, { b: 2 }), { a: 1, b: 2 }); assert.deepEqual(assign({ a: 1 }, null, { b: 2 }), { a: 1, b: 2 });
@ -90,6 +90,12 @@ describe('Misc', function () {
assert.strictEqual(md.render(''), ''); assert.strictEqual(md.render(''), '');
}); });
it('Should parse inlines only', function () {
var md = new Remarkable();
assert.strictEqual(md.renderInline('a *b* c'), 'a <em>b</em> c');
});
}); });

Loading…
Cancel
Save