aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/helpers/rehype.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/helpers/rehype.ts')
-rw-r--r--src/utils/helpers/rehype.ts23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/utils/helpers/rehype.ts b/src/utils/helpers/rehype.ts
new file mode 100644
index 0000000..2716c62
--- /dev/null
+++ b/src/utils/helpers/rehype.ts
@@ -0,0 +1,23 @@
+/**
+ * Update a stringified HTML tree using unified plugins.
+ *
+ * It will parse the provided content to add id to each headings.
+ *
+ * @param {string} content - The page contents.
+ * @returns {string} The updated page contents.
+ */
+export const updateContentTree = async (content: string): Promise<string> => {
+ const { unified } = await import('unified');
+ const rehypeParse = (await import('rehype-parse')).default;
+ const rehypeSanitize = (await import('rehype-sanitize')).default;
+ const rehypeSlug = (await import('rehype-slug')).default;
+ const rehypeStringify = (await import('rehype-stringify')).default;
+
+ return unified()
+ .use(rehypeParse, { fragment: true })
+ .use(rehypeSlug)
+ .use(() => rehypeSanitize({ clobberPrefix: 'h-' }))
+ .use(rehypeStringify)
+ .processSync(content)
+ .toString();
+};