parse_link_title.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 === 0x0A) {
  31. lines++;
  32. } else if (code === 0x5C /* \ */ && pos + 1 < max) {
  33. pos++;
  34. if (str.charCodeAt(pos) === 0x0A) {
  35. lines++;
  36. }
  37. }
  38. pos++;
  39. }
  40. return result;
  41. };