content.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 'use strict'
  2. Object.defineProperty(exports, '__esModule', {value: true})
  3. var assert = require('assert')
  4. var codes = require('../character/codes.js')
  5. var markdownLineEnding = require('../character/markdown-line-ending.js')
  6. var constants = require('../constant/constants.js')
  7. var types = require('../constant/types.js')
  8. var factorySpace = require('../tokenize/factory-space.js')
  9. function _interopDefaultLegacy(e) {
  10. return e && typeof e === 'object' && 'default' in e ? e : {default: e}
  11. }
  12. var assert__default = /*#__PURE__*/ _interopDefaultLegacy(assert)
  13. var tokenize = initializeContent
  14. function initializeContent(effects) {
  15. var contentStart = effects.attempt(
  16. this.parser.constructs.contentInitial,
  17. afterContentStartConstruct,
  18. paragraphInitial
  19. )
  20. var previous
  21. return contentStart
  22. function afterContentStartConstruct(code) {
  23. assert__default['default'](
  24. code === codes.eof || markdownLineEnding(code),
  25. 'expected eol or eof'
  26. )
  27. if (code === codes.eof) {
  28. effects.consume(code)
  29. return
  30. }
  31. effects.enter(types.lineEnding)
  32. effects.consume(code)
  33. effects.exit(types.lineEnding)
  34. return factorySpace(effects, contentStart, types.linePrefix)
  35. }
  36. function paragraphInitial(code) {
  37. assert__default['default'](
  38. code !== codes.eof && !markdownLineEnding(code),
  39. 'expected anything other than a line ending or EOF'
  40. )
  41. effects.enter(types.paragraph)
  42. return lineStart(code)
  43. }
  44. function lineStart(code) {
  45. var token = effects.enter(types.chunkText, {
  46. contentType: constants.contentTypeText,
  47. previous: previous
  48. })
  49. if (previous) {
  50. previous.next = token
  51. }
  52. previous = token
  53. return data(code)
  54. }
  55. function data(code) {
  56. if (code === codes.eof) {
  57. effects.exit(types.chunkText)
  58. effects.exit(types.paragraph)
  59. effects.consume(code)
  60. return
  61. }
  62. if (markdownLineEnding(code)) {
  63. effects.consume(code)
  64. effects.exit(types.chunkText)
  65. return lineStart
  66. }
  67. // Data.
  68. effects.consume(code)
  69. return data
  70. }
  71. }
  72. exports.tokenize = tokenize