question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

It tries to load all `*.tmLanguage.json` unexpectedly

See original GitHub issue

https://user-images.githubusercontent.com/8336744/132042144-9d816b2a-5354-4609-8621-b065e4bfe2b6.mp4

My source code:

import { Skeleton } from 'antd'
import type { FC } from 'react'
import { setCDN, getHighlighter } from 'shiki'

import './index.less'

import { Rehype } from 'components'
import { usePromise } from 'hooks'

export interface ShikiProps {
  theme?: string
  lang?: string
  children?: string
}

setCDN('/shiki/')

export const Shiki: FC<ShikiProps> = ({
  children,
  theme = 'dracula',
  lang,
}) => {
  const [highlighter] = usePromise(() => getHighlighter({ theme }))
  return highlighter ? (
    <Rehype>{children && highlighter.codeToHtml(children, lang)}</Rehype>
  ) : (
    <Skeleton className="shiki" />
  )
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Gerrit0commented, Sep 19, 2021

Additionally, if there are several <Shiki> used at a same time, a lot of duplicate requests will be sent and could fail…

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 codeToHtml to be synchronous. If you know the language ahead of time, you can pass it to getHighlighter in langs. If you don’t, you could ask Shiki to just load one language (it will load everything if there’s nothing in langs), and then add a call to highlighter.loadLanguage(lang) within usePromise

0reactions
KevinBatdorfcommented, Aug 17, 2022

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:

  1. I believe this code is where it’s deciding to load everything. Instead I think it could check the options after filtering, then use an empty array if nothing passed in, then rendering to plain text.
  2. Loading twice should probably not even be possible. So an improvement would be to cache any theme/languages requests
  3. Possibly update the parameters to include a lang property similar to theme (types)

Here’s a basic hook for swr I’m using which seems to work (I’ll update here if I run into issues later):

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

vscode/JavaScript.tmLanguage.json at main - GitHub
"This file has been converted from https://github.com/microsoft/TypeScript-TmLanguage/blob/master/TypeScriptReact.tmLanguage",.
Read more >
VSCode Unable to load and parse grammar for scope source.ts
Opened the file(TypeScript.tmLanguage.json) and it was empty. Noticed the warnings: Overwriting grammar scope name to file mapping for scope ...
Read more >
VS 2019 crashes when editing json file ...
Open a sample Json file without any project in VS to test it. · Please install all of Web workload and try again....
Read more >
Vscode language server crashing - Julia Discourse
Hi, for the last month or so, every time my Windows 10 computer gets rebooted the language server will not start when I...
Read more >
uwaterloo.cforall-0.1.0 - syntaxes - cfa.tmLanguage.json
{ "include": "#directive.condition.noargs" } · ], · "repository": { · "directive.punctuation": {.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found