delete.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. 'use strict';
  2. var whitespace = require('is-whitespace-character');
  3. var locate = require('../locate/delete');
  4. module.exports = strikethrough;
  5. strikethrough.locator = locate;
  6. var C_TILDE = '~';
  7. var DOUBLE = '~~';
  8. function strikethrough(eat, value, silent) {
  9. var self = this;
  10. var character = '';
  11. var previous = '';
  12. var preceding = '';
  13. var subvalue = '';
  14. var index;
  15. var length;
  16. var now;
  17. if (
  18. !self.options.gfm ||
  19. value.charAt(0) !== C_TILDE ||
  20. value.charAt(1) !== C_TILDE ||
  21. whitespace(value.charAt(2))
  22. ) {
  23. return;
  24. }
  25. index = 1;
  26. length = value.length;
  27. now = eat.now();
  28. now.column += 2;
  29. now.offset += 2;
  30. while (++index < length) {
  31. character = value.charAt(index);
  32. if (
  33. character === C_TILDE &&
  34. previous === C_TILDE &&
  35. (!preceding || !whitespace(preceding))
  36. ) {
  37. /* istanbul ignore if - never used (yet) */
  38. if (silent) {
  39. return true;
  40. }
  41. return eat(DOUBLE + subvalue + DOUBLE)({
  42. type: 'delete',
  43. children: self.tokenizeInline(subvalue, now)
  44. });
  45. }
  46. subvalue += previous;
  47. preceding = previous;
  48. previous = character;
  49. }
  50. }