├── docs ├── README.md ├── examples │ └── counter │ │ ├── App.vue │ │ ├── index.js │ │ └── Counter.vue └── .vuepress │ └── config.js ├── .eslintrc ├── lib ├── index.js ├── client.js ├── ExamplePreviewDemoWrapper.vue ├── icons │ ├── play-window.svg │ ├── brackets.svg │ └── codesandbox.svg ├── ExamplePreviewFileContent.vue ├── ExamplePreviewBarButton.vue ├── highlight.js ├── ExamplePreviewBar.vue └── ExamplePreview.vue ├── .gitignore ├── .babelrc ├── .github ├── ISSUE_TEMPLATE.md ├── settings.yml ├── PULL_REQUEST_TEMPLATE.md ├── CODE_OF_CONDUCT.md └── CONTRIBUTING.md ├── .release-it.json ├── test ├── ExamplePreviewFileContent.spec.js ├── ExamplePreviewBarButton.spec.js ├── ExamplePreview.spec.js └── ExamplePreviewBar.spec.js ├── README.md ├── circle.yml ├── LICENSE └── package.json /docs/README.md: -------------------------------------------------------------------------------- 1 | # Hello VuePress 2 | 3 | 4 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "posva" 4 | ], 5 | "env": { 6 | "jest": true 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | const path = require('path') 2 | 3 | module.exports = { 4 | enhanceAppFiles: [path.resolve(__dirname, './client.js')], 5 | } 6 | -------------------------------------------------------------------------------- /lib/client.js: -------------------------------------------------------------------------------- 1 | import ExamplePreview from './ExamplePreview' 2 | 3 | export default ({ Vue }) => { 4 | Vue.component('ExamplePreview', ExamplePreview) 5 | } 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | coverage 3 | npm-debug.log 4 | yarn-error.log 5 | .nyc_output 6 | coverage.lcov 7 | dist 8 | package-lock.json 9 | .DS_Store 10 | yarn.lock 11 | -------------------------------------------------------------------------------- /docs/examples/counter/App.vue: -------------------------------------------------------------------------------- 1 | 4 | 5 | 12 | -------------------------------------------------------------------------------- /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [["env", { "targets": { "node": "current" } }]], 3 | 4 | "plugins": ["syntax-dynamic-import"], 5 | "env": { 6 | "test": { 7 | "plugins": ["dynamic-import-node"] 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 9 | -------------------------------------------------------------------------------- /docs/examples/counter/index.js: -------------------------------------------------------------------------------- 1 | import App from './App' 2 | const files = ['Counter.vue', 'App.vue'] 3 | // const files = { 4 | // 'index.html': 'index.html', 5 | // 'index.js': 'code.js' 6 | // } 7 | // const codesandbox = ['App.vue', 'Bar.vue', 'Foo.vue', 'Home.vue', 'router.js'] 8 | export { App, files } 9 | -------------------------------------------------------------------------------- /lib/ExamplePreviewDemoWrapper.vue: -------------------------------------------------------------------------------- 1 | 6 | 7 | 17 | -------------------------------------------------------------------------------- /.release-it.json: -------------------------------------------------------------------------------- 1 | { 2 | "src": { 3 | "tagName": "v%s", 4 | "commitMessage": "🔖 %s" 5 | }, 6 | "github": { 7 | "release": true, 8 | "releaseName": "🚀 Release %s", 9 | "tokenRef": "GITHUB_TOKEN" 10 | }, 11 | "npm": { 12 | "publish": true 13 | }, 14 | "changelogCommand": "git log --pretty=format:'* %s (%h)' [REV_RANGE]" 15 | } 16 | -------------------------------------------------------------------------------- /docs/examples/counter/Counter.vue: -------------------------------------------------------------------------------- 1 | 9 | 10 | 19 | -------------------------------------------------------------------------------- /test/ExamplePreviewFileContent.spec.js: -------------------------------------------------------------------------------- 1 | import ExamplePreviewFileContent from '../lib/ExamplePreviewFileContent' 2 | import { mount } from '@vue/test-utils' 3 | 4 | describe('ExamplePreviewFileContent ', () => { 5 | it('works with a language', () => { 6 | const wrapper = mount(ExamplePreviewFileContent, { 7 | propsData: { 8 | file: { 9 | name: 'index.js', 10 | content: 'let a = 0', 11 | }, 12 | }, 13 | }) 14 | expect(wrapper.text()).toBe('let a = 0') 15 | expect(wrapper.find('.language-js').exists()).toBe(true) 16 | }) 17 | }) 18 | -------------------------------------------------------------------------------- /.github/settings.yml: -------------------------------------------------------------------------------- 1 | labels: 2 | - name: bug 3 | color: ee0701 4 | - name: contribution welcome 5 | color: 0e8a16 6 | - name: discussion 7 | color: 4935ad 8 | - name: docs 9 | color: 8be281 10 | - name: enhancement 11 | color: a2eeef 12 | - name: good first issue 13 | color: 7057ff 14 | - name: help wanted 15 | color: 008672 16 | - name: question 17 | color: d876e3 18 | - name: wontfix 19 | color: ffffff 20 | - name: WIP 21 | color: ffffff 22 | - name: need repro 23 | color: c9581c 24 | - name: feature request 25 | color: fbca04 26 | -------------------------------------------------------------------------------- /docs/.vuepress/config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | title: 'vuepress-plugin-example-preview', 3 | 4 | description: 'Easily embed example code alongside their preview', 5 | 6 | head: [ 7 | ['meta', { name: 'theme-color', content: '#3eaf7c' }], 8 | ['meta', { name: 'apple-mobile-web-app-capable', content: 'yes' }], 9 | ['meta', { name: 'apple-mobile-web-app-status-bar-style', content: 'black' }], 10 | ], 11 | 12 | themeConfig: { 13 | repo: 'posva/vuepress-plugin-example-preview', 14 | editLinks: true, 15 | docsDir: 'docs', 16 | editLinkText: 'Edit this page on GitHub', 17 | lastUpdated: 'Last Updated', 18 | }, 19 | 20 | plugins: [require('../../lib/index')], 21 | } 22 | -------------------------------------------------------------------------------- /lib/icons/play-window.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Example Preview plugin for Vuepress [![Build Status](https://badgen.net/circleci/github/posva/vuepress-plugin-example-preview)](https://circleci.com/gh/posva/vuepress-plugin-example-preview) [![npm package](https://badgen.net/npm/v/vuepress-plugin-example-preview)](https://www.npmjs.com/package/vuepress-plugin-example-preview) [![coverage](https://badgen.net/codecov/c/github/posva/vuepress-plugin-example-preview)](https://codecov.io/github/posva/vuepress-plugin-example-preview) [![thanks](https://badgen.net/badge/thanks/♥/pink)](https://github.com/posva/thanks) 2 | 3 | > Easily embed example code alongside their preview 4 | 5 | ## Installation 6 | 7 | ```sh 8 | npm install vuepress-plugin-example-preview 9 | ``` 10 | 11 | ## Usage 12 | 13 | ## API 14 | 15 | ## Related 16 | 17 | ## License 18 | 19 | [MIT](http://opensource.org/licenses/MIT) 20 | -------------------------------------------------------------------------------- /lib/ExamplePreviewFileContent.vue: -------------------------------------------------------------------------------- 1 | 4 | 5 | 27 | 28 | 43 | -------------------------------------------------------------------------------- /lib/icons/brackets.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /circle.yml: -------------------------------------------------------------------------------- 1 | # Javascript Node CircleCI 2.0 configuration file 2 | # 3 | # Check https://circleci.com/docs/2.0/language-javascript/ for more details 4 | # 5 | version: 2 6 | 7 | 8 | jobs: 9 | build: 10 | docker: 11 | - image: circleci/node:10 12 | 13 | working_directory: ~/repo 14 | 15 | steps: 16 | - checkout 17 | 18 | # Download and cache dependencies 19 | - restore_cache: 20 | keys: 21 | - dependencies-cache-{{ checksum "yarn.lock" }} 22 | # fallback to using the latest cache if no exact match is found 23 | - dependencies-cache- 24 | 25 | - run: yarn install 26 | 27 | - save_cache: 28 | paths: 29 | - node_modules 30 | key: dependencies-cache-{{ checksum "yarn.lock" }} 31 | 32 | # run tests! 33 | - run: npm test 34 | 35 | - run: 36 | name: Send code coverage 37 | command: './node_modules/.bin/codecov' 38 | -------------------------------------------------------------------------------- /lib/icons/codesandbox.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Eduardo San Martin Morote 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 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | 8 | 9 | **What kind of change does this PR introduce?** (check at least one) 10 | 11 | - [ ] Bugfix 12 | - [ ] Feature 13 | - [ ] Code style update 14 | - [ ] Refactor 15 | - [ ] Build-related changes 16 | - [ ] Other, please describe: 17 | 18 | **Does this PR introduce a breaking change?** (check one) 19 | 20 | - [ ] Yes 21 | - [ ] No 22 | 23 | If yes, please describe the impact and migration path for existing applications: 24 | 25 | **The PR fulfills these requirements:** 26 | 27 | - [ ] When resolving a specific issue, it's referenced in the PR's title (e.g. `fix #xxx[,#xxx]`, where "xxx" is the issue number) 28 | - [ ] All tests are passing 29 | - [ ] New/updated tests are included 30 | 31 | If adding a **new feature**, the PR's description includes: 32 | - [ ] A convincing reason for adding this feature (to avoid wasting your time, it's best to open a suggestion issue first and wait for approval before working on it) 33 | 34 | **Other information:** 35 | -------------------------------------------------------------------------------- /test/ExamplePreviewBarButton.spec.js: -------------------------------------------------------------------------------- 1 | import ExamplePreviewBarButton from '../lib/ExamplePreviewBarButton' 2 | import bracketsSvg from '../lib/icons/brackets.svg' 3 | import codesandboxSvg from '../lib/icons/codesandbox.svg' 4 | import { mount } from '@vue/test-utils' 5 | 6 | const tick = () => new Promise(resolve => process.nextTick(resolve)) 7 | 8 | describe('ExamplePreviewBarButton ', () => { 9 | it('lazy loads the svg', async () => { 10 | /** @type {import('@vue/test-utils').Wrapper} */ 11 | const wrapper = mount(ExamplePreviewBarButton, { 12 | propsData: { 13 | icon: 'brackets', 14 | }, 15 | }) 16 | expect(wrapper.vm.svg).toBe('?') 17 | await tick() 18 | expect(wrapper.vm.svg).toBe(bracketsSvg) 19 | }) 20 | 21 | it('lazy loads the svg when prop icon changes', async () => { 22 | /** @type {import('@vue/test-utils').Wrapper} */ 23 | const wrapper = mount(ExamplePreviewBarButton, { 24 | propsData: { 25 | icon: 'brackets', 26 | }, 27 | }) 28 | await tick() 29 | wrapper.setProps({ icon: 'codesandbox' }) 30 | await tick() 31 | expect(wrapper.vm.svg).toBe(codesandboxSvg) 32 | }) 33 | }) 34 | -------------------------------------------------------------------------------- /lib/ExamplePreviewBarButton.vue: -------------------------------------------------------------------------------- 1 | 4 | 5 | 29 | 30 | 65 | -------------------------------------------------------------------------------- /test/ExamplePreview.spec.js: -------------------------------------------------------------------------------- 1 | import ExamplePreview from '../lib/ExamplePreview' 2 | import { mount } from '@vue/test-utils' 3 | 4 | const tick = () => new Promise(resolve => process.nextTick(resolve)) 5 | 6 | const mocks = { 7 | $localePath: '', 8 | } 9 | 10 | const stubs = { 11 | ExamplePreviewBarButton: { 12 | props: ['icon'], 13 | render (h) { 14 | return h('button', {}, this.icon) 15 | }, 16 | }, 17 | } 18 | 19 | describe('ExamplePreview', () => { 20 | /** @type {import('@vue/test-utils').Wrapper} */ 21 | let wrapper 22 | beforeEach(() => { 23 | wrapper = mount(ExamplePreview, { 24 | mocks, 25 | stubs, 26 | propsData: { 27 | name: 'counter', 28 | }, 29 | }) 30 | return tick() 31 | }) 32 | 33 | it('initialView defaults to demo', () => { 34 | expect(wrapper.vm.viewCode).toBe(false) 35 | }) 36 | 37 | it('initialView changes viewCode', () => { 38 | wrapper = mount(ExamplePreview, { 39 | mocks, 40 | stubs, 41 | propsData: { 42 | initialView: 'code', 43 | name: 'counter', 44 | }, 45 | }) 46 | expect(wrapper.vm.viewCode).toBe(true) 47 | }) 48 | 49 | it('populates files on creation', () => { 50 | expect(wrapper.vm.files).toHaveLength(2) 51 | expect(wrapper.vm.demoComponent).toBeTruthy() 52 | expect(wrapper.vm.currentFile).toMatchObject({ name: 'Counter.vue' }) 53 | }) 54 | }) 55 | -------------------------------------------------------------------------------- /.github/CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. 4 | 5 | We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion. 6 | 7 | Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct. 8 | 9 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team. 10 | 11 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. 12 | 13 | This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/) 14 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are welcome and will be fully credited! 4 | 5 | We accept contributions via Pull Requests on [Github](https://github.com/{{ githubAccount }}/{{ name }}). 6 | 7 | ## Pull Requests 8 | 9 | Here are some guidelines to make the process smoother: 10 | 11 | - **Add a test** - New features and bugfixes need tests. If you find it difficult to test, please tell us in the pull request and we will try to help you! 12 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 13 | - **Run `npm test` locally** - This will allow you to go faster 14 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 15 | - **Send coherent history** - Make sure your commits message means something 16 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. 17 | 18 | ## Creating issues 19 | 20 | ### Bug reports 21 | 22 | Always try to provide as much information as possible. If you are reporting a bug, try to provide a repro on jsfiddle.net (or anything else) or a stacktrace at the very least. This will help us check the problem quicker. 23 | 24 | ### Feature requests 25 | 26 | Lay out the reasoning behind it and propose an API for it. Ideally, you should have a practical example to prove the utility of the feature you're requesting. 27 | 28 | -------------------------------------------------------------------------------- /lib/highlight.js: -------------------------------------------------------------------------------- 1 | const escapeHtml = require('escape-html') 2 | // import escapeHtml from 'escape-html' 3 | // const prism = require('prismjs') 4 | // const loadLanguages = require('prismjs/components/index') 5 | // const { logger, chalk, escapeHtml } = require('@vuepress/shared-utils') 6 | 7 | // required to make embedded highlighting work... 8 | // loadLanguages(['markup', 'css', 'javascript']) 9 | 10 | function wrap (code, lang) { 11 | if (lang === 'text') { 12 | code = escapeHtml(code) 13 | } 14 | return `
${code}
` 15 | } 16 | 17 | module.exports = wrap 18 | 19 | // function highlightCode (str, lang) { 20 | // if (!lang) { 21 | // return wrap(str, 'text') 22 | // } 23 | // lang = lang.toLowerCase() 24 | // const rawLang = lang 25 | // if (lang === 'vue' || lang === 'html') { 26 | // lang = 'markup' 27 | // } 28 | // if (lang === 'md') { 29 | // lang = 'markdown' 30 | // } 31 | // if (lang === 'ts') { 32 | // lang = 'typescript' 33 | // } 34 | // if (lang === 'py') { 35 | // lang = 'python' 36 | // } 37 | // if (!prism.languages[lang]) { 38 | // try { 39 | // // loadLanguages([lang]) 40 | // } catch (e) { 41 | // console.warn(`[vuepress] Syntax highlight for language "${lang}" is not supported.`) 42 | // } 43 | // } 44 | // if (prism.languages[lang]) { 45 | // const code = prism.highlight(str, prism.languages[lang], lang) 46 | // return wrap(code, rawLang) 47 | // } 48 | // return wrap(str, 'text') 49 | // } 50 | -------------------------------------------------------------------------------- /test/ExamplePreviewBar.spec.js: -------------------------------------------------------------------------------- 1 | import ExamplePreviewBar from '../lib/ExamplePreviewBar' 2 | import { mount } from '@vue/test-utils' 3 | 4 | const files = [ 5 | { name: 'index.js', content: 'let a = 0' }, 6 | { name: 'bar.js', content: `let bar = 'bar'` }, 7 | { name: 'App.vue', content: `