|
8 | 8 | import SwiftUI |
9 | 9 | import MarkdownEngine |
10 | 10 |
|
| 11 | +// Optional bridge products. Each is independent — drop either of these |
| 12 | +// `#if` blocks (or remove the matching Swift Package product dependency |
| 13 | +// from the Xcode project) and the demo still compiles. Code blocks fall |
| 14 | +// back to plain monospace; LaTeX falls back to its raw `$…$` source. |
| 15 | +#if canImport(MarkdownEngineCodeBlocks) |
| 16 | +import MarkdownEngineCodeBlocks |
| 17 | +#endif |
| 18 | +#if canImport(MarkdownEngineLatex) |
| 19 | +import MarkdownEngineLatex |
| 20 | +#endif |
| 21 | + |
11 | 22 | struct ContentView: View { |
12 | 23 | @State private var text: String = sampleMarkdown |
13 | 24 |
|
14 | 25 | var body: some View { |
15 | | - NativeTextViewWrapper(text: $text) |
| 26 | + NativeTextViewWrapper(text: $text, configuration: configuration) |
| 27 | + } |
| 28 | + |
| 29 | + /// The engine talks to your app through service protocols. Two of them — |
| 30 | + /// `SyntaxHighlighter` and `LatexRenderer` — render the code-block and |
| 31 | + /// LaTeX visuals. The base `MarkdownEngine` ships no-op defaults |
| 32 | + /// (plain monospace, raw `$…$`); the optional `MarkdownEngineCodeBlocks` |
| 33 | + /// and `MarkdownEngineLatex` products ship ready-made bridges backed by |
| 34 | + /// HighlighterSwift and SwiftMath respectively. |
| 35 | + /// |
| 36 | + /// This demo opportunistically plugs in whichever bridges are linked, |
| 37 | + /// so you can see exactly what each one adds. |
| 38 | + private var configuration: MarkdownEditorConfiguration { |
| 39 | + var config = MarkdownEditorConfiguration.default |
| 40 | + |
| 41 | + #if canImport(MarkdownEngineCodeBlocks) |
| 42 | + // Syntax highlighting for fenced code blocks. Auto-switches between |
| 43 | + // `atom-one-light` and `atom-one-dark` with system appearance. |
| 44 | + config.services.syntaxHighlighter = HighlighterSwiftBridge() |
| 45 | + #endif |
| 46 | + |
| 47 | + #if canImport(MarkdownEngineLatex) |
| 48 | + // LaTeX rendering for `$inline$` and `$$block$$` math. Uses the |
| 49 | + // Latin Modern math font and tints formulas to match the theme. |
| 50 | + config.services.latex = SwiftMathBridge() |
| 51 | + #endif |
| 52 | + |
| 53 | + return config |
16 | 54 | } |
17 | 55 | } |
18 | 56 |
|
19 | | -private let sampleMarkdown = """ |
| 57 | +/// Builds the demo markdown shown when the editor first loads. |
| 58 | +/// |
| 59 | +/// The text is composed from a fixed header/footer plus three feature |
| 60 | +/// sections — inline formatting, block math, and code — that swap between |
| 61 | +/// a full showcase and a short "feature unavailable" note depending on |
| 62 | +/// which optional bridge products are linked. |
| 63 | +/// |
| 64 | +/// When a bridge is missing, the fallback links to the README section |
| 65 | +/// that explains how to enable that feature in your own app. |
| 66 | +private var sampleMarkdown: String { |
| 67 | + [ |
| 68 | + markdownHeader, |
| 69 | + inlineFormattingSection, |
| 70 | + latexSection, |
| 71 | + codeSection, |
| 72 | + markdownFooter, |
| 73 | + ].joined(separator: "\n\n") |
| 74 | +} |
| 75 | + |
| 76 | +private let markdownHeader = """ |
20 | 77 | # MarkdownEngine |
21 | 78 |
|
22 | | -A native macOS Markdown editor built on **TextKit 2**, bridged to SwiftUI. |
| 79 | +A native macOS Markdown editor built on **TextKit 2**, bridged to SwiftUI — brought to you by [nodes-web.com](https://nodes-web.com). |
| 80 | +
|
| 81 | +Edit this text live. Formatting updates as you type. |
| 82 | +
|
| 83 | +--- |
| 84 | +""" |
| 85 | + |
| 86 | +/// Inline formatting demo. Drops the inline-LaTeX example sentence when |
| 87 | +/// the LaTeX bridge isn't linked, so the reader doesn't see raw `$…$`. |
| 88 | +private var inlineFormattingSection: String { |
| 89 | + #if canImport(MarkdownEngineLatex) |
| 90 | + return #""" |
| 91 | + ## Inline formatting |
| 92 | +
|
| 93 | + Mix **bold**, *italic*, and ***both at once***. Reach for `inline code` when a short snippet helps. Inline math fits naturally in prose — the Pythagorean identity says $a^2 + b^2 = c^2$, and Euler's identity famously claims $e^{i\pi} + 1 = 0$. |
| 94 | + """# |
| 95 | + #else |
| 96 | + return """ |
| 97 | + ## Inline formatting |
| 98 | +
|
| 99 | + Mix **bold**, *italic*, and ***both at once***. Reach for `inline code` when a short snippet helps. |
| 100 | + """ |
| 101 | + #endif |
| 102 | +} |
| 103 | + |
| 104 | +/// Block LaTeX demo when the `MarkdownEngineLatex` bridge is linked; |
| 105 | +/// otherwise a short note pointing to the README section that explains |
| 106 | +/// how to enable LaTeX rendering. |
| 107 | +private var latexSection: String { |
| 108 | + #if canImport(MarkdownEngineLatex) |
| 109 | + return #""" |
| 110 | + ## Block math |
| 111 | +
|
| 112 | + $$ |
| 113 | + \int_{-\infty}^{\infty} e^{-x^2}\,dx = \sqrt{\pi} |
| 114 | + $$ |
| 115 | +
|
| 116 | + $$ |
| 117 | + \frac{\partial}{\partial t}\Psi(\mathbf{r}, t) = -\frac{i}{\hbar}\hat{H}\,\Psi(\mathbf{r}, t) |
| 118 | + $$ |
| 119 | + """# |
| 120 | + #else |
| 121 | + return """ |
| 122 | + ## LaTeX |
| 123 | +
|
| 124 | + LaTeX (`$inline$` and `$$block$$`) is parsed but not rendered without the optional `MarkdownEngineLatex` product. See [LaTeX Rendering](https://github.com/nodes-app/swift-markdown-engine#latex-rendering) in the README to wire it up. |
| 125 | + """ |
| 126 | + #endif |
| 127 | +} |
| 128 | + |
| 129 | +/// Fenced code-block demo when the `MarkdownEngineCodeBlocks` bridge is |
| 130 | +/// linked; otherwise a plain monospace example and a link to the |
| 131 | +/// README's Code Blocks section. |
| 132 | +private var codeSection: String { |
| 133 | + #if canImport(MarkdownEngineCodeBlocks) |
| 134 | + return #""" |
| 135 | + ## Code |
| 136 | +
|
| 137 | + Swift, with syntax highlighting: |
| 138 | +
|
| 139 | + ```swift |
| 140 | + import SwiftUI |
| 141 | + import MarkdownEngine |
| 142 | +
|
| 143 | + struct Editor: View { |
| 144 | + @State private var text = "# Hello" |
| 145 | +
|
| 146 | + var body: some View { |
| 147 | + NativeTextViewWrapper(text: $text) |
| 148 | + .frame(minWidth: 640, minHeight: 480) |
| 149 | + } |
| 150 | + } |
| 151 | + ``` |
| 152 | +
|
| 153 | + And a little JSON: |
| 154 | +
|
| 155 | + ```json |
| 156 | + { |
| 157 | + "engine": "MarkdownEngine", |
| 158 | + "features": ["latex", "code", "wiki-links"], |
| 159 | + "version": 1.0 |
| 160 | + } |
| 161 | + ``` |
| 162 | + """# |
| 163 | + #else |
| 164 | + return #""" |
| 165 | + ## Code |
| 166 | +
|
| 167 | + Fenced code blocks render as plain monospace without the optional `MarkdownEngineCodeBlocks` product. See [Code Blocks](https://github.com/nodes-app/swift-markdown-engine#code-blocks) in the README for syntax-highlighted output: |
| 168 | +
|
| 169 | + ```swift |
| 170 | + let greeting = "Hello, world!" |
| 171 | + ``` |
| 172 | + """# |
| 173 | + #endif |
| 174 | +} |
23 | 175 |
|
24 | | -## Features |
25 | | -*Italic* |
26 | | -**Bold** |
| 176 | +private let markdownFooter = """ |
| 177 | +--- |
27 | 178 |
|
| 179 | +Built by [nodes-web.com](https://nodes-web.com). |
28 | 180 | """ |
0 commit comments