vuejs / vitepress

Vite & Vue powered static site generator.
https://vitepress.dev
MIT License
12.37k stars 2.02k forks source link

Syntax highlighting not working for Vue interpolated code blocks #2315

Closed ericfennis closed 1 year ago

ericfennis commented 1 year ago

Describe the bug

We wrote a markdown code group to dynamically generate code for each framework. Unfortunately, the syntax highlighting is not working when using Vue interpolated code blocks, which is known and mentioned in the docs.

Reproduction

Create an array or object with code examples to fill each code block

The markdown:

<script setup>
const codeExample = {..}
</script>

::: code-group

```html-vue [HTML]
{{codeExample.html.code}}
{{codeExample.react.code}}
{{codeExample.vue.code}}
{{codeExample.svelte.code}}
{{codeExample.preact.code}}
{{codeExample.solid.code}}
{{codeExample.angular.code}}
{{codeExample.font.code}}
{{codeExample.flutter.code}}

:::


Visual Result
<img width="891" alt="image" src="https://user-images.githubusercontent.com/11825403/235295993-01f1f136-8e17-4971-a616-da41bd38b860.png">

### Expected behavior

Expected result:
<img width="714" alt="image" src="https://user-images.githubusercontent.com/11825403/235296568-f15c0e82-ad10-41d4-9215-68ce2e514423.png">

### System Info

```sh
System:
    OS: macOS 13.3
    CPU: (12) arm64 Apple M2 Max
    Memory: 784.03 MB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.15.0 - ~/.nvm/versions/node/v18.15.0/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 9.5.0 - ~/.nvm/versions/node/v18.15.0/bin/npm
  Browsers:
    Chrome: 111.0.5563.146
    Safari: 16.4
  npmPackages:
    vitepress: 1.0.0-alpha.73 => 1.0.0-alpha.73

Additional context

In the documentation, this is mentioned: "Note that this might prevent certain tokens from being syntax highlighted properly." Source: https://vitepress.dev/guide/using-vue#unescape-in-code-blocks But none of the code blocks are syntax highlighted.

Validations

brc-dd commented 1 year ago

What's the issue here? Syntax highlighting won't work with interpolated code. It's mentioned in the docs.

ericfennis commented 1 year ago

@brc-dd yeah I know. But doesn't mean it is expected behavior

ericfennis commented 1 year ago

I see you labeled the issue as "Won't fix". But can you maybe explain why it's not possible. Are there technical limitations why syntax highlighting is broken when using Vue interpolation? Or did you only labeled it as unfixed because it is written in de docs that it is not working?

LinusBorg commented 1 year ago
  1. Reporting a bug for a known and documented behavior will result in the report being closed, because there is no bug. If anything, this should have been a feature request.
  2. It's not going to be fixed because markdown conversion to HTML (and code-blocks as part of that) happens before the result is being evaluated as a Vue component template (at which point interpolation would happen). So syntax highlighting happens before interpolation, and that is kind of an architectural foundation that we cannot re-work easily.
ericfennis commented 1 year ago

@LinusBorg yeah maybe it should be feature request then. Sorry for that.

Thanks for the explanation. I understand now what the problem is.

Do you maybe know a workaround? How I can dynamically render code-snippets with syntax highlighting.

brc-dd commented 1 year ago

You can try creating a component that takes the code as text and highlights + renders it as code block 👀

Using shiki should be possible on the browser IMO: https://github.com/shikijs/shiki/issues/22, https://github.com/shikijs/shiki/pull/109

ericfennis commented 1 year ago

@brc-dd hmm that maybe will work. But will increase bundle size ofc. Will think of this. Thanks!

brc-dd commented 1 year ago

Yeah it will add around 100-200kB to the bundle size. How are you getting that codeExample object? If that has no client-side dependency, you can use data loading to render the code directly in the server and pass it as string to the md file and use it with v-html.

ericfennis commented 1 year ago

Yeah we using the dat loading feature with Parma as well. Using v-html also good option. Maybe I can use the internal code highlighter from vitepress to do this. Do you think it will be hard to get the tabs/codegroups working as well?

brc-dd commented 1 year ago

You just need to generate the same html as we generate, rest is handled automatically. (No need of custom css or scripts for interactivity.)