hard-break-escape.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * @typedef {import('micromark-util-types').Construct} Construct
  3. * @typedef {import('micromark-util-types').State} State
  4. * @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
  5. * @typedef {import('micromark-util-types').Tokenizer} Tokenizer
  6. */
  7. import {markdownLineEnding} from 'micromark-util-character'
  8. import {codes, types} from 'micromark-util-symbol'
  9. import {ok as assert} from 'devlop'
  10. /** @type {Construct} */
  11. export const hardBreakEscape = {
  12. name: 'hardBreakEscape',
  13. tokenize: tokenizeHardBreakEscape
  14. }
  15. /**
  16. * @this {TokenizeContext}
  17. * @type {Tokenizer}
  18. */
  19. function tokenizeHardBreakEscape(effects, ok, nok) {
  20. return start
  21. /**
  22. * Start of a hard break (escape).
  23. *
  24. * ```markdown
  25. * > | a\
  26. * ^
  27. * | b
  28. * ```
  29. *
  30. * @type {State}
  31. */
  32. function start(code) {
  33. assert(code === codes.backslash, 'expected `\\`')
  34. effects.enter(types.hardBreakEscape)
  35. effects.consume(code)
  36. return after
  37. }
  38. /**
  39. * After `\`, at eol.
  40. *
  41. * ```markdown
  42. * > | a\
  43. * ^
  44. * | b
  45. * ```
  46. *
  47. * @type {State}
  48. */
  49. function after(code) {
  50. if (markdownLineEnding(code)) {
  51. effects.exit(types.hardBreakEscape)
  52. return ok(code)
  53. }
  54. return nok(code)
  55. }
  56. }