strong.js 1.6 KB

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