|
@@ -1,13 +1,16 @@
|
|
|
import type { Schema as SanitizeOption } from 'hast-util-sanitize';
|
|
import type { Schema as SanitizeOption } from 'hast-util-sanitize';
|
|
|
import type { Plugin } from 'unified';
|
|
import type { Plugin } from 'unified';
|
|
|
-import type { Parent } from 'unist';
|
|
|
|
|
|
|
+import type { Parent, Node } from 'unist';
|
|
|
import { findAfter } from 'unist-util-find-after';
|
|
import { findAfter } from 'unist-util-find-after';
|
|
|
import { visit } from 'unist-util-visit';
|
|
import { visit } from 'unist-util-visit';
|
|
|
|
|
|
|
|
|
|
|
|
|
-function wrapWithSection(parentNode: Parent, startIndex = 0, endIndex: number | undefined): void {
|
|
|
|
|
|
|
+function wrapWithSection(parentNode: Parent, startElem: Node, endElem: Node | null): void {
|
|
|
const siblings = parentNode.children;
|
|
const siblings = parentNode.children;
|
|
|
|
|
|
|
|
|
|
+ const startIndex = siblings.indexOf(startElem);
|
|
|
|
|
+ const endIndex = endElem != null ? siblings.indexOf(endElem) : undefined;
|
|
|
|
|
+
|
|
|
const between = siblings.slice(
|
|
const between = siblings.slice(
|
|
|
startIndex,
|
|
startIndex,
|
|
|
endIndex,
|
|
endIndex,
|
|
@@ -26,19 +29,16 @@ function wrapWithSection(parentNode: Parent, startIndex = 0, endIndex: number |
|
|
|
|
|
|
|
|
export const remarkPlugin: Plugin = function() {
|
|
export const remarkPlugin: Plugin = function() {
|
|
|
|
|
|
|
|
- let cursor = 0;
|
|
|
|
|
return (tree) => {
|
|
return (tree) => {
|
|
|
// wrap with <section>
|
|
// wrap with <section>
|
|
|
visit(
|
|
visit(
|
|
|
tree,
|
|
tree,
|
|
|
- node => node.type === 'thematicBreak',
|
|
|
|
|
|
|
+ node => node.type !== 'thematicBreak',
|
|
|
(node, index, parent: Parent) => {
|
|
(node, index, parent: Parent) => {
|
|
|
- if (index != null) {
|
|
|
|
|
- wrapWithSection(parent, cursor, index);
|
|
|
|
|
|
|
+ const startElem = node;
|
|
|
|
|
+ const endElem = findAfter(parent, startElem, node => node.type === 'thematicBreak');
|
|
|
|
|
|
|
|
- // set cursor after index
|
|
|
|
|
- cursor = index + 1;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ wrapWithSection(parent, startElem, endElem);
|
|
|
},
|
|
},
|
|
|
);
|
|
);
|
|
|
};
|
|
};
|