{"id":15495899,"url":"https://github.com/hairyf/vitepress-plugin-demo","last_synced_at":"2025-04-15T12:19:09.007Z","repository":{"id":169832029,"uuid":"645883905","full_name":"hairyf/vitepress-plugin-demo","owner":"hairyf","description":"Markdown plugin for building demos in vitepress.","archived":false,"fork":false,"pushed_at":"2025-04-10T04:13:38.000Z","size":994,"stargazers_count":65,"open_issues_count":2,"forks_count":8,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-15T12:18:38.490Z","etag":null,"topics":["demo","markdown","react","vite","vitepress","vitepress-demo","vitepress-plugin","vue"],"latest_commit_sha":null,"homepage":"https://vitepress-plugin-demo.vercel.app/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hairyf.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-26T17:03:17.000Z","updated_at":"2025-04-10T04:13:41.000Z","dependencies_parsed_at":"2023-12-09T01:26:13.605Z","dependency_job_id":"19993dce-d6fe-4430-92e9-bee97cffc8fc","html_url":"https://github.com/hairyf/vitepress-plugin-demo","commit_stats":null,"previous_names":["hairyf/markdown-it-vitepress-demos","hairyf/vitepress-plugin-demo"],"tags_count":23,"template":false,"template_full_name":"antfu/starter-ts","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hairyf%2Fvitepress-plugin-demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hairyf%2Fvitepress-plugin-demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hairyf%2Fvitepress-plugin-demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hairyf%2Fvitepress-plugin-demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hairyf","download_url":"https://codeload.github.com/hairyf/vitepress-plugin-demo/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249067787,"owners_count":21207396,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["demo","markdown","react","vite","vitepress","vitepress-demo","vitepress-plugin","vue"],"created_at":"2024-10-02T08:20:15.140Z","updated_at":"2025-04-15T12:19:08.993Z","avatar_url":"https://github.com/hairyf.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# vitepress-plugin-demo\n\n[![npm version][npm-version-src]][npm-version-href]\n[![npm downloads][npm-downloads-src]][npm-downloads-href]\n[![bundle][bundle-src]][bundle-href]\n[![JSDocs][jsdocs-src]][jsdocs-href]\n[![License][license-src]][license-href]\n\n## Features\n\n- ✨ Use the `\u003cdemo\u003e` or `container` in Markdown to reference a demo container.\n- ♾️ Automatically convert TS code and provide JS demo code.\n- 👽️ Support Vue, React, JS, TS and HTML demo block.\n- 📝 Use Markdown syntax by you demo block description.\n- 💡 Support [twoslash](https://shiki.style/packages/vitepress) syntax highlighting.\n- 📦️ Supports multiple [presets](#presets), ready to use out of the box.\n- 🎨 Customize the demo container to suit your needs.\n\n***\n\n`vitepress-plugin-demo` is a `markdown-it` plugin specifically designed for Vitepress demos. It converts code blocks in Markdown into references to the `\u003cdemo-container\u003e` component. It does not generate UI itself but serves as a plugin for creating demo containers.\n\nWith this plugin, you can use the `\u003cdemo\u003e` tag in Markdown to reference a demo container. For example:\n\n```vue\n\u003cdemo src=\"../demo.vue\" title=\"Demo block\" description=\"use demo\" /\u003e\n```\n\nYou can use Markdown syntax in the `desc` field. For example:\n\n```vue\n\u003cdemo src=\"../demo.vue\" title=\"Demo block\" description=\"use `demo` ...\" /\u003e\n```\n\nHowever, we recommend using the `container` mode to write the `desc` content with Markdown:\n\n```markdown\n::: demo src=\"../demo.vue\" title=\"Demo block\"\n\nThis is a `description` that can be written using Markdown.\n\n:::\n```\n\nThis looks more aesthetically pleasing and adheres better to Markdown syntax.\n\nIn addition, you can pass the `attrs` parameter to `props`, so you can utilize the [Line Highlighting in Code Blocks](https://vitepress.dev/guide/markdown#line-highlighting-in-code-blocks) feature of VitePress:\n\n```markdown\n\u003cdemo src=\"../demo.vue\" attributes=\"{1,4,6-8}\" /\u003e\n\u003cdemo src=\"../demo.vue\" attributes=\"{4}\" /\u003e\n```\n\n## Twoslash\n\n`vitepress-plugin-demo` also supports [vitepress/twoslash](https://shiki.style/packages/vitepress) syntax highlighting. You can use the `twoslash` tag in Markdown to reference a demo container. For example:\n\n```markdown\n\u003cdemo src=\"../demo.vue\" title=\"Demo block\" description=\"use demo\" twoslash /\u003e\n```\n\n## React\n\n`vitepress-plugin-demo` also supports React demo blocks. You can use the `react` tag in Markdown to reference a demo container.\n\n```sh\nnpm install react react-dom --save-dev\n```\n\nimport your React component:\n\n```markdown\n\u003cdemo type=\"react\" src=\"../demo.tsx\" title=\"Demo block\" description=\"use demo\" /\u003e\n```\n\n## Install\n\n```bash\nnpm install vitepress-plugin-demo --save-dev\n```\n\n## Usage\n\n```js\nimport { demoMdPlugin } from 'vitepress-plugin-demo'\n// .vitepress/config.js\nexport default defineConfig({\n  markdown: {\n    config(md) {\n      md.use(demoMdPlugin)\n    },\n  },\n})\n```\n\nRegister your `\u003cdemo-container\u003e` component in `theme/index.ts|js`:\n\n```js\n// https://vitepress.dev/guide/custom-theme\nimport Theme from 'vitepress/theme'\n\n// your demo component\nimport CustomDemoContainer from './components/CustomDemoContainer.vue'\n\nexport default {\n  ...Theme,\n  enhanceApp({ app, router, siteData }) {\n    app.component('demo-container', CustomDemoContainer)\n  },\n}\n```\n\n## Presets\n\n`vitepress-plugin-demo` pre-set common component library themes, which you can directly use:\n\n\u003cdetails\u003e\n\u003csummary\u003eNaive UI\u003c/summary\u003e\u003cbr\u003e\n\n```sh\nnpm install vitepress-plugin-demo naive-ui --save-dev\n```\n\n```js\nimport TwoslashFloating from '@shikijs/vitepress-twoslash/client'\nimport NaiveUIContainer from 'vitepress-plugin-demo/client/naive-ui'\nimport '@shikijs/vitepress-twoslash/style.css'\n\nexport default {\n  ...Theme,\n  async enhanceApp({ app, router, siteData }) {\n    if (!import.meta.env.SSR) {\n      const { default: NaiveUI } = await import('naive-ui')\n      app.use(NaiveUI)\n    }\n    app.use(TwoslashFloating)\n    app.use(NaiveUIContainer, {\n      github: 'you github blob url',\n      codeeditor: {\n        editor: ['stackblitz', 'codesandbox'],\n        globals: {\n          package: {/* ... */},\n          files: {/* ... */},\n          opens: ['App.vue']\n        },\n        resolve(props) {\n          const code = props.typescript || props.javascript\n          return { /* cover global config */ }\n        }\n      },\n    })\n  },\n}\n```\n\n\u003cbr\u003e\u003c/details\u003e\n\n## Customs\n\nThe `demo-container` component will receive relevant information about the demo, and you need to implement the rendering of the demo:\n\n```vue\n\u003cscript lang=\"ts\" setup\u003e\nimport { computed } from 'vue'\n\nconst props = defineProps\u003c{\n  typescript: string\n  // if using ts, javascript will transform the to js\n  javascript: string\n  title: string\n  metadata: object\n}\u003e()\n\nconst code = computed(() =\u003e decodeURIComponent(props.typescript || props.javascript))\n\u003c/script\u003e\n\n\u003ctemplate\u003e\n  \u003cdiv\u003e\n    \u003cdiv\u003e{{ title }}\u003c/div\u003e\n    \u003c!-- copy your demo source code --\u003e\n    \u003cdiv @click=\"navigator.clipboard.writeText(code)\"\u003e\n      Copy Code\n    \u003c/div\u003e\n    \u003c!-- The description is rendered in the desc slot --\u003e\n    \u003cslot name=\"md:description\" /\u003e\n    \u003c!-- The demo is rendered in the default slot --\u003e\n    \u003cslot /\u003e\n    \u003c!-- highlighted code for the demo --\u003e\n    \u003cslot name=\"md:typescript\" /\u003e\n    \u003c!-- or --\u003e\n    \u003cslot name=\"md:javascript\" /\u003e\n  \u003c/div\u003e\n\u003c/template\u003e\n```\n\nOther `props` will not be processed and will be directly passed to the `\u003cdemo-container\u003e` component. For example, you can customize whether the code is expanded using the `prop`:\n\n```markdown\n\u003cdemo src=\"../demo.vue\" expand /\u003e\n```\n\n\u003e however, it is important to note that `\u003cdemo\u003e` is not strictly a component and cannot handle excessively complex custom `props`, such as `v-bind`.\n\n```ts\nconst props = defineProps\u003c{\n  // ...\n  expand: boolean\n}\u003e()\n```\n\n## Metadata\n\nThe `demo-container` component will receive relevant information about the demo. You can use the `metadata` to access and use this information within the demo:\n\n```vue\n\u003cscript lang=\"ts\" setup\u003e\nconst props = defineProps\u003c{\n  typescript: string\n  javascript: string\n  title: string\n  // metadata returns information about the demo during build (absolutePath, relativePath, fileName)\n  metadata: object\n}\u003e()\nconst githubBlobUrl = 'https://www.github.com/.../tree/main/'\nconst githubPath = githubBlobUrl + props.metadata.relativePath\n\nfunction toEditGithubDemoFile() {\n  window.open(githubPath)\n}\n\u003c/script\u003e\n```\n\n## CodeSandbox\n\nYou can define the parameters for CodeSandbox by using `codesandbox/lib/api/define` and create a sandbox environment by submitting them to the CodeSandbox API through a `\u003cform\u003e`:\n\n```vue\n\u003cscript lang=\"ts\" setup\u003e\nimport { getParameters } from 'codesandbox/lib/api/define'\n\nconst props = defineProps\u003c{\n  typescript: string\n  javascript: string\n  // ...\n}\u003e()\n\n// Compute the parameters for CodeSandbox\nconst parameters = computed(() =\u003e {\n  return getParameters({\n    files: {\n      'package.json': {\n        // specify your dependencies\n        content: { dependencies: { vue: 'latest' } },\n      },\n      'index.html': { content: `\u003cdiv id=\"app\"\u003e\u003c/div\u003e` },\n      'App.vue': { content: decodeURIComponent(props.javascript) },\n      'src/main.js': { content: '...' },\n    },\n  })\n})\n\u003c/script\u003e\n\n\u003ctemplate\u003e\n  \u003c!-- Form to submit the parameters to CodeSandbox --\u003e\n  \u003cform action=\"https://codesandbox.io/api/v1/sandboxes/define\" method=\"POST\" target=\"_blank\"\u003e\n    \u003cinput type=\"hidden\" name=\"parameters\" :value=\"parameters\"\u003e\n    \u003cbutton\u003eEdit in CodeSandbox\u003c/button\u003e\n  \u003c/form\u003e\n\u003c/template\u003e\n```\n\n## Development\n\n```bash\npnpm install\n\n# Run development server\npnpm dev\n\n# Have fun!\npnpm play\n```\n\n\u003e Unit tests are in progress, PRs welcome!\n\n## Acknowledgements\n\nThis project draws inspiration from the following projects:\n\n- [ruabick](https://github.com/dewfall123/ruabick)\n- [vitepress-demo-preview](https://github.com/flingyp/vitepress-demo-preview)\n- [create-vitepress-demo](https://github.com/bowencool/create-vitepress-demo)\n- [naive-ui](https://github.com/tusen-ai/naive-ui)\n- [element-plus](https://github.com/element-plus/element-plus)\n\n## License\n\n[MIT](./LICENSE) License © [Hairyf](https://github.com/hairyf)\n\n\u003c!-- Badges --\u003e\n\n[npm-version-src]: https://img.shields.io/npm/v/vitepress-plugin-demo?style=flat\u0026colorA=080f12\u0026colorB=1fa669\n[npm-version-href]: https://npmjs.com/package/vitepress-plugin-demo\n[npm-downloads-src]: https://img.shields.io/npm/dm/vitepress-plugin-demo?style=flat\u0026colorA=080f12\u0026colorB=1fa669\n[npm-downloads-href]: https://npmjs.com/package/vitepress-plugin-demo\n[bundle-src]: https://img.shields.io/bundlephobia/minzip/vitepress-plugin-demo?style=flat\u0026colorA=080f12\u0026colorB=1fa669\u0026label=minzip\n[bundle-href]: https://bundlephobia.com/result?p=vitepress-plugin-demo\n[license-src]: https://img.shields.io/github/license/hairyf/vitepress-plugin-demo.svg?style=flat\u0026colorA=080f12\u0026colorB=1fa669\n[license-href]: https://github.com/hairyf/vitepress-plugin-demo/blob/main/LICENSE\n[jsdocs-src]: https://img.shields.io/badge/jsdocs-reference-080f12?style=flat\u0026colorA=080f12\u0026colorB=1fa669\n[jsdocs-href]: https://www.jsdocs.io/package/vitepress-plugin-demo\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhairyf%2Fvitepress-plugin-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhairyf%2Fvitepress-plugin-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhairyf%2Fvitepress-plugin-demo/lists"}