code-indented.mjs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. var codeIndented = {
  2. name: 'codeIndented',
  3. tokenize: tokenizeCodeIndented,
  4. resolve: resolveCodeIndented
  5. }
  6. export default codeIndented
  7. import codes from '../character/codes.mjs'
  8. import markdownLineEnding from '../character/markdown-line-ending.mjs'
  9. import constants from '../constant/constants.mjs'
  10. import types from '../constant/types.mjs'
  11. import chunkedSplice from '../util/chunked-splice.mjs'
  12. import prefixSize from '../util/prefix-size.mjs'
  13. import spaceFactory from './factory-space.mjs'
  14. var indentedContentConstruct = {
  15. tokenize: tokenizeIndentedContent,
  16. partial: true
  17. }
  18. function resolveCodeIndented(events, context) {
  19. var code = {
  20. type: types.codeIndented,
  21. start: events[0][1].start,
  22. end: events[events.length - 1][1].end
  23. }
  24. chunkedSplice(events, 0, 0, [['enter', code, context]])
  25. chunkedSplice(events, events.length, 0, [['exit', code, context]])
  26. return events
  27. }
  28. function tokenizeCodeIndented(effects, ok, nok) {
  29. return effects.attempt(indentedContentConstruct, afterPrefix, nok)
  30. function afterPrefix(code) {
  31. if (code === codes.eof) {
  32. return ok(code)
  33. }
  34. if (markdownLineEnding(code)) {
  35. return effects.attempt(indentedContentConstruct, afterPrefix, ok)(code)
  36. }
  37. effects.enter(types.codeFlowValue)
  38. return content(code)
  39. }
  40. function content(code) {
  41. if (code === codes.eof || markdownLineEnding(code)) {
  42. effects.exit(types.codeFlowValue)
  43. return afterPrefix(code)
  44. }
  45. effects.consume(code)
  46. return content
  47. }
  48. }
  49. function tokenizeIndentedContent(effects, ok, nok) {
  50. var self = this
  51. return spaceFactory(
  52. effects,
  53. afterPrefix,
  54. types.linePrefix,
  55. constants.tabSize + 1
  56. )
  57. function afterPrefix(code) {
  58. if (markdownLineEnding(code)) {
  59. effects.enter(types.lineEnding)
  60. effects.consume(code)
  61. effects.exit(types.lineEnding)
  62. return spaceFactory(
  63. effects,
  64. afterPrefix,
  65. types.linePrefix,
  66. constants.tabSize + 1
  67. )
  68. }
  69. return prefixSize(self.events, types.linePrefix) < constants.tabSize
  70. ? nok(code)
  71. : ok(code)
  72. }
  73. }