list.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * @import {Element, Properties} from 'hast'
  3. * @import {List} from 'mdast'
  4. * @import {State} from '../state.js'
  5. */
  6. /**
  7. * Turn an mdast `list` node into hast.
  8. *
  9. * @param {State} state
  10. * Info passed around.
  11. * @param {List} node
  12. * mdast node.
  13. * @returns {Element}
  14. * hast node.
  15. */
  16. export function list(state, node) {
  17. /** @type {Properties} */
  18. const properties = {}
  19. const results = state.all(node)
  20. let index = -1
  21. if (typeof node.start === 'number' && node.start !== 1) {
  22. properties.start = node.start
  23. }
  24. // Like GitHub, add a class for custom styling.
  25. while (++index < results.length) {
  26. const child = results[index]
  27. if (
  28. child.type === 'element' &&
  29. child.tagName === 'li' &&
  30. child.properties &&
  31. Array.isArray(child.properties.className) &&
  32. child.properties.className.includes('task-list-item')
  33. ) {
  34. properties.className = ['contains-task-list']
  35. break
  36. }
  37. }
  38. /** @type {Element} */
  39. const result = {
  40. type: 'element',
  41. tagName: node.ordered ? 'ol' : 'ul',
  42. properties,
  43. children: state.wrap(results, true)
  44. }
  45. state.patch(node, result)
  46. return state.applyData(node, result)
  47. }