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

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

Latest version **1.0.4** (published 2020-04-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install @vuepress-reco/vuepress-plugin-extract-code
pnpm add @vuepress-reco/vuepress-plugin-extract-code
yarn add @vuepress-reco/vuepress-plugin-extract-code
bun add @vuepress-reco/vuepress-plugin-extract-code
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2020-04-18 |
| First published | 2019-11-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 29.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 17 |
| Author | reco_luan |
| Maintainers | reco_luan, smallsunnyfox |
| Keywords | vuepress-theme-reco, extract-code, vuepress |

## Links

- npm: https://www.npmjs.com/package/@vuepress-reco/vuepress-plugin-extract-code
- Repository: https://github.com/vuepress-reco/vuepress-plugin-extract-code
- Homepage: https://vuepress-theme-reco.recoluan.com
- Issues: https://github.com/vuepress-reco/vuepress-plugin-extract-code/issues
- npm.io page: https://npm.io/package/@vuepress-reco/vuepress-plugin-extract-code

## Recent versions

- 1.0.4 (latest) — 2020-04-18
- 1.0.3 — 2019-11-03
- 1.0.2 — 2019-11-01
- 1.0.1 — 2019-11-01
- 1.0.0 — 2019-11-01

## README

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

## Introduce

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

![demo](./bin/demo.png)

### Name

- **As plugin**: `@vuepress-reco/vuepress-plugin-extract-code`

## Usage

### 修改 `config.js`

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

### 展示 `VUE` 组件

> `@` 是当前项目目录的别名

```html
<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`，然后将文件指向对应的文件即可。

```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` 标签去展示效果截图

  ```html
  <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` 组件的话，可以这样来展示：

```html
<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. 另外，如果你想要展示的案例代码就是一个独立的可以显示效果的功能组件，那最方便不过了，因为这个组件既可以作为代码来源，又可以用来显示效果：

```html
<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>
```

### 展示不具名的代码

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

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

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

<RecoDemo :collapse="true">
<template slot="code-js">
<pre>
const a = 1
a.b = 1
</pre>
</template>
</RecoDemo>

### 默认显示代码块

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

```html
<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>
```

---
_Source: https://npm.io/package/@vuepress-reco/vuepress-plugin-extract-code · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
