From 744ad8a989c2893e0e816442abc696330677f638 Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 19 Dec 2023 08:54:20 -0800 Subject: [PATCH] fix: keep element props (#139) * fix: keep element props * fix: add code.data * refactor --- src/index.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 2e56c86..c1f8aa5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import type { Element, ElementContent, Root } from 'hast'; +import type { Element, ElementContent, ElementData, Root } from 'hast'; import type { Options, Theme } from '../'; import type { CharsHighlighterOptions } from './types'; import type { Highlighter, CodeToHastOptions } from 'shikiji'; @@ -54,8 +54,10 @@ function apply( }: ApplyProps, ) { element.tagName = inline ? 'span' : 'figure'; - // User can replace this with a real Fragment at runtime - element.properties = { 'data-rehype-pretty-code-figure': '' }; + element.properties['data-rehype-pretty-code-figure'] = ''; + + const codeData = element.children[0]?.data as ElementData | undefined; + element.children = [tree] .map((tree) => { const pre = tree.children[0]; @@ -95,6 +97,7 @@ function apply( code.properties['data-language'] = lang; code.properties['data-theme'] = themeNamesString; + code.data = codeData; if (inline) { if (keepBackground) {