1
0

index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. module.exports = ({ markdownAST }) => {
  2. const headingsIndexArr = [];
  3. let newTree = [];
  4. const allImports = markdownAST.children.filter(item => item.type === 'import');
  5. const allExports = markdownAST.children.filter(item => item.type === 'export');
  6. let noImportExport = markdownAST.children.filter(item => !(item.type === 'import' || item.type === 'export'));
  7. noImportExport.forEach((node, index) => {
  8. if (node.type === 'heading' && node.depth === 2) {
  9. headingsIndexArr.push(index);
  10. }
  11. });
  12. if (headingsIndexArr.length > 0) {
  13. if (headingsIndexArr[0] !== 0) {
  14. newTree = noImportExport.slice(0, headingsIndexArr[0]);
  15. }
  16. headingsIndexArr.forEach((currentIndex, i) => {
  17. const startPoint = currentIndex;
  18. const endPoint = i === headingsIndexArr.length - 1 ? noImportExport.length : headingsIndexArr[i + 1];
  19. const children = noImportExport.slice(startPoint, endPoint);
  20. if (children.length) {
  21. const wrapperNode = {
  22. type: 'custom',
  23. children,
  24. data: { hName: 'section' },
  25. };
  26. newTree.push(wrapperNode);
  27. }
  28. });
  29. markdownAST.children = [...allImports, ...newTree, ...allExports];
  30. }
  31. return markdownAST;
  32. };