123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- 'use strict';
- var normalizeReference = require('../common/utils').normalizeReference;
- var isSpace = require('../common/utils').isSpace;
- module.exports = function reference(state, startLine, _endLine, silent) {
- var ch,
- destEndPos,
- destEndLineNo,
- endLine,
- href,
- i,
- l,
- label,
- labelEnd,
- oldParentType,
- res,
- start,
- str,
- terminate,
- terminatorRules,
- title,
- lines = 0,
- pos = state.bMarks[startLine] + state.tShift[startLine],
- max = state.eMarks[startLine],
- nextLine = startLine + 1;
-
- if (state.sCount[startLine] - state.blkIndent >= 4) { return false; }
- if (state.src.charCodeAt(pos) !== 0x5B) { return false; }
-
-
- while (++pos < max) {
- if (state.src.charCodeAt(pos) === 0x5D &&
- state.src.charCodeAt(pos - 1) !== 0x5C) {
- if (pos + 1 === max) { return false; }
- if (state.src.charCodeAt(pos + 1) !== 0x3A) { return false; }
- break;
- }
- }
- endLine = state.lineMax;
-
- terminatorRules = state.md.block.ruler.getRules('reference');
- oldParentType = state.parentType;
- state.parentType = 'reference';
- for (; nextLine < endLine && !state.isEmpty(nextLine); nextLine++) {
-
-
- if (state.sCount[nextLine] - state.blkIndent > 3) { continue; }
-
- if (state.sCount[nextLine] < 0) { continue; }
-
- terminate = false;
- for (i = 0, l = terminatorRules.length; i < l; i++) {
- if (terminatorRules[i](state, nextLine, endLine, true)) {
- terminate = true;
- break;
- }
- }
- if (terminate) { break; }
- }
- str = state.getLines(startLine, nextLine, state.blkIndent, false).trim();
- max = str.length;
- for (pos = 1; pos < max; pos++) {
- ch = str.charCodeAt(pos);
- if (ch === 0x5B ) {
- return false;
- } else if (ch === 0x5D ) {
- labelEnd = pos;
- break;
- } else if (ch === 0x0A ) {
- lines++;
- } else if (ch === 0x5C ) {
- pos++;
- if (pos < max && str.charCodeAt(pos) === 0x0A) {
- lines++;
- }
- }
- }
- if (labelEnd < 0 || str.charCodeAt(labelEnd + 1) !== 0x3A) { return false; }
-
-
- for (pos = labelEnd + 2; pos < max; pos++) {
- ch = str.charCodeAt(pos);
- if (ch === 0x0A) {
- lines++;
- } else if (isSpace(ch)) {
-
- } else {
- break;
- }
- }
-
-
- res = state.md.helpers.parseLinkDestination(str, pos, max);
- if (!res.ok) { return false; }
- href = state.md.normalizeLink(res.str);
- if (!state.md.validateLink(href)) { return false; }
- pos = res.pos;
- lines += res.lines;
-
- destEndPos = pos;
- destEndLineNo = lines;
-
-
- start = pos;
- for (; pos < max; pos++) {
- ch = str.charCodeAt(pos);
- if (ch === 0x0A) {
- lines++;
- } else if (isSpace(ch)) {
-
- } else {
- break;
- }
- }
-
-
- res = state.md.helpers.parseLinkTitle(str, pos, max);
- if (pos < max && start !== pos && res.ok) {
- title = res.str;
- pos = res.pos;
- lines += res.lines;
- } else {
- title = '';
- pos = destEndPos;
- lines = destEndLineNo;
- }
-
- while (pos < max) {
- ch = str.charCodeAt(pos);
- if (!isSpace(ch)) { break; }
- pos++;
- }
- if (pos < max && str.charCodeAt(pos) !== 0x0A) {
- if (title) {
-
-
- title = '';
- pos = destEndPos;
- lines = destEndLineNo;
- while (pos < max) {
- ch = str.charCodeAt(pos);
- if (!isSpace(ch)) { break; }
- pos++;
- }
- }
- }
- if (pos < max && str.charCodeAt(pos) !== 0x0A) {
-
- return false;
- }
- label = normalizeReference(str.slice(1, labelEnd));
- if (!label) {
-
- return false;
- }
-
-
- if (silent) { return true; }
- if (typeof state.env.references === 'undefined') {
- state.env.references = {};
- }
- if (typeof state.env.references[label] === 'undefined') {
- state.env.references[label] = { title: title, href: href };
- }
- state.parentType = oldParentType;
- state.line = startLine + lines + 1;
- return true;
- };
|