|
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | +# Hexo Codapi Plugin |
| 7 | + |
| 8 | +Embed **live, executable code snippets** in your Hexo blog using |
| 9 | +[Codapi](https://codapi.org). |
| 10 | + |
| 11 | +This plugin provides: |
| 12 | + |
| 13 | +- A `{% codapi %}` **tag plugin** for author-friendly usage in Markdown |
| 14 | +- **Automatic, per-page injection** of Codapi JS/CSS (only on pages that need it) |
| 15 | +- Theme-agnostic integration (no theme edits required) |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## Features |
| 20 | + |
| 21 | +- Clean author syntax (`{% codapi %}`) |
| 22 | +- Outputs Codapi-compatible HTML (`<pre>` + `<codapi-snippet>`) |
| 23 | +- Injects assets **only on pages containing Codapi snippets** |
| 24 | +- Configurable defaults and CDN URLs |
| 25 | +- Works with any Hexo theme |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +## Installation |
| 30 | + |
| 31 | +```bash |
| 32 | +npm install hexo-codapi |
| 33 | +``` |
| 34 | + |
| 35 | +### Hexo 8.x Users (Required) |
| 36 | + |
| 37 | +If you're using Hexo 8.0 or later, you need to explicitly load the plugin. Create a file `scripts/load-codapi.js` in your Hexo blog directory: |
| 38 | + |
| 39 | +```javascript |
| 40 | +// scripts/load-codapi.js |
| 41 | +const plugin = require('hexo-codapi'); |
| 42 | +plugin(hexo); |
| 43 | +``` |
| 44 | + |
| 45 | +This is due to a change in Hexo 8.x's plugin loading mechanism. |
| 46 | + |
| 47 | +### Hexo 5.x - 7.x Users |
| 48 | + |
| 49 | +For older Hexo versions, the plugin loads automatically - no additional setup needed. |
| 50 | + |
| 51 | +Then rebuild: |
| 52 | + |
| 53 | +```bash |
| 54 | +hexo clean && hexo generate |
| 55 | +``` |
| 56 | + |
| 57 | +--- |
| 58 | + |
| 59 | +## Usage |
| 60 | + |
| 61 | +In any post or page: |
| 62 | + |
| 63 | +```text |
| 64 | +{% codapi go basic %} |
| 65 | +println("Hello, Codapi!") |
| 66 | +{% endcodapi %} |
| 67 | +``` |
| 68 | + |
| 69 | +This renders an executable Go snippet powered by Codapi. |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +## Syntax |
| 74 | + |
| 75 | +```text |
| 76 | +{% codapi <sandbox> <editor> [engine=...] [selector=...] %} |
| 77 | +<code> |
| 78 | +{% endcodapi %} |
| 79 | +``` |
| 80 | + |
| 81 | +### Parameters |
| 82 | + |
| 83 | +| Parameter | Description | Default | |
| 84 | +| ---------- | ------------------------------------------ | ------- | |
| 85 | +| `sandbox` | Codapi sandbox (e.g. `go`, `js`, `bash`) | `go` | |
| 86 | +| `editor` | Editor UI (`basic`, `full`) | `basic` | |
| 87 | +| `engine` | Optional execution engine | *(none)* | |
| 88 | +| `selector` | Attach Codapi to an existing element | *(none)* | |
| 89 | + |
| 90 | +### Examples |
| 91 | + |
| 92 | +Basic Go: |
| 93 | + |
| 94 | +```text |
| 95 | +{% codapi go %} |
| 96 | +println("Hello World") |
| 97 | +{% endcodapi %} |
| 98 | +``` |
| 99 | + |
| 100 | +JavaScript with browser engine: |
| 101 | + |
| 102 | +```text |
| 103 | +{% codapi js basic engine=browser %} |
| 104 | +console.log("Hello from the browser") |
| 105 | +{% endcodapi %} |
| 106 | +``` |
| 107 | + |
| 108 | +--- |
| 109 | + |
| 110 | +## Configuration |
| 111 | + |
| 112 | +Add to your Hexo `_config.yml`: |
| 113 | + |
| 114 | +```yml |
| 115 | +codapi: |
| 116 | + version: 0.20.0 |
| 117 | + default_sandbox: go |
| 118 | + default_editor: basic |
| 119 | +``` |
| 120 | +
|
| 121 | +### Optional overrides |
| 122 | +
|
| 123 | +```yml |
| 124 | +codapi: |
| 125 | + js: https://unpkg.com/@antonz/codapi@0.20.0/dist/snippet.js |
| 126 | + css: https://unpkg.com/@antonz/codapi@0.20.0/dist/snippet.css |
| 127 | + priority: 10 |
| 128 | +``` |
| 129 | +
|
| 130 | +| Option | Description | |
| 131 | +| ------------ | ------------------------------------------ | |
| 132 | +| `version` | Codapi package version (used for CDN URLs) | |
| 133 | +| `js` / `css` | Override CDN URLs | |
| 134 | +| `priority` | Hexo filter priority (`after_render:html`) | |
| 135 | + |
| 136 | +### Self-hosting Codapi assets |
| 137 | + |
| 138 | +To self-host `snippet.js` and `snippet.css` instead of using the CDN: |
| 139 | + |
| 140 | +1. **Download the assets** from the [Codapi releases](https://github.com/nalgeon/codapi/releases) or unpkg: |
| 141 | + ```bash |
| 142 | + # Create assets directory in your Hexo source |
| 143 | + mkdir -p source/assets/codapi |
| 144 | +
|
| 145 | + # Download the files |
| 146 | + curl -o source/assets/codapi/snippet.js https://unpkg.com/@antonz/codapi@0.20.0/dist/snippet.js |
| 147 | + curl -o source/assets/codapi/snippet.css https://unpkg.com/@antonz/codapi@0.20.0/dist/snippet.css |
| 148 | + ``` |
| 149 | + |
| 150 | +2. **Update your `_config.yml`** to point to the self-hosted files: |
| 151 | + ```yml |
| 152 | + codapi: |
| 153 | + js: /assets/codapi/snippet.js |
| 154 | + css: /assets/codapi/snippet.css |
| 155 | + ``` |
| 156 | + |
| 157 | +The plugin will inject these paths instead of CDN URLs. Hexo will automatically copy files from `source/assets/` to `public/assets/` during generation. |
| 158 | + |
| 159 | +--- |
| 160 | + |
| 161 | +## How it works |
| 162 | + |
| 163 | +1. `{% codapi %}` expands to: |
| 164 | + |
| 165 | + ```html |
| 166 | + <pre>...</pre> |
| 167 | + <codapi-snippet ...></codapi-snippet> |
| 168 | + ``` |
| 169 | +2. During `after_render:html`, the plugin: |
| 170 | + |
| 171 | + * Detects `<codapi-snippet>` in the rendered page |
| 172 | + * Injects `snippet.css` into `<head>` |
| 173 | + * Injects `snippet.js` before `</body>` |
| 174 | +3. Pages without Codapi snippets remain untouched |
| 175 | + |
| 176 | +This guarantees: |
| 177 | + |
| 178 | +* No global JS/CSS bloat |
| 179 | +* Full theme compatibility |
| 180 | + |
| 181 | +--- |
| 182 | + |
| 183 | +## Notes & Limitations |
| 184 | + |
| 185 | +* Some Codapi sandboxes run fully in-browser; others require a Codapi server. |
| 186 | +* This plugin only handles **embedding**, not server configuration. |
| 187 | +* If your theme omits `</head>` or `</body>` (very rare), injection will be skipped safely. |
| 188 | + |
| 189 | +--- |
| 190 | + |
| 191 | +## License |
| 192 | + |
| 193 | +MIT |
| 194 | + |
| 195 | +--- |
| 196 | + |
| 197 | +## Credits |
| 198 | + |
| 199 | +* [Codapi](https://codapi.org) by Anton Zhiyanov |
| 200 | +* Hexo plugin API |
0 commit comments