|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "os/signal" |
| 7 | + "syscall" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/mhersson/mpls/internal/previewserver" |
| 11 | + "github.com/mhersson/mpls/pkg/parser" |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + demoWait time.Duration |
| 17 | + demoNoAuto bool |
| 18 | +) |
| 19 | + |
| 20 | +const demoMarkdown = `# Theme and Feature Showcase |
| 21 | +
|
| 22 | +Demonstrating some of the **mpls** markdown rendering features. |
| 23 | +
|
| 24 | +> [!IMPORTANT] |
| 25 | +> GitHub-style alerts supported - emojis work too! :smile: :rocket: |
| 26 | +
|
| 27 | +<img src="./assets/yoshi.png" alt="yoshi" style="float:right; width:250px; margin-right: 100px; margin-top:80px;"> |
| 28 | +
|
| 29 | +## Tables & Images |
| 30 | +
|
| 31 | +> [!TIP] |
| 32 | +> Images support both ` + "`![]()`" + ` and ` + "`<img style=\"...\">`" + ` syntax. |
| 33 | +
|
| 34 | +| Feature | Status | Notes | |
| 35 | +|---------|:------:|----------:| |
| 36 | +| Tables | ✓ | GFM style | |
| 37 | +| Mermaid | ✓ | Diagrams | |
| 38 | +| KaTeX | ✓ | Math | |
| 39 | +
|
| 40 | +## Code |
| 41 | +
|
| 42 | +` + "```go\nfunc main() {\n fmt.Println(\"Hello, mpls!\")\n}\n```" + ` |
| 43 | +
|
| 44 | +<div style="display:flex; gap:40px;"> |
| 45 | +<div> |
| 46 | +
|
| 47 | +## Lists, Tasks & Math :notebook: |
| 48 | +
|
| 49 | +- Unordered item |
| 50 | +- Another with **bold** |
| 51 | +
|
| 52 | +1. First ordered |
| 53 | +2. Second ordered |
| 54 | +
|
| 55 | +- [x] Completed task |
| 56 | +- [ ] Pending task |
| 57 | +
|
| 58 | +</div> |
| 59 | +<div style="flex:1; display:flex; justify-content:center; align-items:center;"> |
| 60 | +
|
| 61 | +$$\int_{-\infty}^{\infty} e^{-x^2} dx = \sqrt{\pi}$$ |
| 62 | +
|
| 63 | +</div> |
| 64 | +</div> |
| 65 | +
|
| 66 | +## Mermaid Diagrams |
| 67 | +
|
| 68 | +` + "```mermaid\n" + |
| 69 | + "flowchart LR\n" + |
| 70 | + " subgraph Editor\n" + |
| 71 | + " E[📝 Edit Markdown]\n" + |
| 72 | + " end\n" + |
| 73 | + " subgraph mpls\n" + |
| 74 | + " L[LSP Server]\n" + |
| 75 | + " P[Parser]\n" + |
| 76 | + " W[WebSocket]\n" + |
| 77 | + " L --> P --> W\n" + |
| 78 | + " end\n" + |
| 79 | + " subgraph Browser\n" + |
| 80 | + " V[🌐 Live Preview]\n" + |
| 81 | + " end\n" + |
| 82 | + " E -->|didChange| L\n" + |
| 83 | + " W -->|update| V\n" + |
| 84 | + " style E fill:#6c9,stroke:#485,color:#333\n" + |
| 85 | + " style L fill:#69c,stroke:#458,color:#333\n" + |
| 86 | + " style P fill:#c6c,stroke:#848,color:#333\n" + |
| 87 | + " style W fill:#69c,stroke:#458,color:#333\n" + |
| 88 | + " style V fill:#f96,stroke:#b64,color:#333\n" + |
| 89 | + "```" + ` |
| 90 | +` |
| 91 | + |
| 92 | +var demoCmd = &cobra.Command{ |
| 93 | + Use: "demo", |
| 94 | + Short: "Start a demo preview server with sample content", |
| 95 | + Long: `Start a standalone preview server with sample markdown content. |
| 96 | +Useful for taking screenshots of different themes. |
| 97 | +
|
| 98 | +Example: |
| 99 | + mpls demo --theme catppuccin-mocha --wait 10s |
| 100 | + mpls demo --theme dark --port 9999 --no-auto`, |
| 101 | + Run: func(_ *cobra.Command, _ []string) { |
| 102 | + // Get current working directory for workspace root |
| 103 | + cwd, err := os.Getwd() |
| 104 | + if err != nil { |
| 105 | + fmt.Fprintf(os.Stderr, "Failed to get working directory: %v\n", err) |
| 106 | + os.Exit(1) |
| 107 | + } |
| 108 | + |
| 109 | + // Set code highlighting style based on theme |
| 110 | + if chromaStyle := previewserver.GetChromaStyleForTheme(previewserver.Theme); chromaStyle != "" { |
| 111 | + parser.CodeHighlightingStyle = chromaStyle |
| 112 | + } |
| 113 | + |
| 114 | + // Enable emoji support for demo |
| 115 | + parser.EnableEmoji = true |
| 116 | + |
| 117 | + // Set workspace root for relative path resolution |
| 118 | + parser.WorkspaceRoot = cwd |
| 119 | + |
| 120 | + // Render demo markdown |
| 121 | + demoURI := "file://" + cwd + "/demo.md" |
| 122 | + html, meta := parser.HTML(demoMarkdown, demoURI, 0) |
| 123 | + |
| 124 | + // Create and configure preview server |
| 125 | + server := previewserver.New() |
| 126 | + server.SetWorkspaceRoot(cwd) |
| 127 | + |
| 128 | + // Pre-populate content so it's available when browser connects |
| 129 | + server.Update("demo.md", html, meta) |
| 130 | + |
| 131 | + // Start server in background |
| 132 | + go server.Start() |
| 133 | + |
| 134 | + url := fmt.Sprintf("http://localhost:%d", server.Port) |
| 135 | + fmt.Printf("Demo server running at %s (theme: %s)\n", url, previewserver.Theme) |
| 136 | + |
| 137 | + // Open browser unless --no-auto |
| 138 | + if !demoNoAuto { |
| 139 | + if err := previewserver.Openbrowser(url, previewserver.Browser); err != nil { |
| 140 | + fmt.Printf("Failed to open browser: %v\n", err) |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + // Wait for specified duration |
| 145 | + if demoWait > 0 { |
| 146 | + fmt.Printf("Waiting %s before exit...\n", demoWait) |
| 147 | + time.Sleep(demoWait) |
| 148 | + server.Stop() |
| 149 | + } else { |
| 150 | + fmt.Println("Press Ctrl+C to stop.") |
| 151 | + |
| 152 | + sigChan := make(chan os.Signal, 1) |
| 153 | + signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) |
| 154 | + <-sigChan |
| 155 | + |
| 156 | + fmt.Println("\nShutting down...") |
| 157 | + server.Stop() |
| 158 | + } |
| 159 | + }, |
| 160 | +} |
| 161 | + |
| 162 | +func init() { |
| 163 | + demoCmd.Flags().DurationVar(&demoWait, "wait", 0, "Duration to keep server running (0 = run until interrupted)") |
| 164 | + demoCmd.Flags().BoolVar(&demoNoAuto, "no-auto", false, "Don't open browser automatically") |
| 165 | + |
| 166 | + // Theme, port, and browser flags are defined on root command |
| 167 | + // They work via the global previewserver variables |
| 168 | +} |
0 commit comments