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

No longer able to add custom code highlight grammars #2384

Closed
ashour opened this issue Oct 18, 2023 · 2 comments
Closed

No longer able to add custom code highlight grammars #2384

ashour opened this issue Oct 18, 2023 · 2 comments

Comments

@ashour
Copy link
Contributor

ashour commented Oct 18, 2023

Environment

  • Operating System: Darwin
  • Node Version: v18.18.2
  • Nuxt Version: 3.7.4
  • CLI Version: 3.9.0
  • Nitro Version: 2.6.3
  • Package Manager: npm@9.8.1
  • Builder: -
  • User Config: modules, runtimeConfig, routeRules, image, content
  • Runtime Modules: @nuxt/content@2.8.5, @nuxtjs/tailwindcss@6.8.0, @nuxt/image@1.0.0-rc.3
  • Build Modules: -

Reproduction

https://stackblitz.com/edit/github-thwkf4?file=content%2Findex.md

Describe the bug

It seems that we can no longer add custom code highlight grammars. We used to be able to via nuxt.config.ts:

// https://nuxt.com/docs/api/configuration/nuxt-config
import { readFileSync } from 'fs'

export default defineNuxtConfig({
  modules: ['@nuxt/content', '@nuxtjs/tailwindcss', '@nuxt/image'],
  content: {
    highlight: {
      theme: 'github-dark-dimmed',
      preload: [
        {
          id: 'gdscript',
          scopeName: 'source.gdscript',
          grammar: JSON.parse(
            readFileSync(
              './shiki/languages/gdscript.tmLanguage.json',
            ).toString(),
          ),
          aliases: ['gd', 'gdscript'],
        },
      ],
    },
  },
})

When have the exact config above now, you get no syntax highlighting. A TypeScript error in the nuxt.config.ts file says:

Type '{ id: string; scopeName: string; grammar: any; aliases: string[]; }' is not assignable to type 'Lang'.ts(2322)

I tried hunting down why this is happening but wasn't able to find much out. Just wanted to log it.

If this is no longer supported, you might want to remove it from the docs.

Additional context

No response

Logs

No response

@GerardSmit
Copy link

After some digging: it is expected to add the tmLanguage JSON directly into the preload-array instead of { id: 'gdscript', grammer: ... }.

Example:

// https://nuxt.com/docs/api/configuration/nuxt-config
import { readFileSync } from 'fs'

export default defineNuxtConfig({
  modules: ['@nuxt/content', '@nuxtjs/tailwindcss', '@nuxt/image'],
  content: {
    highlight: {
      theme: 'github-dark-dimmed',
      preload: [
        JSON.parse(fs.readFileSync('./shiki/languages/gdscript.tmLanguage.json', 'utf-8'))
      ],
    },
  },
})

The preload variable is passed into langs of Shikiji: https://github.com/nuxt-modules/mdc/blob/e744cd341799372ca0a631403809417de9640750/src/runtime/shiki/highlighter.ts#L18

You can read the docs about langs here: https://github.com/antfu/shikiji#fine-grained-bundle

@ashour
Copy link
Contributor Author

ashour commented Oct 24, 2023

@GerardSmit yeah, good on you. That seems to have done it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants