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

Allow endusers to modify the shiki configuration #1067

Closed
4 tasks done
lurr opened this issue Jul 26, 2022 · 5 comments
Closed
4 tasks done

Allow endusers to modify the shiki configuration #1067

lurr opened this issue Jul 26, 2022 · 5 comments

Comments

@lurr
Copy link

lurr commented Jul 26, 2022

Is your feature request related to a problem? Please describe.

I've been writing a small website on the side, and am looking to add custom languages (e.g. LC-3) to shiki for the articles.
I've noticed there's no way to configure this within .vitepress/config.ts, and I do not want to resort to modifying the shiki package to do this.

Describe the solution you'd like

As described on https://shiki.matsu.io/, custom languages can be loaded during the getHighlighter call:

getHighlighter({
  theme: 'nord',
  langs: [
    {
      id: 'rockstar',
      scopeName: 'source.rockstar',
      path: './rockstar.tmLanguage.json' // this can also be a JSON object describing the grammar, doesn't need to load from a file.
    }
  ]
})

Another method is to use highlighter.loadLanguage(lang) afterwards.

It would be great if we could specify arguments to shiki.getHighlighter in .vitepress/config.{js,ts} as we can with markdown-it, vue, etc.

Describe alternatives you've considered

No response

Additional context

No response

Validations

@brc-dd
Copy link
Member

brc-dd commented Jul 26, 2022

You can directly pass your highlighter function.

@lurr
Copy link
Author

lurr commented Jul 26, 2022

You can directly pass your highlighter function.

Took a while to figure this one out - had to add @types/markdown-it to my devDependencies to make it happy with that.
Thanks for the pointer! This was actually resolved by #857

@lurr lurr closed this as completed Jul 26, 2022
@brc-dd
Copy link
Member

brc-dd commented Jul 26, 2022

Yeah, it's not documented yet.

Also, I don't think it'll work with shiki. Shiki's APIs are async, and we are just asking for options.highlight. So unless one can do top-level await in vitepress config file, how will they get the highligher instance?! 👀 How did you do it?

@lurr
Copy link
Author

lurr commented Jul 26, 2022

Yeah, it's not documented yet.

Also, I don't think it'll work with shiki. Shiki's APIs are async, and we are just asking for options.highlight. So unless one can do top-level await in vitepress config file, how will they get the highligher instance?! eyes How did you do it?

I defined the following:

export async function highlighter() {
    const highlighter = await getHighlighter({
        theme: 'material-palenight'
    })

    // Inject another grammar
    highlighter.loadLanguage(lc3)

    const preRE = /^<pre.*?>/
    const vueRE = /-vue$/

    return (str: string, lang: string) => {
        const vPre = vueRE.test(lang) ? '' : 'v-pre'
        lang = lang.replace(vueRE, '')

        return highlighter
            .codeToHtml(str, { lang, theme: 'material-palenight' })
            .replace(preRE, `<pre ${vPre}>`)
    }
}

I figured out that vitepress loads the configuration by awaiting the config; which allows you to do things such as

export default async () => defineConfig({
    ...
    markdown: {
        highlight: await highlighter()
    },
    ...
})

From there, I had access to an async context and could do anything.

@brc-dd
Copy link
Member

brc-dd commented Jul 26, 2022

I figured out that vitepress loads the configuration by awaiting the config; which allows you to do things such as

Ah, we actually use Vite's loadConfigFromFile. Good to know it supports async config too. 👍

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants