factory-name.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
  3. * @typedef {import('micromark-util-types').Effects} Effects
  4. * @typedef {import('micromark-util-types').State} State
  5. */
  6. import { asciiAlpha, asciiAlphanumeric } from 'micromark-util-character';
  7. import { codes } from 'micromark-util-symbol';
  8. /**
  9. * @this {TokenizeContext}
  10. * @param {Effects} effects
  11. * @param {State} ok
  12. * @param {State} nok
  13. * @param {string} type
  14. */
  15. export function factoryName(effects, ok, nok, type) {
  16. const self = this;
  17. return start;
  18. /** @type {State} */
  19. function start(code) {
  20. if (asciiAlpha(code)) {
  21. effects.enter(type);
  22. effects.consume(code);
  23. return name;
  24. }
  25. return nok(code);
  26. }
  27. /** @type {State} */
  28. function name(code) {
  29. if (
  30. code === codes.dash ||
  31. code === codes.underscore ||
  32. asciiAlphanumeric(code)
  33. ) {
  34. effects.consume(code);
  35. return name;
  36. }
  37. effects.exit(type);
  38. return self.previous === codes.dash || self.previous === codes.underscore
  39. ? nok(code)
  40. : ok(code);
  41. }
  42. }