Skip to content

Conversation

@juji
Copy link

@juji juji commented Nov 2, 2025

Description

This PR adds support for named code groups with automatic tab synchronization and localStorage persistence. When multiple code groups share the same name parameter, selecting a tab in one group automatically selects the corresponding tab in all other groups with the same name, and the preference is saved across page visits.

What this PR adds:

  • New name parameter for code-group containers
  • Tab synchronization across groups with matching names
  • localStorage persistence - tab selections are remembered across page visits and sessions
  • Client-side logic to sync tab selections in real-time
  • Data attribute (data-group-name) added to HTML output
  • Validation to ensure group names only contain alphanumeric characters, hyphens, and underscores

Use case:
This is especially useful in documentation where the same tool (e.g., package managers like npm/pnpm/yarn) is shown in multiple contexts throughout a page, providing a consistent and improved user experience. Users only need to select their preference once, and it persists across the entire documentation site.

Additional Context

Implementation highlights:

  • Markdown syntax: ::: code-group name=package-managers
  • Tab synchronization is automatic and requires no user configuration
  • User preferences are stored in localStorage under vitepress:tabsCache
  • Preferences persist across page visits and are restored on load
  • Non-breaking change - the parameter is optional
  • Includes comprehensive E2E tests for functionality and edge cases
  • Documentation updated with interactive examples demonstrating the sync behavior

Files changed:

  • src/node/markdown/plugins/containers.ts - Parsing and HTML generation
  • src/client/app/composables/codeGroups.ts - Client-side synchronization logic and localStorage persistence
  • __tests__/e2e/markdown-extensions/ - E2E tests and fixtures
  • docs/*/guide/markdown.md - User documentation in all languages

Testing:

  • All existing tests pass (40 E2E tests)
  • New E2E tests verify tab synchronization works correctly
  • Edge cases tested (invalid characters, spaces, empty values)
  • localStorage persistence tested for correct restoration
  • Live examples in documentation demonstrate the feature

Translation needed:

  • English documentation is complete in docs/en/guide/markdown.md
  • All other language versions (es, ja, ko, zh, pt, ru, fa) include English content with <!-- TODO: Translate to [Language] --> markers
  • I am not capable of translating to these languages and would appreciate help from native speakers or the community
  • The English content is ready to be translated and clearly marked in each file

Code Example

::: code-group name=package-managers

```bash [npm]
npm install vitepress
```

```bash [pnpm]
pnpm add vitepress
```

```bash [yarn]
yarn add vitepress
```

:::

<!-- Later in the same domain: -->

::: code-group name=package-managers

```bash [npm]
npm run docs
```

```bash [pnpm]
pnpm run docs
```

```bash [yarn]
yarn docs
```

:::
Screen.Recording.2025-11-02.at.23.57.47.mov

Showing how it works. Also shows that on refresh, the last selected tab is still ON


Tip

The author of this PR can publish a preview release by commenting /publish below.

juji added 5 commits November 2, 2025 22:32
- Add optional group-name parameter to code-group containers
- Validates group names (alphanumeric, hyphens, underscores only)
- Adds data-group-name attribute to generated HTML
- Includes E2E tests for validation and functionality
- Updates documentation with usage examples and guidelines
- Add syncTabsInOtherGroups function to sync tabs across groups
- When clicking a tab in a named group, all groups with same name sync
- Add E2E test for synchronization behavior
- Update documentation to explain sync purpose and usage
- Add second test fixture for testing sync between groups
- Add second code group to output section
- Users can now actually test the synchronization behavior
- Add tip to encourage trying the interactive demo
@juji juji changed the title Group name Group name for Code Groups to sync opened tab Nov 2, 2025
@juji juji changed the title Group name for Code Groups to sync opened tab feat: Group name for Code Groups to sync opened tab Nov 2, 2025
@juji
Copy link
Author

juji commented Nov 2, 2025

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

Successfully merging this pull request may close these issues.

1 participant