├── preview.gif ├── src ├── client │ ├── index.ts │ ├── emitter.ts │ ├── styles │ │ └── index.scss │ ├── config.ts │ └── components │ │ └── CodeSwitcher.ts ├── node │ ├── index.ts │ └── codeSwitcherPlugin.ts └── shared │ └── types.ts ├── .gitignore ├── demo ├── package.json └── src │ ├── .vuepress │ └── config.ts │ └── index.md ├── tsconfig.build.json ├── .github └── workflows │ └── demo.yaml ├── LICENSE ├── package.json └── README.md /preview.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tophcodes/vuepress-plugin-code-switcher/HEAD/preview.gif -------------------------------------------------------------------------------- /src/client/index.ts: -------------------------------------------------------------------------------- 1 | export * from './components/CodeSwitcher.js' 2 | export * from '../shared/types.js' 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | demo/src/.vuepress/dist 4 | demo/src/.vuepress/.cache 5 | demo/src/.vuepress/.temp 6 | lib 7 | -------------------------------------------------------------------------------- /src/client/emitter.ts: -------------------------------------------------------------------------------- 1 | import emitter from 'tiny-emitter/instance' 2 | 3 | export default { 4 | $on: (...args) => emitter.on(...args), 5 | $emit: (...args) => emitter.emit(...args), 6 | } 7 | -------------------------------------------------------------------------------- /src/node/index.ts: -------------------------------------------------------------------------------- 1 | import { codeSwitcherPlugin } from './codeSwitcherPlugin.js' 2 | 3 | export * from '../shared/types.js' 4 | export * from './codeSwitcherPlugin.js' 5 | export default codeSwitcherPlugin 6 | -------------------------------------------------------------------------------- /src/shared/types.ts: -------------------------------------------------------------------------------- 1 | export type LanguageMapping = Record 2 | export type DefaultGroups = Record 3 | 4 | export type PluginOptions = { 5 | groups?: DefaultGroups, 6 | componentName?: string, 7 | } 8 | -------------------------------------------------------------------------------- /demo/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@vuepress-plugin-code-switcher/demo", 3 | "private": true, 4 | "scripts": { 5 | "dev": "vuepress dev src", 6 | "build": "vuepress build src" 7 | }, 8 | "devDependencies": { 9 | "vuepress": "^2.0.0-beta.51" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "allowSyntheticDefaultImports": true, 4 | "lib": ["DOM", "ES2020"], 5 | "module": "ESNext", 6 | "moduleResolution": "node", 7 | "noEmitOnError": true, 8 | "noImplicitAny": false, 9 | "skipLibCheck": true, 10 | "declaration": true, 11 | "target": "ES2020", 12 | "rootDir": "./src", 13 | "outDir": "./lib", 14 | "types": ["@vuepress/client/types", "tiny-emitter"] 15 | }, 16 | "include": ["./src"] 17 | } 18 | -------------------------------------------------------------------------------- /src/client/styles/index.scss: -------------------------------------------------------------------------------- 1 | .code-switcher { 2 | div[class*="language-"]::before { 3 | content: ""; 4 | } 5 | 6 | .tab-header { 7 | ul { 8 | padding: 0; 9 | text-align: right; 10 | margin-bottom: -5px; 11 | margin-top: 20px; 12 | } 13 | 14 | li { 15 | display: inline-block; 16 | padding: 0 10px; 17 | cursor: pointer; 18 | border-radius: 4px; 19 | border: 2px solid var(--c-border); 20 | margin-left: 5px; 21 | font-size: 80%; 22 | 23 | &.active { 24 | border-color: var(--c-brand); 25 | } 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/node/codeSwitcherPlugin.ts: -------------------------------------------------------------------------------- 1 | import type { Plugin } from '@vuepress/core' 2 | import { getDirname, path } from '@vuepress/utils' 3 | import type { PluginOptions } from '../shared/types' 4 | 5 | const __dirname = getDirname(import.meta.url) 6 | 7 | export const codeSwitcherPlugin = ({ 8 | componentName = 'CodeSwitcher', 9 | groups = {}, 10 | }: PluginOptions = {}): Plugin => ({ 11 | name: 'vuepress-plugin-code-switcher', 12 | 13 | clientConfigFile: () => path.resolve(__dirname, '../client/config.js'), 14 | 15 | define: { 16 | __CODE_SWITCHER_COMPONENT_NAME__: componentName, 17 | __CODE_SWITCHER_GROUPS__: groups, 18 | } 19 | }) 20 | -------------------------------------------------------------------------------- /src/client/config.ts: -------------------------------------------------------------------------------- 1 | import { defineClientConfig } from '@vuepress/client' 2 | import { h } from 'vue' 3 | import type { PluginOptions } from '../shared/types' 4 | import { CodeSwitcher } from './components/CodeSwitcher' 5 | 6 | import './styles/index.scss' 7 | 8 | declare const __CODE_SWITCHER_COMPONENT_NAME__: string 9 | declare const __CODE_SWITCHER_GROUPS__: PluginOptions 10 | 11 | const defaultGroups = __CODE_SWITCHER_GROUPS__ 12 | 13 | export default defineClientConfig({ 14 | enhance({ app }) { 15 | app.component(__CODE_SWITCHER_COMPONENT_NAME__, (props, context) => { 16 | return h(CodeSwitcher, { 17 | groups: defaultGroups, 18 | ...props 19 | }, context.slots) 20 | }) 21 | } 22 | }) 23 | -------------------------------------------------------------------------------- /demo/src/.vuepress/config.ts: -------------------------------------------------------------------------------- 1 | import { defineUserConfig } from 'vuepress' 2 | import { codeSwitcherPlugin } from '../../../lib/node' 3 | import { defaultTheme } from '@vuepress/theme-default' 4 | 5 | export default defineUserConfig({ 6 | title: 'Code Switcher Vuepress Plugin', 7 | 8 | theme: defaultTheme({ 9 | repo: 'https://github.com/padarom/vuepress-plugin-code-switcher', 10 | editLink: true, 11 | docsDir: 'demo/src', 12 | lastUpdated: true, 13 | }), 14 | 15 | plugins: [ 16 | codeSwitcherPlugin({ 17 | groups: { 18 | synchronized: { julia: 'Julia', kotlin: 'Kotlin', perl: 'Perl' }, 19 | 'group-1': { nim: 'Nim', ocaml: 'OCaml' }, 20 | 'group-2': { nim: 'Nim', ocaml: 'OCaml' }, 21 | }, 22 | }), 23 | ], 24 | }) 25 | -------------------------------------------------------------------------------- /.github/workflows/demo.yaml: -------------------------------------------------------------------------------- 1 | name: Build and deploy the demo page 2 | on: 3 | push: 4 | branches: 5 | - main 6 | 7 | jobs: 8 | build-and-deploy: 9 | runs-on: ubuntu-latest 10 | 11 | steps: 12 | - name: Checkout Git repository 13 | uses: actions/checkout@v2 14 | 15 | - name: Install dependencies 16 | run: npm install && cd demo && npm install 17 | 18 | - name: Build the plugin 19 | run: npm run build 20 | 21 | - name: Build and Deploy 22 | uses: jenkey2011/vuepress-deploy@master 23 | env: 24 | # See https://github.com/marketplace/actions/vuepress-deploy#create-a-personal-access-token 25 | # on how to create and store an access token in case a new one is 26 | # ever needed 27 | ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }} 28 | CNAME: code-switcher.padarom.xyz 29 | TARGET_BRANCH: gh-pages 30 | BUILD_SCRIPT: npm run build:demo 31 | BUILD_DIR: demo/src/.vuepress/dist 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Christopher Mühl 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vuepress-plugin-code-switcher", 3 | "version": "2.0.1", 4 | "author": { 5 | "name": "Christopher Mühl", 6 | "email": "christopher@padarom.xyz", 7 | "url": "https://padarom.xyz" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/padarom/vuepress-plugin-code-switcher" 12 | }, 13 | "scripts": { 14 | "build": "tsc -b tsconfig.build.json && cpx \"src/**/*.{d.ts,vue,scss}\" lib", 15 | "build:demo": "npm run build --workspace @vuepress-plugin-code-switcher/demo", 16 | "dev": "npm run dev --workspace @vuepress-plugin-code-switcher/demo" 17 | }, 18 | "workspaces": ["./demo"], 19 | "packageManager": "pnpm@7.11.0", 20 | "type": "module", 21 | "exports": { 22 | ".": "./lib/node/index.js", 23 | "./client": "./lib/client/index.js", 24 | "./package.json": "./package.json" 25 | }, 26 | "main": "./lib/node/index.ts", 27 | "types": "./lib/node/index.d.ts", 28 | "files": [ 29 | "lib" 30 | ], 31 | "license": "MIT", 32 | "devDependencies": { 33 | "cpx": "^1.5.0", 34 | "sass": "^1.55.0", 35 | "sass-loader": "^13.0.2", 36 | "typescript": "^4.8.4" 37 | }, 38 | "dependencies": { 39 | "@vuepress/client": "^2.0.0-beta.51", 40 | "@vuepress/core": "^2.0.0-beta.51", 41 | "@vuepress/utils": "^2.0.0-beta.51", 42 | "tiny-emitter": "^2.1.0", 43 | "vue": "^3.2.40" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vuepress-plugin-code-switcher 2 | Component that allows having synchronized language switchable code blocks (e.g. to switch between Java and Kotlin examples). Selected languages are persisted to local storage to have language selection be permanent across page requests. 3 | 4 | This plugin supports both Vuepress 1 and 2. Since Vuepress 1 plugins are incompatible with Vuepress 2 I try to maintain the plugin for both Vuepress versions. Those plugin versions can be seen in different GitHub branches as shown below. 5 | 6 | | | Vuepress 1 | Vuepress 2 | 7 | | -- | --- | --- | 8 | | npm | Versions `1.x.x` | Versions `2.x.x` | 9 | | GitHub | [`vuepress-1` Branch](https://github.com/padarom/vuepress-plugin-code-switcher/tree/vuepress-1) | [`main` Branch](https://github.com/padarom/vuepress-plugin-code-switcher/tree/main) | 10 | 11 | ## Demo 12 | A live demo is available at https://code-switcher.padarom.xyz. 13 | 14 | ![](preview.gif) 15 | ## Installation 16 | **These instructions are only valid for Vuepress 2. If you use Vuepress 1, see [here](https://github.com/padarom/vuepress-plugin-code-switcher/blob/vuepress-1/README.md#installation).** 17 | 18 | ``` 19 | $ npm install vuepress-plugin-code-switcher@~2.0 --save 20 | ``` 21 | 22 | After installing, add it to your Vuepress configuration's plugin list: 23 | 24 | ```ts 25 | import { codeSwitcherPlugin } from 'vuepress-plugin-code-switcher' 26 | 27 | export default { 28 | // Your remaining configuration ... 29 | plugins: [ codeSwitcherPlugin(/* your config options go here */) ], 30 | } 31 | ``` 32 | 33 | ### Usage 34 | ````markdown 35 | 36 | 45 | 54 | 55 | ```` 56 | 57 | > The extra newline between the `