aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/helpers/rehype.ts
blob: 2716c62f5a33105878dd6320f63d728b15694212 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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();
};