factory-label.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use strict'
  2. var assert = require('assert')
  3. var codes = require('../character/codes.js')
  4. var markdownLineEnding = require('../character/markdown-line-ending.js')
  5. var markdownSpace = require('../character/markdown-space.js')
  6. var constants = require('../constant/constants.js')
  7. var types = require('../constant/types.js')
  8. function _interopDefaultLegacy(e) {
  9. return e && typeof e === 'object' && 'default' in e ? e : {default: e}
  10. }
  11. var assert__default = /*#__PURE__*/ _interopDefaultLegacy(assert)
  12. // eslint-disable-next-line max-params
  13. function labelFactory(effects, ok, nok, type, markerType, stringType) {
  14. var self = this
  15. var size = 0
  16. var data
  17. return start
  18. function start(code) {
  19. assert__default['default'](code === codes.leftSquareBracket, 'expected `[`')
  20. effects.enter(type)
  21. effects.enter(markerType)
  22. effects.consume(code)
  23. effects.exit(markerType)
  24. effects.enter(stringType)
  25. return atBreak
  26. }
  27. function atBreak(code) {
  28. if (
  29. code === codes.eof ||
  30. code === codes.leftSquareBracket ||
  31. (code === codes.rightSquareBracket && !data) ||
  32. /* c8 ignore next */
  33. (code === codes.caret &&
  34. /* c8 ignore next */
  35. !size &&
  36. /* c8 ignore next */
  37. '_hiddenFootnoteSupport' in self.parser.constructs) ||
  38. size > constants.linkReferenceSizeMax
  39. ) {
  40. return nok(code)
  41. }
  42. if (code === codes.rightSquareBracket) {
  43. effects.exit(stringType)
  44. effects.enter(markerType)
  45. effects.consume(code)
  46. effects.exit(markerType)
  47. effects.exit(type)
  48. return ok
  49. }
  50. if (markdownLineEnding(code)) {
  51. effects.enter(types.lineEnding)
  52. effects.consume(code)
  53. effects.exit(types.lineEnding)
  54. return atBreak
  55. }
  56. effects.enter(types.chunkString, {contentType: constants.contentTypeString})
  57. return label(code)
  58. }
  59. function label(code) {
  60. if (
  61. code === codes.eof ||
  62. code === codes.leftSquareBracket ||
  63. code === codes.rightSquareBracket ||
  64. markdownLineEnding(code) ||
  65. size++ > constants.linkReferenceSizeMax
  66. ) {
  67. effects.exit(types.chunkString)
  68. return atBreak(code)
  69. }
  70. effects.consume(code)
  71. data = data || !markdownSpace(code)
  72. return code === codes.backslash ? labelEscape : label
  73. }
  74. function labelEscape(code) {
  75. if (
  76. code === codes.leftSquareBracket ||
  77. code === codes.backslash ||
  78. code === codes.rightSquareBracket
  79. ) {
  80. effects.consume(code)
  81. size++
  82. return label
  83. }
  84. return label(code)
  85. }
  86. }
  87. module.exports = labelFactory