label-start-link.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 {codes, types} from 'micromark-util-symbol'
  8. import {ok as assert} from 'devlop'
  9. import {labelEnd} from './label-end.js'
  10. /** @type {Construct} */
  11. export const labelStartLink = {
  12. name: 'labelStartLink',
  13. tokenize: tokenizeLabelStartLink,
  14. resolveAll: labelEnd.resolveAll
  15. }
  16. /**
  17. * @this {TokenizeContext}
  18. * @type {Tokenizer}
  19. */
  20. function tokenizeLabelStartLink(effects, ok, nok) {
  21. const self = this
  22. return start
  23. /**
  24. * Start of label (link) start.
  25. *
  26. * ```markdown
  27. * > | a [b] c
  28. * ^
  29. * ```
  30. *
  31. * @type {State}
  32. */
  33. function start(code) {
  34. assert(code === codes.leftSquareBracket, 'expected `[`')
  35. effects.enter(types.labelLink)
  36. effects.enter(types.labelMarker)
  37. effects.consume(code)
  38. effects.exit(types.labelMarker)
  39. effects.exit(types.labelLink)
  40. return after
  41. }
  42. /** @type {State} */
  43. function after(code) {
  44. // To do: this isn’t needed in `micromark-extension-gfm-footnote`,
  45. // remove.
  46. // Hidden footnotes hook.
  47. /* c8 ignore next 3 */
  48. return code === codes.caret &&
  49. '_hiddenFootnoteSupport' in self.parser.constructs
  50. ? nok(code)
  51. : ok(code)
  52. }
  53. }