vitepress: Support code groups and allow adding title to code blocks

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

VueJS community is moving from JS to TS now. Hence, we have to provide both TS & JS code.

I already started using VitePress alpha in my OSS project but for another project, I prefer providing TS & JS code via code groups like VuePress.

Describe the solution you’d like

Provide code blocks via markdown (using component is bit ugly for markdown only content). We can inspire from Nuxt content 2 for new syntax

:::code group

:::group{ts}

```ts
const a: number = 1
```

:::

:::group{js}

```js
const a = 1
```

:::

:::

Describe alternatives you’ve considered

None

Additional context

https://content.nuxtjs.org/guide/writing/mdc

Validations

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 7
  • Comments: 18 (16 by maintainers)

Commits related to this issue

Most upvoted comments

We can custom group title in code block

```ts [TypeScript]
const a: number = 1
```

Minimizing layers (especially this kind without indentation) is useful to keep the plain text readability of markdown.

@kecrily I guess the OP meant to have title of each group too. :::group{foo} --> title is foo.

@VoVAllen Yeah, that’s similar to what others wrote. This is the intended syntax BTW:

:::code-group

```js [JavaScript]
some js stuff
```

```ts [TypeScript]
some ts stuff
```

:::

So, if you implement such a component (the one you wrote), then we just need to add some stuff in our markdown component plugin to make it understand :::code-group and ```ts [TypeScript].

Off-topic: this behavior is consistent with that on GitHub. On GitHub you can also embed markdown in html with line breaks.

@marshallswain Sorry, I didn’t get you. You can still do what you’ve wrote. You need to add spaces before and after the fences in this. HTML (or Vue in this case) should be separated from Markdown by empty lines.

Can you guys review #1560 and see if there are any issues (especially with UI)? Here is the deploy preview: https://deploy-preview-1560--vitepress-docs.netlify.app/test