Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Sep 30, 2024
1 parent 59bb266 commit b47865a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
6 changes: 2 additions & 4 deletions src/react/ShikiMagicMoveRenderer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react'
import type { KeyedTokensInfo, MagicMoveRenderOptions } from '../types'
import { MagicMoveRenderer as Renderer } from '../renderer'
import { createCSSPropertiesFromString } from './utils'
import { normalizeCSSProperties } from './utils'

export interface ShikiMagicMoveRendererProps {
animate?: boolean
Expand Down Expand Up @@ -87,9 +87,7 @@ export function ShikiMagicMoveRenderer(
return (
<span
style={{
...typeof token.htmlStyle === 'string'
? createCSSPropertiesFromString(token.htmlStyle)
: token.htmlStyle,
...normalizeCSSProperties(token.htmlStyle),
color: token.color,
}}
className={['shiki-magic-move-item', token.htmlClass].filter(Boolean).join(' ')}
Expand Down
19 changes: 11 additions & 8 deletions src/react/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
export function createCSSPropertiesFromString(css?: string) {
const style: Record<string, string> = {}
css?.split(';').forEach((pair) => {
const [key, value] = pair.split(':')
if (key && value)
style[key.trim()] = value.trim()
})
return style as React.CSSProperties
export function normalizeCSSProperties(css?: string | Record<string, string>): React.CSSProperties {
if (typeof css === 'string') {
const style: Record<string, string> = {}
css?.split(';').forEach((pair) => {
const [key, value] = pair.split(':')
if (key && value)
style[key.trim()] = value.trim()
})
return style as React.CSSProperties
}
return css as React.CSSProperties
}

0 comments on commit b47865a

Please sign in to comment.