strong.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. 'use strict';
  2. var trim = require('trim');
  3. var whitespace = require('is-whitespace-character');
  4. var locate = require('../locate/strong');
  5. module.exports = strong;
  6. strong.locator = locate;
  7. var C_ASTERISK = '*';
  8. var C_UNDERSCORE = '_';
  9. function strong(eat, value, silent) {
  10. var self = this;
  11. var index = 0;
  12. var character = value.charAt(index);
  13. var now;
  14. var pedantic;
  15. var marker;
  16. var queue;
  17. var subvalue;
  18. var length;
  19. var prev;
  20. if (
  21. (character !== C_ASTERISK && character !== C_UNDERSCORE) ||
  22. value.charAt(++index) !== character
  23. ) {
  24. return;
  25. }
  26. pedantic = self.options.pedantic;
  27. marker = character;
  28. subvalue = marker + marker;
  29. length = value.length;
  30. index++;
  31. queue = '';
  32. character = '';
  33. if (pedantic && whitespace(value.charAt(index))) {
  34. return;
  35. }
  36. while (index < length) {
  37. prev = character;
  38. character = value.charAt(index);
  39. if (
  40. character === marker &&
  41. value.charAt(index + 1) === marker &&
  42. (!pedantic || !whitespace(prev))
  43. ) {
  44. character = value.charAt(index + 2);
  45. if (character !== marker) {
  46. if (!trim(queue)) {
  47. return;
  48. }
  49. /* istanbul ignore if - never used (yet) */
  50. if (silent) {
  51. return true;
  52. }
  53. now = eat.now();
  54. now.column += 2;
  55. now.offset += 2;
  56. return eat(subvalue + queue + subvalue)({
  57. type: 'strong',
  58. children: self.tokenizeInline(queue, now)
  59. });
  60. }
  61. }
  62. if (!pedantic && character === '\\') {
  63. queue += character;
  64. character = value.charAt(++index);
  65. }
  66. queue += character;
  67. index++;
  68. }
  69. }