Browse Source

Instantiating LinkifyIt lazily, only if needed

pull/747/head
Fabio Spampinato 4 years ago
parent
commit
f7b9ab2a18
  1. 19
      lib/common/utils.js
  2. 6
      lib/index.js

19
lib/common/utils.js

@ -39,6 +39,24 @@ function arrayReplaceAt(src, pos, newElements) {
return [].concat(src.slice(0, pos), newElements, src.slice(pos + 1));
}
// Sets a property whose initial is computed lazily on an target object
function defineLazyProperty(target, property, instantiator) {
var initied = false;
var value;
Object.defineProperty(target, property, {
get: function () {
if (initied) return value;
initied = true;
value = instantiator();
return value;
},
set: function (newValue) {
initied = true;
value = newValue;
}
});
}
////////////////////////////////////////////////////////////////////////////////
function isValidEntityCode(c) {
@ -300,6 +318,7 @@ exports.lib.mdurl = require('mdurl');
exports.lib.ucmicro = require('uc.micro');
exports.assign = assign;
exports.defineLazyProperty = defineLazyProperty;
exports.isString = isString;
exports.has = has;
exports.unescapeMd = unescapeMd;

6
lib/index.js

@ -9,7 +9,6 @@ var Renderer = require('./renderer');
var ParserCore = require('./parser_core');
var ParserBlock = require('./parser_block');
var ParserInline = require('./parser_inline');
var LinkifyIt = require('linkify-it');
var mdurl = require('mdurl');
var punycode = require('punycode');
@ -288,7 +287,10 @@ function MarkdownIt(presetName, options) {
* Used by [linkify](https://github.com/markdown-it/markdown-it/blob/master/lib/rules_core/linkify.js)
* rule.
**/
this.linkify = new LinkifyIt();
utils.defineLazyProperty(this, 'linkify', function () {
var LinkifyIt = require('linkify-it');
return new LinkifyIt();
});
/**
* MarkdownIt#validateLink(url) -> Boolean

Loading…
Cancel
Save