From f5a06ec0b626d857d06382e7b8709d943d6f2000 Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Thu, 3 Nov 2016 13:31:36 +0000 Subject: [PATCH] Make link/image/reference helpers overridable --- lib/index.js | 2 +- lib/rules_block/reference.js | 6 ++---- lib/rules_inline/image.js | 11 ++++------- lib/rules_inline/link.js | 11 ++++------- 4 files changed, 11 insertions(+), 19 deletions(-) diff --git a/lib/index.js b/lib/index.js index 3f73b85..0803058 100644 --- a/lib/index.js +++ b/lib/index.js @@ -338,7 +338,7 @@ function MarkdownIt(presetName, options) { * Link components parser functions, useful to write plugins. See details * [here](https://github.com/markdown-it/markdown-it/blob/master/lib/helpers). **/ - this.helpers = helpers; + this.helpers = utils.assign({}, helpers); this.options = {}; diff --git a/lib/rules_block/reference.js b/lib/rules_block/reference.js index c84823c..c093373 100644 --- a/lib/rules_block/reference.js +++ b/lib/rules_block/reference.js @@ -1,8 +1,6 @@ 'use strict'; -var parseLinkDestination = require('../helpers/parse_link_destination'); -var parseLinkTitle = require('../helpers/parse_link_title'); var normalizeReference = require('../common/utils').normalizeReference; var isSpace = require('../common/utils').isSpace; @@ -106,7 +104,7 @@ module.exports = function reference(state, startLine, _endLine, silent) { // [label]: destination 'title' // ^^^^^^^^^^^ parse this - res = parseLinkDestination(str, pos, max); + res = state.md.helpers.parseLinkDestination(str, pos, max); if (!res.ok) { return false; } href = state.md.normalizeLink(res.str); @@ -135,7 +133,7 @@ module.exports = function reference(state, startLine, _endLine, silent) { // [label]: destination 'title' // ^^^^^^^ parse this - res = parseLinkTitle(str, pos, max); + res = state.md.helpers.parseLinkTitle(str, pos, max); if (pos < max && start !== pos && res.ok) { title = res.str; pos = res.pos; diff --git a/lib/rules_inline/image.js b/lib/rules_inline/image.js index 8d5c7a3..53edd32 100644 --- a/lib/rules_inline/image.js +++ b/lib/rules_inline/image.js @@ -2,9 +2,6 @@ 'use strict'; -var parseLinkLabel = require('../helpers/parse_link_label'); -var parseLinkDestination = require('../helpers/parse_link_destination'); -var parseLinkTitle = require('../helpers/parse_link_title'); var normalizeReference = require('../common/utils').normalizeReference; var isSpace = require('../common/utils').isSpace; @@ -31,7 +28,7 @@ module.exports = function image(state, silent) { if (state.src.charCodeAt(state.pos + 1) !== 0x5B/* [ */) { return false; } labelStart = state.pos + 2; - labelEnd = parseLinkLabel(state, state.pos + 1, false); + labelEnd = state.md.helpers.parseLinkLabel(state, state.pos + 1, false); // parser failed to find ']', so it's not a valid link if (labelEnd < 0) { return false; } @@ -54,7 +51,7 @@ module.exports = function image(state, silent) { // [link]( "title" ) // ^^^^^^ parsing link destination start = pos; - res = parseLinkDestination(state.src, pos, state.posMax); + res = state.md.helpers.parseLinkDestination(state.src, pos, state.posMax); if (res.ok) { href = state.md.normalizeLink(res.str); if (state.md.validateLink(href)) { @@ -74,7 +71,7 @@ module.exports = function image(state, silent) { // [link]( "title" ) // ^^^^^^^ parsing link title - res = parseLinkTitle(state.src, pos, state.posMax); + res = state.md.helpers.parseLinkTitle(state.src, pos, state.posMax); if (pos < max && start !== pos && res.ok) { title = res.str; pos = res.pos; @@ -102,7 +99,7 @@ module.exports = function image(state, silent) { if (pos < max && state.src.charCodeAt(pos) === 0x5B/* [ */) { start = pos + 1; - pos = parseLinkLabel(state, pos); + pos = state.md.helpers.parseLinkLabel(state, pos); if (pos >= 0) { label = state.src.slice(start, pos++); } else { diff --git a/lib/rules_inline/link.js b/lib/rules_inline/link.js index e7fd35d..4b9056f 100644 --- a/lib/rules_inline/link.js +++ b/lib/rules_inline/link.js @@ -2,9 +2,6 @@ 'use strict'; -var parseLinkLabel = require('../helpers/parse_link_label'); -var parseLinkDestination = require('../helpers/parse_link_destination'); -var parseLinkTitle = require('../helpers/parse_link_title'); var normalizeReference = require('../common/utils').normalizeReference; var isSpace = require('../common/utils').isSpace; @@ -28,7 +25,7 @@ module.exports = function link(state, silent) { if (state.src.charCodeAt(state.pos) !== 0x5B/* [ */) { return false; } labelStart = state.pos + 1; - labelEnd = parseLinkLabel(state, state.pos, true); + labelEnd = state.md.helpers.parseLinkLabel(state, state.pos, true); // parser failed to find ']', so it's not a valid link if (labelEnd < 0) { return false; } @@ -51,7 +48,7 @@ module.exports = function link(state, silent) { // [link]( "title" ) // ^^^^^^ parsing link destination start = pos; - res = parseLinkDestination(state.src, pos, state.posMax); + res = state.md.helpers.parseLinkDestination(state.src, pos, state.posMax); if (res.ok) { href = state.md.normalizeLink(res.str); if (state.md.validateLink(href)) { @@ -71,7 +68,7 @@ module.exports = function link(state, silent) { // [link]( "title" ) // ^^^^^^^ parsing link title - res = parseLinkTitle(state.src, pos, state.posMax); + res = state.md.helpers.parseLinkTitle(state.src, pos, state.posMax); if (pos < max && start !== pos && res.ok) { title = res.str; pos = res.pos; @@ -99,7 +96,7 @@ module.exports = function link(state, silent) { if (pos < max && state.src.charCodeAt(pos) === 0x5B/* [ */) { start = pos + 1; - pos = parseLinkLabel(state, pos); + pos = state.md.helpers.parseLinkLabel(state, pos); if (pos >= 0) { label = state.src.slice(start, pos++); } else {