From a5a04532df87f732129504ad381ad10dd6a62734 Mon Sep 17 00:00:00 2001 From: Vitaly Puzrin Date: Mon, 22 Dec 2014 21:37:22 +0300 Subject: [PATCH] Both helpers & utils are not in each instance of main parser --- CHANGELOG.md | 7 +++++++ lib/common/utils.js | 21 +++++++++++++++++++-- lib/helpers/index.js | 7 +++++++ lib/helpers/normalize_link.js | 17 ----------------- lib/helpers/normalize_reference.js | 3 +++ lib/helpers/parse_link_destination.js | 2 +- lib/index.js | 7 +++---- lib/rules_inline/autolink.js | 2 +- test/misc.js | 3 +-- 9 files changed, 42 insertions(+), 27 deletions(-) create mode 100644 lib/helpers/index.js delete mode 100644 lib/helpers/normalize_link.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 86a5aae..31e1aa2 100644 --- a/CHANGELOG.md +++ b/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 ------------------ diff --git a/lib/common/utils.js b/lib/common/utils.js index 0f31d58..6b9756e 100644 --- a/lib/common/utils.js +++ b/lib/common/utils.js @@ -33,6 +33,12 @@ function assign(obj /*from1, from2, from3, ...*/) { 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; @@ -124,8 +130,18 @@ function escapeHtml(str) { //////////////////////////////////////////////////////////////////////////////// -function arrayReplaceAt(src, pos, newElements) { - return [].concat(src.slice(0, pos), newElements, src.slice(pos + 1)); +// Incoming link can be partially encoded. Convert possible combinations to +// 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.escapeHtml = escapeHtml; exports.arrayReplaceAt = arrayReplaceAt; +exports.normalizeLink = normalizeLink; diff --git a/lib/helpers/index.js b/lib/helpers/index.js new file mode 100644 index 0000000..bfdbfa2 --- /dev/null +++ b/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'); diff --git a/lib/helpers/normalize_link.js b/lib/helpers/normalize_link.js deleted file mode 100644 index 2781f88..0000000 --- a/lib/helpers/normalize_link.js +++ /dev/null @@ -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); -}; diff --git a/lib/helpers/normalize_reference.js b/lib/helpers/normalize_reference.js index 30e3129..26b9215 100644 --- a/lib/helpers/normalize_reference.js +++ b/lib/helpers/normalize_reference.js @@ -1,5 +1,8 @@ '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) { // use .toUpperCase() instead of .toLowerCase() // here to avoid a conflict with Object.prototype diff --git a/lib/helpers/parse_link_destination.js b/lib/helpers/parse_link_destination.js index aece26c..3f35da4 100644 --- a/lib/helpers/parse_link_destination.js +++ b/lib/helpers/parse_link_destination.js @@ -6,7 +6,7 @@ 'use strict'; -var normalizeLink = require('./normalize_link'); +var normalizeLink = require('../common/utils').normalizeLink; var unescapeMd = require('../common/utils').unescapeMd; diff --git a/lib/index.js b/lib/index.js index 8d7bc68..a715578 100644 --- a/lib/index.js +++ b/lib/index.js @@ -4,6 +4,7 @@ var utils = require('./common/utils'); +var helpers = require('./helpers'); var assign = require('./common/utils').assign; var isString = require('./common/utils').isString; var Renderer = require('./renderer'); @@ -52,8 +53,9 @@ function MarkdownIt(presetName, options) { this.renderer = new Renderer(); this.ruler = new Ruler(); - // Expose utils for easy acces from plugins + // Expose utils & helpers for easy acces from plugins this.utils = utils; + this.helpers = helpers; this.options = {}; this.configure(config[presetName]); @@ -163,6 +165,3 @@ MarkdownIt.prototype.renderInline = function (src, env) { module.exports = MarkdownIt; - -// Expose helpers, useful for custom renderer functions -module.exports.utils = require('./common/utils'); diff --git a/lib/rules_inline/autolink.js b/lib/rules_inline/autolink.js index e63b9aa..5350f7d 100644 --- a/lib/rules_inline/autolink.js +++ b/lib/rules_inline/autolink.js @@ -3,7 +3,7 @@ 'use strict'; var url_schemas = require('../common/url_schemas'); -var normalizeLink = require('../helpers/normalize_link'); +var normalizeLink = require('../common/utils').normalizeLink; /*eslint max-len:0*/ diff --git a/test/misc.js b/test/misc.js index 1fe5e5b..6d5a1a2 100644 --- a/test/misc.js +++ b/test/misc.js @@ -3,7 +3,6 @@ var assert = require('assert'); var markdownit = require('../'); -var utils = require('../').utils; describe('Utils', function () { @@ -222,7 +221,7 @@ describe('Custom fences', function () { md.renderer.rules.fence_custom.foo = function (tokens, idx, options, env, self) { return '
' + - utils.escapeHtml(tokens[idx].content) + + md.utils.escapeHtml(tokens[idx].content) + '
' + self.getBreak(tokens, idx); };