Skip to content

add previous sibling element to h1 while walking the tree? #90

Discussion options

You must be logged in to vote

this did the trick with unist-util-visit-parents but @wooorm says it's buggy (see comment below):

import { visitParents } from 'unist-util-visit-parents'

.use(() => (tree) => {
	visitParents(tree, 'element', (node, parents) => {
		const headers = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']
		if (headers.includes(node.tagName)) {
			if (node.tagName === 'h1') {
				node.properties.className = ['chapter']
				const parent = parents[parents.length - 1]
				const sibling = parent.children
				const pos = sibling.indexOf(node)
				parent.children = sibling
					.slice(0, pos - 1)
					.concat(h('.page-break-before'), node)
					.concat(sibling.slice(pos + 1))
			}
			const title = toString(node.chil…

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@wooorm
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by deadcoder0904
Comment options

You must be logged in to vote
1 reply
@deadcoder0904
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants