balance_pairs.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // For each opening emphasis-like marker find a matching closing one
  2. //
  3. 'use strict';
  4. module.exports = function link_pairs(state) {
  5. var i, j, lastDelim, currDelim,
  6. delimiters = state.delimiters,
  7. max = state.delimiters.length;
  8. for (i = 0; i < max; i++) {
  9. lastDelim = delimiters[i];
  10. if (!lastDelim.close) { continue; }
  11. j = i - lastDelim.jump - 1;
  12. while (j >= 0) {
  13. currDelim = delimiters[j];
  14. if (currDelim.open &&
  15. currDelim.marker === lastDelim.marker &&
  16. currDelim.end < 0 &&
  17. currDelim.level === lastDelim.level) {
  18. // typeofs are for backward compatibility with plugins
  19. var odd_match = (currDelim.close || lastDelim.open) &&
  20. typeof currDelim.length !== 'undefined' &&
  21. typeof lastDelim.length !== 'undefined' &&
  22. (currDelim.length + lastDelim.length) % 3 === 0;
  23. if (!odd_match) {
  24. lastDelim.jump = i - j;
  25. lastDelim.open = false;
  26. currDelim.end = i;
  27. currDelim.jump = 0;
  28. break;
  29. }
  30. }
  31. j -= currDelim.jump + 1;
  32. }
  33. }
  34. };