Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove theme dictionary hook #252

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since prism is being imported, it's stable and won't cause unnecessary recalculations. I tested this a bit by changing some state while prism was in the dependency array and the memoized function was not invoked again

])
}
Loading