character-escape.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * @import {
  3. * Construct,
  4. * State,
  5. * TokenizeContext,
  6. * Tokenizer
  7. * } from 'micromark-util-types'
  8. */
  9. import { asciiPunctuation } from 'micromark-util-character';
  10. /** @type {Construct} */
  11. export const characterEscape = {
  12. name: 'characterEscape',
  13. tokenize: tokenizeCharacterEscape
  14. };
  15. /**
  16. * @this {TokenizeContext}
  17. * Context.
  18. * @type {Tokenizer}
  19. */
  20. function tokenizeCharacterEscape(effects, ok, nok) {
  21. return start;
  22. /**
  23. * Start of character escape.
  24. *
  25. * ```markdown
  26. * > | a\*b
  27. * ^
  28. * ```
  29. *
  30. * @type {State}
  31. */
  32. function start(code) {
  33. effects.enter("characterEscape");
  34. effects.enter("escapeMarker");
  35. effects.consume(code);
  36. effects.exit("escapeMarker");
  37. return inside;
  38. }
  39. /**
  40. * After `\`, at punctuation.
  41. *
  42. * ```markdown
  43. * > | a\*b
  44. * ^
  45. * ```
  46. *
  47. * @type {State}
  48. */
  49. function inside(code) {
  50. // ASCII punctuation.
  51. if (asciiPunctuation(code)) {
  52. effects.enter("characterEscapeValue");
  53. effects.consume(code);
  54. effects.exit("characterEscapeValue");
  55. effects.exit("characterEscape");
  56. return ok;
  57. }
  58. return nok(code);
  59. }
  60. }