-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (33 loc) · 903 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* @typedef {import('mdast').Root} Root
* @typedef {import('mdast').Paragraph} Paragraph
* @typedef {import('mdast').Link} Link
* @typedef {import('mdast').LinkReference} LinkReference
*/
import {visit} from 'unist-util-visit'
/**
* Plugin to remove the wrapping paragraph for images.
*
* @type {import('unified').Plugin<void[], Root>}
*/
export default function remarkUnwrapTags() {
return (tree) => walkAndUnwrap(tree)
}
export const walkAndUnwrap = (tree) => {
visit(tree, 'paragraph', (node, index, parent) => {
if (node.children) {
node.children.forEach((child) => {
if (child && child.value) {
child.value = child.value
.replaceAll('\n', ' ')
.replaceAll('\r', ' ')
.toString()
}
})
}
if (parent.type === 'mdxJsxFlowElement') {
parent.children = node.children
}
})
return tree
}