parse_link_label.js 1013 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Parse link label
  2. //
  3. // this function assumes that first character ("[") already matches;
  4. // returns the end of the label
  5. //
  6. 'use strict';
  7. module.exports = function parseLinkLabel(state, start, disableNested) {
  8. var level, found, marker, prevPos,
  9. labelEnd = -1,
  10. max = state.posMax,
  11. oldPos = state.pos;
  12. state.pos = start + 1;
  13. level = 1;
  14. while (state.pos < max) {
  15. marker = state.src.charCodeAt(state.pos);
  16. if (marker === 0x5D /* ] */) {
  17. level--;
  18. if (level === 0) {
  19. found = true;
  20. break;
  21. }
  22. }
  23. prevPos = state.pos;
  24. state.md.inline.skipToken(state);
  25. if (marker === 0x5B /* [ */) {
  26. if (prevPos === state.pos - 1) {
  27. // increase level if we find text `[`, which is not a part of any token
  28. level++;
  29. } else if (disableNested) {
  30. state.pos = oldPos;
  31. return -1;
  32. }
  33. }
  34. }
  35. if (found) {
  36. labelEnd = state.pos;
  37. }
  38. // restore old state
  39. state.pos = oldPos;
  40. return labelEnd;
  41. };