aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/helpers/rehype.ts
blob: fc51da136e19d71091c1ec5a00b8fe2f721d2ae4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
 * 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 rehypeSlug = (await import('rehype-slug')).default;
  const rehypeStringify = (await import('rehype-stringify')).default;

  return unified()
    .use(rehypeParse, { fragment: true })
    .use(rehypeSlug)
    .use(rehypeStringify)
    .processSync(content)
    .toString();
};