Browse Source

Both helpers & utils are not in each instance of main parser

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
a5a04532df
  1. 7
      CHANGELOG.md
  2. 21
      lib/common/utils.js
  3. 7
      lib/helpers/index.js
  4. 17
      lib/helpers/normalize_link.js
  5. 3
      lib/helpers/normalize_reference.js
  6. 2
      lib/helpers/parse_link_destination.js
  7. 7
      lib/index.js
  8. 2
      lib/rules_inline/autolink.js
  9. 3
      test/misc.js

7
CHANGELOG.md

@ -1,3 +1,10 @@
2.x.x / WIP
------------------
- Exposed helpers into parser instances (for plugins).
- Removed utils from global export - been in instances seems enougth.
2.1.1 / 2014-12-22 2.1.1 / 2014-12-22
------------------ ------------------

21
lib/common/utils.js

@ -33,6 +33,12 @@ function assign(obj /*from1, from2, from3, ...*/) {
return obj; return obj;
} }
// Remove element from array and put another array at those position.
// Useful for some operations with tokens
function arrayReplaceAt(src, pos, newElements) {
return [].concat(src.slice(0, pos), newElements, src.slice(pos + 1));
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
var UNESCAPE_MD_RE = /\\([\\!"#$%&'()*+,.\/:;<=>?@[\]^_`{|}~-])/g; var UNESCAPE_MD_RE = /\\([\\!"#$%&'()*+,.\/:;<=>?@[\]^_`{|}~-])/g;
@ -124,8 +130,18 @@ function escapeHtml(str) {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
function arrayReplaceAt(src, pos, newElements) { // Incoming link can be partially encoded. Convert possible combinations to
return [].concat(src.slice(0, pos), newElements, src.slice(pos + 1)); // unified form.
function normalizeLink(url) {
var normalized = replaceEntities(url);
// We don't care much about result of mailformed URIs,
// but shoud not throw exception.
try {
normalized = decodeURI(normalized);
} catch (__) {}
return encodeURI(normalized);
} }
@ -138,3 +154,4 @@ exports.fromCodePoint = fromCodePoint;
exports.replaceEntities = replaceEntities; exports.replaceEntities = replaceEntities;
exports.escapeHtml = escapeHtml; exports.escapeHtml = escapeHtml;
exports.arrayReplaceAt = arrayReplaceAt; exports.arrayReplaceAt = arrayReplaceAt;
exports.normalizeLink = normalizeLink;

7
lib/helpers/index.js

@ -0,0 +1,7 @@
// Just a shortcut for bulk export
'use strict';
exports.parseLinkLabel = require('./parse_link_label');
exports.parseLinkDestination = require('./parse_link_destination');
exports.parseLinkTitle = require('./parse_link_title');

17
lib/helpers/normalize_link.js

@ -1,17 +0,0 @@
'use strict';
var replaceEntities = require('../common/utils').replaceEntities;
module.exports = function normalizeLink(url) {
var normalized = replaceEntities(url);
// We don't care much about result of mailformed URIs,
// but shoud not throw exception.
try {
normalized = decodeURI(normalized);
} catch (__) {}
return encodeURI(normalized);
};

3
lib/helpers/normalize_reference.js

@ -1,5 +1,8 @@
'use strict'; 'use strict';
// Hepler to [reference labels]. No better place for this code :)
// It's only for refs/links and should not be exported anywhere.
module.exports = function normalizeReference(str) { module.exports = function normalizeReference(str) {
// use .toUpperCase() instead of .toLowerCase() // use .toUpperCase() instead of .toLowerCase()
// here to avoid a conflict with Object.prototype // here to avoid a conflict with Object.prototype

2
lib/helpers/parse_link_destination.js

@ -6,7 +6,7 @@
'use strict'; 'use strict';
var normalizeLink = require('./normalize_link'); var normalizeLink = require('../common/utils').normalizeLink;
var unescapeMd = require('../common/utils').unescapeMd; var unescapeMd = require('../common/utils').unescapeMd;

7
lib/index.js

@ -4,6 +4,7 @@
var utils = require('./common/utils'); var utils = require('./common/utils');
var helpers = require('./helpers');
var assign = require('./common/utils').assign; var assign = require('./common/utils').assign;
var isString = require('./common/utils').isString; var isString = require('./common/utils').isString;
var Renderer = require('./renderer'); var Renderer = require('./renderer');
@ -52,8 +53,9 @@ function MarkdownIt(presetName, options) {
this.renderer = new Renderer(); this.renderer = new Renderer();
this.ruler = new Ruler(); this.ruler = new Ruler();
// Expose utils for easy acces from plugins // Expose utils & helpers for easy acces from plugins
this.utils = utils; this.utils = utils;
this.helpers = helpers;
this.options = {}; this.options = {};
this.configure(config[presetName]); this.configure(config[presetName]);
@ -163,6 +165,3 @@ MarkdownIt.prototype.renderInline = function (src, env) {
module.exports = MarkdownIt; module.exports = MarkdownIt;
// Expose helpers, useful for custom renderer functions
module.exports.utils = require('./common/utils');

2
lib/rules_inline/autolink.js

@ -3,7 +3,7 @@
'use strict'; 'use strict';
var url_schemas = require('../common/url_schemas'); var url_schemas = require('../common/url_schemas');
var normalizeLink = require('../helpers/normalize_link'); var normalizeLink = require('../common/utils').normalizeLink;
/*eslint max-len:0*/ /*eslint max-len:0*/

3
test/misc.js

@ -3,7 +3,6 @@
var assert = require('assert'); var assert = require('assert');
var markdownit = require('../'); var markdownit = require('../');
var utils = require('../').utils;
describe('Utils', function () { describe('Utils', function () {
@ -222,7 +221,7 @@ describe('Custom fences', function () {
md.renderer.rules.fence_custom.foo = function (tokens, idx, options, env, self) { md.renderer.rules.fence_custom.foo = function (tokens, idx, options, env, self) {
return '<div class="foo">' + return '<div class="foo">' +
utils.escapeHtml(tokens[idx].content) + md.utils.escapeHtml(tokens[idx].content) +
'</div>' + self.getBreak(tokens, idx); '</div>' + self.getBreak(tokens, idx);
}; };

Loading…
Cancel
Save