diff --git a/lib/common/utils.js b/lib/common/utils.js index b5e59b4..0f31d58 100644 --- a/lib/common/utils.js +++ b/lib/common/utils.js @@ -124,6 +124,11 @@ function escapeHtml(str) { //////////////////////////////////////////////////////////////////////////////// +function arrayReplaceAt(src, pos, newElements) { + return [].concat(src.slice(0, pos), newElements, src.slice(pos + 1)); +} + + exports.assign = assign; exports.isString = isString; exports.has = has; @@ -132,3 +137,4 @@ exports.isValidEntityCode = isValidEntityCode; exports.fromCodePoint = fromCodePoint; exports.replaceEntities = replaceEntities; exports.escapeHtml = escapeHtml; +exports.arrayReplaceAt = arrayReplaceAt; diff --git a/lib/index.js b/lib/index.js index 65a291a..8d7bc68 100644 --- a/lib/index.js +++ b/lib/index.js @@ -3,6 +3,7 @@ 'use strict'; +var utils = require('./common/utils'); var assign = require('./common/utils').assign; var isString = require('./common/utils').isString; var Renderer = require('./renderer'); @@ -51,6 +52,9 @@ function MarkdownIt(presetName, options) { this.renderer = new Renderer(); this.ruler = new Ruler(); + // Expose utils for easy acces from plugins + this.utils = utils; + this.options = {}; this.configure(config[presetName]); diff --git a/lib/rules_core/abbr2.js b/lib/rules_core/abbr2.js index cdc03ce..0a3315c 100644 --- a/lib/rules_core/abbr2.js +++ b/lib/rules_core/abbr2.js @@ -3,6 +3,9 @@ 'use strict'; +var arrayReplaceAt = require('../common/utils').arrayReplaceAt; + + var PUNCT_CHARS = ' \n()[]\'".,!?-'; @@ -82,7 +85,7 @@ module.exports = function abbr2(state) { } // replace current node - blockTokens[j].children = tokens = [].concat(tokens.slice(0, i), nodes, tokens.slice(i + 1)); + blockTokens[j].children = tokens = arrayReplaceAt(tokens, i, nodes); } } }; diff --git a/lib/rules_core/linkify.js b/lib/rules_core/linkify.js index 4bd8625..b8a958c 100644 --- a/lib/rules_core/linkify.js +++ b/lib/rules_core/linkify.js @@ -5,7 +5,8 @@ 'use strict'; -var Autolinker = require('autolinker'); +var Autolinker = require('autolinker'); +var arrayReplaceAt = require('../common/utils').arrayReplaceAt; var LINK_SCAN_RE = /www|@|\:\/\//; @@ -154,7 +155,7 @@ module.exports = function linkify(state) { } // replace current node - blockTokens[j].children = tokens = [].concat(tokens.slice(0, i), nodes, tokens.slice(i + 1)); + blockTokens[j].children = tokens = arrayReplaceAt(tokens, i, nodes); } } }