parse_link_title.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Parse link title
  2. //
  3. 'use strict';
  4. var unescapeAll = require('../common/utils').unescapeAll;
  5. module.exports = function parseLinkTitle(str, pos, max) {
  6. var code,
  7. marker,
  8. lines = 0,
  9. start = pos,
  10. result = {
  11. ok: false,
  12. pos: 0,
  13. lines: 0,
  14. str: ''
  15. };
  16. if (pos >= max) { return result; }
  17. marker = str.charCodeAt(pos);
  18. if (marker !== 0x22 /* " */ && marker !== 0x27 /* ' */ && marker !== 0x28 /* ( */) { return result; }
  19. pos++;
  20. // if opening marker is "(", switch it to closing marker ")"
  21. if (marker === 0x28) { marker = 0x29; }
  22. while (pos < max) {
  23. code = str.charCodeAt(pos);
  24. if (code === marker) {
  25. result.pos = pos + 1;
  26. result.lines = lines;
  27. result.str = unescapeAll(str.slice(start + 1, pos));
  28. result.ok = true;
  29. return result;
  30. } else if (code === 0x28 /* ( */ && marker === 0x29 /* ) */) {
  31. return result;
  32. } else if (code === 0x0A) {
  33. lines++;
  34. } else if (code === 0x5C /* \ */ && pos + 1 < max) {
  35. pos++;
  36. if (str.charCodeAt(pos) === 0x0A) {
  37. lines++;
  38. }
  39. }
  40. pos++;
  41. }
  42. return result;
  43. };