diff --git a/lib/index.js b/lib/index.js index 10733d4..2d73e24 100644 --- a/lib/index.js +++ b/lib/index.js @@ -8,6 +8,7 @@ var Renderer = require('./renderer'); var ParserBlock = require('./parser_block'); var ParserInline = require('./parser_inline'); var Typographer = require('./typographer'); +var Linkifier = require('./linkifier'); var defaults = require('./defaults/remarkable'); @@ -25,12 +26,7 @@ function Remarkable(options) { this.block = new ParserBlock(); this.renderer = new Renderer(); this.typographer = new Typographer(); - this.linkifier = new Typographer(); - - // Linkifier is a separate typographer, for convenience. - // Configure it here. - this.linkifier.ruler.enable([], true); - this.linkifier.ruler.after(require('./rules_typographer/linkify')); + this.linkifier = new Linkifier(); // Cross-references to simplify code (a bit dirty, but easy). this.block.inline = this.inline; diff --git a/lib/linkifier.js b/lib/linkifier.js new file mode 100644 index 0000000..19ad529 --- /dev/null +++ b/lib/linkifier.js @@ -0,0 +1,49 @@ +// Class of link replacement rules +// +'use strict'; + + +var assign = require('./common/utils').assign; +var Ruler = require('./ruler'); + + +var rules = [ + require('./rules_text/linkify') +]; + + +function Linkifier() { + this._rules = []; + + this.options = {}; + + this.ruler = new Ruler(this.rulesUpdate.bind(this)); + + for (var i = 0; i < rules.length; i++) { + this.ruler.after(rules[i]); + } +} + + +Linkifier.prototype.rulesUpdate = function () { + this._rules = this.ruler.getRules(); +}; + + +Linkifier.prototype.set = function (options) { + assign(this.options, options); +}; + + +Linkifier.prototype.process = function (state) { + var i, l, rules; + + rules = this._rules; + + for (i = 0, l = rules.length; i < l; i++) { + rules[i](this, state); + } +}; + + +module.exports = Linkifier; diff --git a/lib/rules_typographer/linkify.js b/lib/rules_text/linkify.js similarity index 100% rename from lib/rules_typographer/linkify.js rename to lib/rules_text/linkify.js diff --git a/lib/rules_typographer/replace.js b/lib/rules_text/replace.js similarity index 100% rename from lib/rules_typographer/replace.js rename to lib/rules_text/replace.js diff --git a/lib/rules_typographer/smartquotes.js b/lib/rules_text/smartquotes.js similarity index 100% rename from lib/rules_typographer/smartquotes.js rename to lib/rules_text/smartquotes.js diff --git a/lib/typographer.js b/lib/typographer.js index ec1748b..42580aa 100644 --- a/lib/typographer.js +++ b/lib/typographer.js @@ -1,4 +1,4 @@ -// Class of typographic replacements rules +// Class of typographic replacement rules // 'use strict'; @@ -7,16 +7,15 @@ // - miltiplication 2 x 4 -> 2 × 4 -var defaults = require('./defaults/typographer'); -var assign = require('./common/utils').assign; -var Ruler = require('./ruler'); +var defaults = require('./defaults/typographer'); +var assign = require('./common/utils').assign; +var Ruler = require('./ruler'); -var rules = []; - - -rules.push(require('./rules_typographer/replace')); -rules.push(require('./rules_typographer/smartquotes')); +var rules = [ + require('./rules_text/replace'), + require('./rules_text/smartquotes') +]; function Typographer() {