1 Star 0 Fork 0

vuepress-reco/vuepress-plugin-extract-code

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

@vuepress-reco/vuepress-plugin-extract-code

Introduce

Features and code presentation plugin for vuepress-theme-reco or other vuepress theme.

demo

Name

  • As plugin: @vuepress-reco/vuepress-plugin-extract-code

Usage

修改 config.js

module.exports = {
  plugins: [
    '@vuepress-reco/extract-code'
  ]
}

展示 VUE 组件

@ 是当前项目目录的别名

<RecoDemo>
  <template slot="code-template">
    <<< @/docs/.vuepress/demo/demo.vue?template
  </template>
  <template slot="code-script">
    <<< @/docs/.vuepress/demo/demo.vue?script
  </template>
  <template slot="code-style">
    <<< @/docs/.vuepress/demo/demo.vue?style
  </template>
</RecoDemo>

随意组合想展示的代码

比如,你想展示 html 代码,需要将插槽名字改为 code-html,然后将文件指向对应的文件即可。

<RecoDemo>
  <template slot="code-html">
    <<< @/docs/.vuepress/config/nav/index.html
  </template>
  <template slot="code-css">
    <<< @/docs/.vuepress/config/nav/style.css
  </template>
  <template slot="code-js">
    <<< @/docs/.vuepress/config/nav/index.js
  </template>
</RecoDemo>

展示代码效果

如果你想同时展示你的案例的显示效果,可以通过 demo 这个插槽来操作:

1. 可以通过 img 标签去展示效果截图

<RecoDemo>
  <template slot="code-template">
    <<< @/docs/.vuepress/demo/demo.vue?template
  </template>
  <template slot="code-script">
    <<< @/docs/.vuepress/demo/demo.vue?script
  </template>
  <template slot="code-style">
    <<< @/docs/.vuepress/demo/demo.vue?style
  </template>

  <img src="./images/demo.png" slot="demo" />
</RecoDemo>

2. 当然,如果可以将展示效果写成一个 vue 组件的话,可以这样来展示:

<RecoDemo>
  <template slot="code-template">
    <<< @/docs/.vuepress/demo/demo.vue?template
  </template>
  <template slot="code-script">
    <<< @/docs/.vuepress/demo/demo.vue?script
  </template>
  <template slot="code-style">
    <<< @/docs/.vuepress/demo/demo.vue?style
  </template>

  <some-demo slot="demo"></some-demo>
</RecoDemo>

只要你的组件可以在 .md 文件中正常使用就可以,你可以把这些组件放在 .vuepress/components 下;可以通过官方注册插件 @vuepress/plugin-register-components 去注册组件;可以通过组件的方式去注入。

3. 另外,如果你想要展示的案例代码就是一个独立的可以显示效果的功能组件,那最方便不过了,因为这个组件既可以作为代码来源,又可以用来显示效果:

<RecoDemo>
  <template slot="code-template">
    <<< @/docs/.vuepress/demo/some-demo.vue?template
  </template>
  <template slot="code-script">
    <<< @/docs/.vuepress/demo/some-demo.vue?script
  </template>
  <template slot="code-style">
    <<< @/docs/.vuepress/demo/some-demo.vue?style
  </template>

  <some-demo slot="demo"></some-demo>
</RecoDemo>

展示不具名的代码

如果你的代码只是某个代码块,可以直接在文本里写,这样很方便,不需要去建立一个文件,但是这样不会显示代码高亮的。

<RecoDemo :collapse="true">
  <template slot="code-js">
    <pre>
      console.log(`I'm reco_luan.`)
    </pre>
  </template>
</RecoDemo>

但是这样还有一个缺点,就是会有多余的代码缩进,可以这样粗暴解决:

默认显示代码块

代码展示默认是隐藏的,点击左上角的按钮才可以显示,可以把 collapse 设置为 true 来默认显示代码:

<RecoDemo :collapse="true">
  <template slot="code-template">
    <<< @/docs/.vuepress/demo/some-demo.vue?template
  </template>
  <template slot="code-script">
    <<< @/docs/.vuepress/demo/some-demo.vue?script
  </template>
  <template slot="code-style">
    <<< @/docs/.vuepress/demo/some-demo.vue?style
  </template>

  <some-demo slot="demo"></some-demo>
</RecoDemo>
MIT License Copyright (c) 2019 vuepress-reco Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Features and code presentation plugin for vuepress-theme-reco or other vuepress theme. 展开 收起
README
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/vuepress-reco/vuepress-plugin-extract-code.git
git@gitee.com:vuepress-reco/vuepress-plugin-extract-code.git
vuepress-reco
vuepress-plugin-extract-code
vuepress-plugin-extract-code
master

搜索帮助