content.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 constants = require('../constant/constants.js')
  6. var types = require('../constant/types.js')
  7. var prefixSize = require('../util/prefix-size.js')
  8. var subtokenize = require('../util/subtokenize.js')
  9. var factorySpace = require('./factory-space.js')
  10. function _interopDefaultLegacy(e) {
  11. return e && typeof e === 'object' && 'default' in e ? e : {default: e}
  12. }
  13. var assert__default = /*#__PURE__*/ _interopDefaultLegacy(assert)
  14. // No name because it must not be turned off.
  15. var content = {
  16. tokenize: tokenizeContent,
  17. resolve: resolveContent,
  18. interruptible: true,
  19. lazy: true
  20. }
  21. var continuationConstruct = {tokenize: tokenizeContinuation, partial: true}
  22. // Content is transparent: it’s parsed right now. That way, definitions are also
  23. // parsed right now: before text in paragraphs (specifically, media) are parsed.
  24. function resolveContent(events) {
  25. subtokenize(events)
  26. return events
  27. }
  28. function tokenizeContent(effects, ok) {
  29. var previous
  30. return start
  31. function start(code) {
  32. assert__default['default'](
  33. code !== codes.eof && !markdownLineEnding(code),
  34. 'expected no eof or eol'
  35. )
  36. effects.enter(types.content)
  37. previous = effects.enter(types.chunkContent, {
  38. contentType: constants.contentTypeContent
  39. })
  40. return data(code)
  41. }
  42. function data(code) {
  43. if (code === codes.eof) {
  44. return contentEnd(code)
  45. }
  46. if (markdownLineEnding(code)) {
  47. return effects.check(
  48. continuationConstruct,
  49. contentContinue,
  50. contentEnd
  51. )(code)
  52. }
  53. // Data.
  54. effects.consume(code)
  55. return data
  56. }
  57. function contentEnd(code) {
  58. effects.exit(types.chunkContent)
  59. effects.exit(types.content)
  60. return ok(code)
  61. }
  62. function contentContinue(code) {
  63. assert__default['default'](markdownLineEnding(code), 'expected eol')
  64. effects.consume(code)
  65. effects.exit(types.chunkContent)
  66. previous = previous.next = effects.enter(types.chunkContent, {
  67. contentType: constants.contentTypeContent,
  68. previous: previous
  69. })
  70. return data
  71. }
  72. }
  73. function tokenizeContinuation(effects, ok, nok) {
  74. var self = this
  75. return startLookahead
  76. function startLookahead(code) {
  77. assert__default['default'](
  78. markdownLineEnding(code),
  79. 'expected a line ending'
  80. )
  81. effects.enter(types.lineEnding)
  82. effects.consume(code)
  83. effects.exit(types.lineEnding)
  84. return factorySpace(effects, prefixed, types.linePrefix)
  85. }
  86. function prefixed(code) {
  87. if (code === codes.eof || markdownLineEnding(code)) {
  88. return nok(code)
  89. }
  90. if (
  91. self.parser.constructs.disable.null.indexOf('codeIndented') > -1 ||
  92. prefixSize(self.events, types.linePrefix) < constants.tabSize
  93. ) {
  94. return effects.interrupt(self.parser.constructs.flow, nok, ok)(code)
  95. }
  96. return ok(code)
  97. }
  98. }
  99. module.exports = content