Browse Source

Make link/image/reference helpers overridable

pull/302/head
Alex Kocharin 8 years ago
parent
commit
f5a06ec0b6
  1. 2
      lib/index.js
  2. 6
      lib/rules_block/reference.js
  3. 11
      lib/rules_inline/image.js
  4. 11
      lib/rules_inline/link.js

2
lib/index.js

@ -338,7 +338,7 @@ function MarkdownIt(presetName, options) {
* Link components parser functions, useful to write plugins. See details * Link components parser functions, useful to write plugins. See details
* [here](https://github.com/markdown-it/markdown-it/blob/master/lib/helpers). * [here](https://github.com/markdown-it/markdown-it/blob/master/lib/helpers).
**/ **/
this.helpers = helpers; this.helpers = utils.assign({}, helpers);
this.options = {}; this.options = {};

6
lib/rules_block/reference.js

@ -1,8 +1,6 @@
'use strict'; 'use strict';
var parseLinkDestination = require('../helpers/parse_link_destination');
var parseLinkTitle = require('../helpers/parse_link_title');
var normalizeReference = require('../common/utils').normalizeReference; var normalizeReference = require('../common/utils').normalizeReference;
var isSpace = require('../common/utils').isSpace; var isSpace = require('../common/utils').isSpace;
@ -106,7 +104,7 @@ module.exports = function reference(state, startLine, _endLine, silent) {
// [label]: destination 'title' // [label]: destination 'title'
// ^^^^^^^^^^^ parse this // ^^^^^^^^^^^ parse this
res = parseLinkDestination(str, pos, max); res = state.md.helpers.parseLinkDestination(str, pos, max);
if (!res.ok) { return false; } if (!res.ok) { return false; }
href = state.md.normalizeLink(res.str); href = state.md.normalizeLink(res.str);
@ -135,7 +133,7 @@ module.exports = function reference(state, startLine, _endLine, silent) {
// [label]: destination 'title' // [label]: destination 'title'
// ^^^^^^^ parse this // ^^^^^^^ parse this
res = parseLinkTitle(str, pos, max); res = state.md.helpers.parseLinkTitle(str, pos, max);
if (pos < max && start !== pos && res.ok) { if (pos < max && start !== pos && res.ok) {
title = res.str; title = res.str;
pos = res.pos; pos = res.pos;

11
lib/rules_inline/image.js

@ -2,9 +2,6 @@
'use strict'; '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 normalizeReference = require('../common/utils').normalizeReference;
var isSpace = require('../common/utils').isSpace; 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; } if (state.src.charCodeAt(state.pos + 1) !== 0x5B/* [ */) { return false; }
labelStart = state.pos + 2; 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 // parser failed to find ']', so it's not a valid link
if (labelEnd < 0) { return false; } if (labelEnd < 0) { return false; }
@ -54,7 +51,7 @@ module.exports = function image(state, silent) {
// [link]( <href> "title" ) // [link]( <href> "title" )
// ^^^^^^ parsing link destination // ^^^^^^ parsing link destination
start = pos; start = pos;
res = parseLinkDestination(state.src, pos, state.posMax); res = state.md.helpers.parseLinkDestination(state.src, pos, state.posMax);
if (res.ok) { if (res.ok) {
href = state.md.normalizeLink(res.str); href = state.md.normalizeLink(res.str);
if (state.md.validateLink(href)) { if (state.md.validateLink(href)) {
@ -74,7 +71,7 @@ module.exports = function image(state, silent) {
// [link]( <href> "title" ) // [link]( <href> "title" )
// ^^^^^^^ parsing 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) { if (pos < max && start !== pos && res.ok) {
title = res.str; title = res.str;
pos = res.pos; pos = res.pos;
@ -102,7 +99,7 @@ module.exports = function image(state, silent) {
if (pos < max && state.src.charCodeAt(pos) === 0x5B/* [ */) { if (pos < max && state.src.charCodeAt(pos) === 0x5B/* [ */) {
start = pos + 1; start = pos + 1;
pos = parseLinkLabel(state, pos); pos = state.md.helpers.parseLinkLabel(state, pos);
if (pos >= 0) { if (pos >= 0) {
label = state.src.slice(start, pos++); label = state.src.slice(start, pos++);
} else { } else {

11
lib/rules_inline/link.js

@ -2,9 +2,6 @@
'use strict'; '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 normalizeReference = require('../common/utils').normalizeReference;
var isSpace = require('../common/utils').isSpace; 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; } if (state.src.charCodeAt(state.pos) !== 0x5B/* [ */) { return false; }
labelStart = state.pos + 1; 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 // parser failed to find ']', so it's not a valid link
if (labelEnd < 0) { return false; } if (labelEnd < 0) { return false; }
@ -51,7 +48,7 @@ module.exports = function link(state, silent) {
// [link]( <href> "title" ) // [link]( <href> "title" )
// ^^^^^^ parsing link destination // ^^^^^^ parsing link destination
start = pos; start = pos;
res = parseLinkDestination(state.src, pos, state.posMax); res = state.md.helpers.parseLinkDestination(state.src, pos, state.posMax);
if (res.ok) { if (res.ok) {
href = state.md.normalizeLink(res.str); href = state.md.normalizeLink(res.str);
if (state.md.validateLink(href)) { if (state.md.validateLink(href)) {
@ -71,7 +68,7 @@ module.exports = function link(state, silent) {
// [link]( <href> "title" ) // [link]( <href> "title" )
// ^^^^^^^ parsing 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) { if (pos < max && start !== pos && res.ok) {
title = res.str; title = res.str;
pos = res.pos; pos = res.pos;
@ -99,7 +96,7 @@ module.exports = function link(state, silent) {
if (pos < max && state.src.charCodeAt(pos) === 0x5B/* [ */) { if (pos < max && state.src.charCodeAt(pos) === 0x5B/* [ */) {
start = pos + 1; start = pos + 1;
pos = parseLinkLabel(state, pos); pos = state.md.helpers.parseLinkLabel(state, pos);
if (pos >= 0) { if (pos >= 0) {
label = state.src.slice(start, pos++); label = state.src.slice(start, pos++);
} else { } else {

Loading…
Cancel
Save