image.js 833 B

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