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
It tries to load all *.tmLanguage.json
unexpectedly
#221
Comments
Probably something I've fixed in fd178bf. I'll release a new version tomorrow, please give it a try. |
This is a problem with your implementation, not Shiki. You are creating one highlighter per element, and Shiki stores the loaded languages/themes on a highlighter instance rather than in a global cache, which is rather unintuitive, but not necessarily wrong... setCDN('/shiki/')
const hl = getHighlighter({})
export const Shiki: FC<ShikiProps> = ({
children,
theme = 'dracula',
lang,
}) => {
const [highlighter] = usePromise(async () => {
const highlighter = await hl
await highlighter.loadTheme(theme)
return highlighter
})
return highlighter ? (
<Rehype>{children && highlighter.codeToHtml(children, lang)}</Rehype>
) : (
<Skeleton className="shiki" />
)
} Loading grammars up front is also necessary for |
@Gerrit0 Thank you very much, I didn't seen any document about that But loading all languages by default is still unexpectedly to me. If it is expected in |
I agree the documentation could be better. Let me start a proper doc website... |
To stop them all from loading it seems you need to pass in the list of languages you need up front. I've also noticed it loads the resources twice (and only twice) regardless. Three possible opportunities:
Here's a basic hook for import { getHighlighter, Lang, setCDN, Theme } from 'shiki'
import useSWRImmutable from 'swr/immutable'
type Params = { theme: Theme; lang: Lang }
setCDN('https://unpkg.com/shiki/')
const fetcher = ({ theme, lang }: Params) =>
getHighlighter({ langs: [lang], theme })
export const useTheme = ({ theme, lang }: Params) => {
const { data: highlighter, error } = useSWRImmutable(
{ theme, lang },
fetcher,
)
return { highlighter, error, loading: !highlighter && !error }
} btw, love shiki so far. If you'd like me to PR any of the above i'd be happy to contribute. |
Jietu20210904-010141-HD.mp4
My source code:
The text was updated successfully, but these errors were encountered: