presentation.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import type { Schema as SanitizeOption } from 'hast-util-sanitize';
  2. import type { Plugin } from 'unified';
  3. import type { Parent } from 'unist';
  4. import { findAfter } from 'unist-util-find-after';
  5. import { visitParents } from 'unist-util-visit-parents';
  6. export const remarkPlugin: Plugin = function() {
  7. return (tree) => {
  8. visitParents(
  9. tree,
  10. node => node.type === 'heading',
  11. (node, ancestors) => {
  12. const parent = ancestors.slice(-1)[0] as Parent;
  13. const startElem = node;
  14. const endElem = findAfter(parent, startElem, node => node.type === 'heading');
  15. const startIndex = parent.children.indexOf(startElem);
  16. const endIndex = endElem != null ? parent.children.indexOf(endElem) : undefined;
  17. const between = parent.children.slice(
  18. startIndex,
  19. endIndex,
  20. );
  21. const section = {
  22. type: 'slide',
  23. children: between,
  24. data: {
  25. hName: 'slide',
  26. },
  27. };
  28. parent.children.splice(startIndex, section.children.length, section);
  29. },
  30. );
  31. };
  32. };
  33. export const sanitizeOption: SanitizeOption = {
  34. tagNames: ['slide'],
  35. };