| ... | ... | @@ -3,9 +3,63 @@ import bytes from "./bindgen/wasm_bytes.js"; |
| 3 | 3 | |
| 4 | 4 | wasm.initSync({ module: bytes }); |
| 5 | 5 | |
| 6 | | type Transformed = Success | Failure; |
| 6 | /** The result of a Markodown transform */ |
| 7 | export type Transformed = Success | Failure; |
| 7 | 8 | |
| 9 | /** Convert Markodown (`.mdo`) source code into Marko source code (`.marko`) */ |
| 10 | export function transform(options: TransformOptions): Transformed { |
| 11 | const formats = options.format ?? ["marko"]; |
| 12 | let forceFormat = null; |
| 13 | if (formats.includes("html") && !formats.includes("marko")) { |
| 14 | forceFormat = wasm.OutputFormat.Html; |
| 15 | } else if (formats.includes("marko") && !formats.includes("html")) { |
| 16 | forceFormat = wasm.OutputFormat.Marko; |
| 17 | } else if (!formats.includes("html") && !formats.includes("marko")) { |
| 18 | throw new Error("No supported formats in " + JSON.stringify(formats)); |
| 19 | } |
| 20 | |
| 21 | return wasm.transform( |
| 22 | options.source, |
| 23 | forceFormat, |
| 24 | options?.layoutImport, |
| 25 | options?.componentImports, |
| 26 | options?.selfImport, |
| 27 | options?.markdownOnly, |
| 28 | options?.cloverExtensions, |
| 29 | ); |
| 30 | } |
| 31 | |
| 32 | /** Converts a flat document outline into a nested tree. */ |
| 33 | export function outlineToTree(outline: Heading[]): HeadingTree[] { |
| 34 | const root: HeadingTree[] = []; |
| 35 | const stack: HeadingTree[] = []; |
| 36 | |
| 37 | for (const heading of outline) { |
| 38 | if (heading.level === 1) continue; |
| 39 | |
| 40 | const node: HeadingTree = { ...heading, children: [] }; |
| 41 | |
| 42 | while ( |
| 43 | stack.length && stack[stack.length - 1].level >= heading.level |
| 44 | ) { |
| 45 | stack.pop(); |
| 46 | } |
| 47 | |
| 48 | if (stack.length) { |
| 49 | stack[stack.length - 1].children.push(node); |
| 50 | } else { |
| 51 | root.push(node); |
| 52 | } |
| 53 | |
| 54 | stack.push(node); |
| 55 | } |
| 56 | |
| 57 | return root; |
| 58 | } |
| 59 | |
| 60 | /** Options for {@linkcode transform}. */ |
| 8 | 61 | export interface TransformOptions { |
| 62 | /** The Markodown source code to be transformed */ |
| 9 | 63 | source: string; |
| 10 | 64 | /** |
| 11 | 65 | * Specify the allowed output formats. For simplicity, pass `marko`. By |
| ... | ... | @@ -29,7 +83,8 @@ export interface TransformOptions { |
| 29 | 83 | markdownOnly?: boolean; |
| 30 | 84 | /** |
| 31 | 85 | * These extensions are special-cased so that Clover can re-use this on |
| 32 | | * her website without |
| 86 | * her website without maintaining a second markdown parser. I promise |
| 87 | * we are not wasting your bundle size on my features. |
| 33 | 88 | */ |
| 34 | 89 | cloverExtensions?: CloverQuestionExtensions; |
| 35 | 90 | } |
| ... | ... | @@ -40,7 +95,7 @@ export interface ComponentImports { |
| 40 | 95 | heading?: string; |
| 41 | 96 | /** Replace `pre > code` with this import. */ |
| 42 | 97 | codeBlock?: string; |
| 43 | | /** Replace markdown links with this import. */ |
| 98 | /** Replace links with this import. */ |
| 44 | 99 | link?: string; |
| 45 | 100 | /** Replace images with this import. */ |
| 46 | 101 | image?: string; |
| ... | ... | @@ -56,6 +111,8 @@ export interface ComponentImports { |
| 56 | 111 | * emit custom HTML elements instead of imported components. |
| 57 | 112 | * |
| 58 | 113 | * Also includes `@html <raw>` block syntax for raw HTML passthrough. |
| 114 | * |
| 115 | * @internal |
| 59 | 116 | */ |
| 60 | 117 | export interface CloverQuestionExtensions { |
| 61 | 118 | /** |
| ... | ... | @@ -85,61 +142,89 @@ export interface CloverQuestionExtensions { |
| 85 | 142 | labelledRedaction: string; |
| 86 | 143 | } |
| 87 | 144 | |
| 145 | /** The transformer currently supports two output formats. */ |
| 88 | 146 | export type OutputFormat = "marko" | "html"; |
| 89 | 147 | |
| 148 | /** The transform is a success when `success: true` or there are no errors. */ |
| 90 | 149 | export interface Success { |
| 150 | /** Easy boolean to discriminate {@linkcode TransformResult} */ |
| 91 | 151 | success: true; |
| 152 | /** The transformed text. Format is determined by `format` */ |
| 92 | 153 | text: string; |
| 93 | | errors: []; |
| 154 | /** The resolved output format of `text` */ |
| 94 | 155 | format: OutputFormat; |
| 156 | /** List of errors, if any */ |
| 157 | errors: []; |
| 95 | 158 | } |
| 96 | 159 | |
| 160 | /** The transform is a success when `success: false` or there is at least one error. */ |
| 97 | 161 | export interface Failure { |
| 162 | /** Easy boolean to discriminate {@linkcode TransformResult} */ |
| 98 | 163 | success: false; |
| 164 | /** The transformed text. Format is determined by `format` */ |
| 99 | 165 | text: null; |
| 100 | | errors: TransformError[]; |
| 101 | | outline: Heading[] | null; |
| 166 | /** The resolved output format of `text` */ |
| 167 | format: null; |
| 168 | /** List of errors, if any */ |
| 169 | errors: [TransformError, ...TransformError[]]; |
| 102 | 170 | } |
| 103 | 171 | |
| 172 | /** This is passed to Markodown layouts */ |
| 173 | export interface LayoutInput { |
| 174 | /** |
| 175 | * Scanned from heading tags, the document outline is provided flat here. You |
| 176 | * can convert it into a nested tree with {@linkcode outlineToTree} |
| 177 | */ |
| 178 | outline: Heading[]; |
| 179 | /** |
| 180 | * A copy of the Module Namespace object of the page. Use this to reflect |
| 181 | * frontmatter or other customizable exports. Don't render `module.default` as |
| 182 | * a component, since that will recursively call this layout. |
| 183 | */ |
| 184 | module: Record<string, unknown>; |
| 185 | /** The rendered document */ |
| 186 | // @ts-ignore fails if marko types not chilling |
| 187 | content: Marko.Body; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Scanned from heading tags, this represents one heading in the document. You |
| 192 | * can convert it into a nested tree with {@linkcode outlineToTree} |
| 193 | */ |
| 104 | 194 | export interface Heading { |
| 195 | /** Which header element this corresponds to. */ |
| 105 | 196 | level: 1 | 2 | 3 | 4 | 5 | 6; |
| 197 | /** The link ID. Derived from the `id` attribute of the heading, or generated for you otherwise. */ |
| 106 | 198 | id: string; |
| 199 | /** The rendered heading name */ |
| 107 | 200 | // @ts-ignore fails if marko types not chilling |
| 108 | 201 | content: Marko.Body; |
| 109 | 202 | } |
| 110 | 203 | |
| 204 | /** Generated by {@linkcode outlineToTree} */ |
| 205 | export interface HeadingTree extends Heading { |
| 206 | /** Sub-headings */ |
| 207 | children: HeadingTree[]; |
| 208 | } |
| 209 | |
| 111 | 210 | export interface TransformError { |
| 211 | /** What went wrong? */ |
| 112 | 212 | message: string; |
| 113 | | labels: LabelledSpan[]; |
| 213 | /** Additional notes for the failure */ |
| 214 | notes: TransformNote[]; |
| 215 | /** One-based line */ |
| 114 | 216 | line: number; |
| 217 | /** One-based column, byte offset */ |
| 115 | 218 | column: number; |
| 116 | 219 | } |
| 117 | 220 | |
| 118 | | export interface LabelledSpan { |
| 119 | | message: string; |
| 221 | export interface TransformNote { |
| 222 | /** What this span is communicating */ |
| 223 | message?: string | null; |
| 224 | /** One-based line */ |
| 120 | 225 | line: number; |
| 226 | /** One-based column, byte offset */ |
| 121 | 227 | column: number; |
| 228 | /** Byte length */ |
| 122 | 229 | width: number; |
| 123 | 230 | } |
| 124 | | |
| 125 | | export function transform(options: TransformOptions): Transformed { |
| 126 | | const formats = options.format ?? ["marko"]; |
| 127 | | let forceFormat = null; |
| 128 | | if (formats.includes("html") && !formats.includes("marko")) { |
| 129 | | forceFormat = wasm.OutputFormat.Html; |
| 130 | | } else if (formats.includes("marko") && !formats.includes("html")) { |
| 131 | | forceFormat = wasm.OutputFormat.Marko; |
| 132 | | } else if (!formats.includes("html") && !formats.includes("marko")) { |
| 133 | | throw new Error("No supported formats in " + JSON.stringify(formats)); |
| 134 | | } |
| 135 | | |
| 136 | | return wasm.transform( |
| 137 | | options.source, |
| 138 | | forceFormat, |
| 139 | | options?.layoutImport, |
| 140 | | options?.componentImports, |
| 141 | | options?.selfImport, |
| 142 | | options?.markdownOnly, |
| 143 | | options?.cloverExtensions, |
| 144 | | ); |
| 145 | | } |