emphasis.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * @typedef {import('mdast').Emphasis} Emphasis
  3. * @typedef {import('mdast').Parents} Parents
  4. * @typedef {import('../types.js').Info} Info
  5. * @typedef {import('../types.js').State} State
  6. */
  7. import {checkEmphasis} from '../util/check-emphasis.js'
  8. emphasis.peek = emphasisPeek
  9. // To do: there are cases where emphasis cannot “form” depending on the
  10. // previous or next character of sequences.
  11. // There’s no way around that though, except for injecting zero-width stuff.
  12. // Do we need to safeguard against that?
  13. /**
  14. * @param {Emphasis} node
  15. * @param {Parents | undefined} _
  16. * @param {State} state
  17. * @param {Info} info
  18. * @returns {string}
  19. */
  20. export function emphasis(node, _, state, info) {
  21. const marker = checkEmphasis(state)
  22. const exit = state.enter('emphasis')
  23. const tracker = state.createTracker(info)
  24. let value = tracker.move(marker)
  25. value += tracker.move(
  26. state.containerPhrasing(node, {
  27. before: value,
  28. after: marker,
  29. ...tracker.current()
  30. })
  31. )
  32. value += tracker.move(marker)
  33. exit()
  34. return value
  35. }
  36. /**
  37. * @param {Emphasis} _
  38. * @param {Parents | undefined} _1
  39. * @param {State} state
  40. * @returns {string}
  41. */
  42. function emphasisPeek(_, _1, state) {
  43. return state.options.emphasis || '*'
  44. }