image-reference.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. * @import {ElementContent, Element, Properties} from 'hast'
  3. * @import {ImageReference} from 'mdast'
  4. * @import {State} from '../state.js'
  5. */
  6. import {normalizeUri} from 'micromark-util-sanitize-uri'
  7. import {revert} from '../revert.js'
  8. /**
  9. * Turn an mdast `imageReference` node into hast.
  10. *
  11. * @param {State} state
  12. * Info passed around.
  13. * @param {ImageReference} node
  14. * mdast node.
  15. * @returns {Array<ElementContent> | ElementContent}
  16. * hast node.
  17. */
  18. export function imageReference(state, node) {
  19. const id = String(node.identifier).toUpperCase()
  20. const definition = state.definitionById.get(id)
  21. if (!definition) {
  22. return revert(state, node)
  23. }
  24. /** @type {Properties} */
  25. const properties = {src: normalizeUri(definition.url || ''), alt: node.alt}
  26. if (definition.title !== null && definition.title !== undefined) {
  27. properties.title = definition.title
  28. }
  29. /** @type {Element} */
  30. const result = {type: 'element', tagName: 'img', properties, children: []}
  31. state.patch(node, result)
  32. return state.applyData(node, result)
  33. }