opencode-mermaid-renderer 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +90 -0
  3. package/index.ts +81 -0
  4. package/package.json +46 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026
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.
package/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # opencode-mermaid-renderer
2
+
3
+ Render mermaid diagrams as beautiful ASCII art directly in OpenCode.
4
+
5
+ Powered by [beautiful-mermaid](https://github.com/lukilabs/beautiful-mermaid) from Craft.
6
+
7
+ ## Usage
8
+
9
+ Add the plugin to your `.opencode/opencode.jsonc`:
10
+
11
+ ```jsonc
12
+ {
13
+ "plugin": ["opencode-mermaid-renderer@0.0.1"]
14
+ }
15
+ ```
16
+
17
+ ## Example
18
+
19
+ When the AI generates a mermaid diagram:
20
+
21
+ ````markdown
22
+ ```mermaid
23
+ graph TD
24
+ A[Start] --> B{Decision}
25
+ B -->|Yes| C[Process]
26
+ B -->|No| D[End]
27
+ ```
28
+ ````
29
+
30
+ The plugin automatically renders it as ASCII art:
31
+
32
+ ```
33
+ ┌──────────┐
34
+ │ │
35
+ │ Start │
36
+ │ │
37
+ └─────┬────┘
38
+
39
+
40
+
41
+
42
+
43
+ ┌──────────┐
44
+ │ │
45
+ │ Decision ├──No────┐
46
+ │ │ │
47
+ └─────┬────┘ │
48
+ │ │
49
+ │ │
50
+ Yes │
51
+ │ │
52
+ ▼ ▼
53
+ ┌──────────┐ ┌─────┐
54
+ │ │ │ │
55
+ │ Process │ │ End │
56
+ │ │ │ │
57
+ └──────────┘ └─────┘
58
+ ```
59
+
60
+ ## Supported Diagram Types
61
+
62
+ - **Flowcharts** - `graph TD`, `graph LR`, `graph BT`, `graph RL`
63
+ - **State diagrams** - `stateDiagram-v2`
64
+ - **Sequence diagrams** - `sequenceDiagram`
65
+ - **Class diagrams** - `classDiagram`
66
+ - **ER diagrams** - `erDiagram`
67
+
68
+ ## Error Handling
69
+
70
+ If a mermaid diagram fails to render (invalid syntax, unsupported features), the plugin keeps the original code block and adds an HTML comment with the error:
71
+
72
+ ```markdown
73
+ ```mermaid
74
+ invalid syntax here
75
+ ```
76
+ <!-- mermaid render failed: Parse error... -->
77
+ ```
78
+
79
+ ## Requirements
80
+
81
+ - OpenCode >= 1.0.137
82
+
83
+ ## License
84
+
85
+ MIT
86
+
87
+ ## Credits
88
+
89
+ - [beautiful-mermaid](https://github.com/lukilabs/beautiful-mermaid) by Craft/Lukilabs
90
+ - Inspired by [@franlol/opencode-md-table-formatter](https://github.com/franlol/opencode-md-table-formatter)
package/index.ts ADDED
@@ -0,0 +1,81 @@
1
+ import type { Plugin, Hooks } from "@opencode-ai/plugin"
2
+ import { renderMermaidAscii } from "beautiful-mermaid"
3
+
4
+ /**
5
+ * Regex to match mermaid code blocks
6
+ * Matches: ```mermaid\n..content..\n```
7
+ */
8
+ const MERMAID_BLOCK_REGEX = /```mermaid\n([\s\S]*?)```/g
9
+
10
+ /**
11
+ * OpenCode plugin that renders mermaid diagrams as ASCII art
12
+ *
13
+ * Supported diagram types:
14
+ * - Flowcharts (graph TD/LR/BT/RL)
15
+ * - State diagrams (stateDiagram-v2)
16
+ * - Sequence diagrams (sequenceDiagram)
17
+ * - Class diagrams (classDiagram)
18
+ * - ER diagrams (erDiagram)
19
+ */
20
+ export const MermaidRenderer: Plugin = async () => {
21
+ return {
22
+ "experimental.text.complete": async (
23
+ _input: { sessionID: string; messageID: string; partID: string },
24
+ output: { text: string }
25
+ ) => {
26
+ try {
27
+ output.text = renderMermaidBlocks(output.text)
28
+ } catch (error) {
29
+ // If something goes catastrophically wrong, keep original text
30
+ // and add a comment for debugging
31
+ output.text =
32
+ output.text +
33
+ "\n\n<!-- mermaid-renderer: unexpected error - " +
34
+ (error as Error).message +
35
+ " -->"
36
+ }
37
+ },
38
+ } as Hooks
39
+ }
40
+
41
+ /**
42
+ * Find and render all mermaid code blocks in the text
43
+ */
44
+ function renderMermaidBlocks(text: string): string {
45
+ return text.replace(MERMAID_BLOCK_REGEX, (_match, mermaidCode: string) => {
46
+ return renderSingleBlock(mermaidCode.trim())
47
+ })
48
+ }
49
+
50
+ /**
51
+ * Render a single mermaid diagram to ASCII
52
+ * On error, returns the original code block with an error comment
53
+ */
54
+ function renderSingleBlock(mermaidCode: string): string {
55
+ try {
56
+ const ascii = renderMermaidAscii(mermaidCode)
57
+
58
+ // Wrap in a code block for proper formatting
59
+ return "```\n" + ascii + "\n```"
60
+ } catch (error) {
61
+ // Keep original mermaid block and add error as comment
62
+ const errorMessage = (error as Error).message || "Unknown error"
63
+ return (
64
+ "```mermaid\n" +
65
+ mermaidCode +
66
+ "\n```\n<!-- mermaid render failed: " +
67
+ escapeHtmlComment(errorMessage) +
68
+ " -->"
69
+ )
70
+ }
71
+ }
72
+
73
+ /**
74
+ * Escape characters that could break HTML comments
75
+ */
76
+ function escapeHtmlComment(text: string): string {
77
+ return text.replace(/--/g, "- -").replace(/>/g, "&gt;")
78
+ }
79
+
80
+ // Default export for OpenCode plugin system
81
+ export default MermaidRenderer
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "opencode-mermaid-renderer",
3
+ "version": "0.0.1",
4
+ "description": "Render mermaid diagrams as beautiful ASCII art in OpenCode",
5
+ "keywords": [
6
+ "opencode",
7
+ "plugin",
8
+ "mermaid",
9
+ "diagram",
10
+ "ascii",
11
+ "flowchart",
12
+ "sequence-diagram",
13
+ "state-diagram",
14
+ "class-diagram",
15
+ "er-diagram"
16
+ ],
17
+ "homepage": "https://github.com/anis-dr/opencode-mermaid-renderer#readme",
18
+ "bugs": {
19
+ "url": "https://github.com/anis-dr/opencode-mermaid-renderer/issues"
20
+ },
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/anis-dr/opencode-mermaid-renderer.git"
24
+ },
25
+ "license": "MIT",
26
+ "author": "anis-dr",
27
+ "type": "module",
28
+ "main": "index.ts",
29
+ "files": [
30
+ "index.ts",
31
+ "LICENSE",
32
+ "README.md"
33
+ ],
34
+ "scripts": {
35
+ "test": "echo \"Error: no test specified\" && exit 1"
36
+ },
37
+ "dependencies": {
38
+ "beautiful-mermaid": "^0.1.3"
39
+ },
40
+ "peerDependencies": {
41
+ "@opencode-ai/plugin": ">=0.13.7"
42
+ },
43
+ "engines": {
44
+ "node": ">=18.0.0"
45
+ }
46
+ }