link.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Process [link](<to> "stuff")
  2. 'use strict';
  3. var normalizeReference = require('../common/utils').normalizeReference;
  4. var isSpace = require('../common/utils').isSpace;
  5. module.exports = function link(state, silent) {
  6. var attrs,
  7. code,
  8. label,
  9. labelEnd,
  10. labelStart,
  11. pos,
  12. res,
  13. ref,
  14. title,
  15. token,
  16. href = '',
  17. oldPos = state.pos,
  18. max = state.posMax,
  19. start = state.pos,
  20. parseReference = true;
  21. if (state.src.charCodeAt(state.pos) !== 0x5B/* [ */) { return false; }
  22. labelStart = state.pos + 1;
  23. labelEnd = state.md.helpers.parseLinkLabel(state, state.pos, true);
  24. // parser failed to find ']', so it's not a valid link
  25. if (labelEnd < 0) { return false; }
  26. pos = labelEnd + 1;
  27. if (pos < max && state.src.charCodeAt(pos) === 0x28/* ( */) {
  28. //
  29. // Inline link
  30. //
  31. // might have found a valid shortcut link, disable reference parsing
  32. parseReference = false;
  33. // [link]( <href> "title" )
  34. // ^^ skipping these spaces
  35. pos++;
  36. for (; pos < max; pos++) {
  37. code = state.src.charCodeAt(pos);
  38. if (!isSpace(code) && code !== 0x0A) { break; }
  39. }
  40. if (pos >= max) { return false; }
  41. // [link]( <href> "title" )
  42. // ^^^^^^ parsing link destination
  43. start = pos;
  44. res = state.md.helpers.parseLinkDestination(state.src, pos, state.posMax);
  45. if (res.ok) {
  46. href = state.md.normalizeLink(res.str);
  47. if (state.md.validateLink(href)) {
  48. pos = res.pos;
  49. } else {
  50. href = '';
  51. }
  52. }
  53. // [link]( <href> "title" )
  54. // ^^ skipping these spaces
  55. start = pos;
  56. for (; pos < max; pos++) {
  57. code = state.src.charCodeAt(pos);
  58. if (!isSpace(code) && code !== 0x0A) { break; }
  59. }
  60. // [link]( <href> "title" )
  61. // ^^^^^^^ parsing link title
  62. res = state.md.helpers.parseLinkTitle(state.src, pos, state.posMax);
  63. if (pos < max && start !== pos && res.ok) {
  64. title = res.str;
  65. pos = res.pos;
  66. // [link]( <href> "title" )
  67. // ^^ skipping these spaces
  68. for (; pos < max; pos++) {
  69. code = state.src.charCodeAt(pos);
  70. if (!isSpace(code) && code !== 0x0A) { break; }
  71. }
  72. } else {
  73. title = '';
  74. }
  75. if (pos >= max || state.src.charCodeAt(pos) !== 0x29/* ) */) {
  76. // parsing a valid shortcut link failed, fallback to reference
  77. parseReference = true;
  78. }
  79. pos++;
  80. }
  81. if (parseReference) {
  82. //
  83. // Link reference
  84. //
  85. if (typeof state.env.references === 'undefined') { return false; }
  86. if (pos < max && state.src.charCodeAt(pos) === 0x5B/* [ */) {
  87. start = pos + 1;
  88. pos = state.md.helpers.parseLinkLabel(state, pos);
  89. if (pos >= 0) {
  90. label = state.src.slice(start, pos++);
  91. } else {
  92. pos = labelEnd + 1;
  93. }
  94. } else {
  95. pos = labelEnd + 1;
  96. }
  97. // covers label === '' and label === undefined
  98. // (collapsed reference link and shortcut reference link respectively)
  99. if (!label) { label = state.src.slice(labelStart, labelEnd); }
  100. ref = state.env.references[normalizeReference(label)];
  101. if (!ref) {
  102. state.pos = oldPos;
  103. return false;
  104. }
  105. href = ref.href;
  106. title = ref.title;
  107. }
  108. //
  109. // We found the end of the link, and know for a fact it's a valid link;
  110. // so all that's left to do is to call tokenizer.
  111. //
  112. if (!silent) {
  113. state.pos = labelStart;
  114. state.posMax = labelEnd;
  115. token = state.push('link_open', 'a', 1);
  116. token.attrs = attrs = [ [ 'href', href ] ];
  117. if (title) {
  118. attrs.push([ 'title', title ]);
  119. }
  120. state.md.inline.tokenize(state);
  121. token = state.push('link_close', 'a', -1);
  122. }
  123. state.pos = pos;
  124. state.posMax = max;
  125. return true;
  126. };