Skip to content

Commit 3c334b3

Browse files
authored
feat: close #19, add vuepress-plugin-code-copy to support copy code (#22)
* feat: close #19, add vuepress-plugin-code-copy to support copy code * fix: prevent highlighting automatically again
1 parent 075d0e4 commit 3c334b3

File tree

6 files changed

+46
-9
lines changed

6 files changed

+46
-9
lines changed

README-zh_CN.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ module.exports = {
9999
embed: '',
100100
},
101101
demoCodeMark: 'demo-code',
102+
copyOptions: { ... },
102103
}]
103104
],
104105
}
@@ -154,6 +155,12 @@ It passes [CodeSandbox options](https://codesandbox.io/docs/importing#define-api
154155

155156
插件的标记,即跟在 `:::` 后的标记。
156157

158+
### copyOptions
159+
* 类型:`Object/Boolean`
160+
* 默认值:`{ align: 'top', selector: '.demo-and-code-wrapper div[class*="language-"] pre' }`
161+
162+
透传 [vuepress-plugin-code-copy](https://github.com/znicholasbrown/vuepress-plugin-code-copy#options) 的参数,或传 `false` 禁用它。
163+
157164
## Related
158165
* [vuepress-plugin-demo-block](https://github.com/xiguaxigua/vuepress-plugin-demo-block)
159166

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ module.exports = {
9999
embed: '',
100100
},
101101
demoCodeMark: 'demo-code',
102+
copyOptions: { ... },
102103
}]
103104
],
104105
}
@@ -154,6 +155,12 @@ It passes [CodeSandbox options](https://codesandbox.io/docs/importing#define-api
154155

155156
The mark of the plugin, follows the tag after `:::`.
156157

158+
### copyOptions
159+
* Type: `Object/Boolean`
160+
* Default: `{ align: 'top', selector: '.demo-and-code-wrapper div[class*="language-"] pre' }`
161+
162+
It passes [vuepress-plugin-code-copy](https://github.com/znicholasbrown/vuepress-plugin-code-copy#options)'s options, or `false` to disable it.
163+
157164
## Related
158165
* [vuepress-plugin-demo-block](https://github.com/xiguaxigua/vuepress-plugin-demo-block)
159166

@@ -179,4 +186,4 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
179186

180187
<!-- ALL-CONTRIBUTORS-LIST:END -->
181188

182-
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
189+
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vuepress-plugin-demo-code",
3-
"version": "0.4.2",
3+
"version": "0.5.0",
44
"description": "📝 Demo and code plugin for vuepress",
55
"main": "src/index.js",
66
"files": [
@@ -34,7 +34,8 @@
3434
"dependencies": {
3535
"codesandbox": "2.1.10",
3636
"markdown-it-container": "^2.0.0",
37-
"prismjs": "^1.17.1"
37+
"prismjs": "^1.17.1",
38+
"vuepress-plugin-code-copy": "^1.0.4"
3839
},
3940
"devDependencies": {
4041
"@babel/core": "^7.7.2",

src/DemoAndCode.vue

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,19 @@ html {
157157
.demo-and-code-wrapper {
158158
padding: 20px 0;
159159
160+
// for vuepress-plugin-code-copy
161+
.code-copy {
162+
position: absolute;
163+
top: 20px;
164+
right: 0;
165+
166+
opacity: 1;
167+
168+
svg {
169+
right: 10px;
170+
}
171+
}
172+
160173
.code-control {
161174
position: sticky;
162175
z-index: 9;

src/highlight.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
if (typeof window !== 'undefined') {
2+
// prevent highlighting automatically
3+
window.Prism = { manual: true }
4+
}
5+
16
const prism = require('prismjs')
27
const escapeHtml = require('escape-html')
38
const loadLanguages = require('prismjs/components/index')
49

5-
// prevent Prism calling `highlightAll`
6-
prism.manual = true
7-
8-
// loadLanguages(['markup', 'css', 'javascript'])
9-
1010
function wrap (code, lang) {
1111
if (lang === 'text') {
1212
code = escapeHtml(code)

src/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,20 @@ const defaults = {
1818
}
1919

2020
module.exports = (options = {}) => {
21-
const { demoCodeMark = 'demo' } = options
21+
const {
22+
demoCodeMark = 'demo',
23+
copyOptions = {
24+
align: 'top',
25+
selector: '.demo-and-code-wrapper div[class*="language-"] pre',
26+
},
27+
} = options
2228
const END_TYPE = `container_${demoCodeMark}_close`
2329

2430
return {
2531
name: 'vuepress-plugin-demo-code',
32+
plugins: [
33+
['code-copy', copyOptions],
34+
],
2635
enhanceAppFiles: [
2736
path.resolve(__dirname, 'enhanceAppFile.js'),
2837
],

0 commit comments

Comments
 (0)