Skip to content

Commit

Permalink
Merge pull request #252 from FormidableLabs/remove-theme-dictionary-hook
Browse files Browse the repository at this point in the history
Remove theme dictionary hook
  • Loading branch information
nlkluth authored Dec 9, 2024
2 parents 20631f4 + 3cfe239 commit 53c411c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .changeset/slow-kiwis-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"prism-react-renderer": patch
---

Remove client side hooks
4 changes: 2 additions & 2 deletions packages/prism-react-renderer/src/components/highlight.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { InternalHighlightProps } from "../types"
import { useThemeDictionary } from "./useThemeDictionary"
import { useGetLineProps } from "./useGetLineProps"
import { useGetTokenProps } from "./useGetTokenProps"
import { useTokenize } from "./useTokenize"
import themeToDict from "../utils/themeToDict"

export const Highlight = ({
children,
Expand All @@ -12,7 +12,7 @@ export const Highlight = ({
prism,
}: InternalHighlightProps) => {
const language = _language.toLowerCase()
const themeDictionary = useThemeDictionary(language, theme)
const themeDictionary = themeToDict(theme, language)
const getLineProps = useGetLineProps(themeDictionary)
const getTokenProps = useGetTokenProps(themeDictionary)
const grammar = prism.languages[language]
Expand Down
24 changes: 0 additions & 24 deletions packages/prism-react-renderer/src/components/useThemeDictionary.ts

This file was deleted.

17 changes: 11 additions & 6 deletions packages/prism-react-renderer/src/components/useTokenize.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EnvConfig, Language, PrismGrammar, PrismLib } from "../types"
import normalizeTokens from "../utils/normalizeTokens"
import { useMemo, useRef } from "react"
import { useMemo } from "react"

type Options = {
prism: PrismLib
Expand All @@ -10,7 +10,6 @@ type Options = {
}

export const useTokenize = ({ prism, code, grammar, language }: Options) => {
const prismRef = useRef(prism)
return useMemo(() => {
if (grammar == null) return normalizeTokens([code])

Expand All @@ -21,9 +20,15 @@ export const useTokenize = ({ prism, code, grammar, language }: Options) => {
tokens: [],
}

prismRef.current.hooks.run("before-tokenize", prismConfig)
prismConfig.tokens = prismRef.current.tokenize(code, grammar)
prismRef.current.hooks.run("after-tokenize", prismConfig)
prism.hooks.run("before-tokenize", prismConfig)
prismConfig.tokens = prism.tokenize(code, grammar)
prism.hooks.run("after-tokenize", prismConfig)
return normalizeTokens(prismConfig.tokens)
}, [code, grammar, language])
}, [
code,
grammar,
language,
// prism is a stable import
prism,
])
}

0 comments on commit 53c411c

Please sign in to comment.