quietmark 0.0.0-stage → 0.1.0
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.
- package/LICENSE +21 -0
- package/README.md +276 -2
- package/dist/index.d.ts +76 -0
- package/dist/index.js +170 -0
- package/dist/index.js.map +1 -0
- package/dist/inline.d.ts +2 -0
- package/dist/inline.js +16 -0
- package/dist/inline.js.map +1 -0
- package/dist/types.d.ts +221 -0
- package/dist/types.js +4 -0
- package/dist/types.js.map +1 -0
- package/dist/wasm/inline.d.ts +1 -0
- package/dist/wasm/inline.js +2 -0
- package/dist/wasm/quietmark.d.ts +139 -0
- package/dist/wasm/quietmark.js +807 -0
- package/dist/wasm/quietmark_bg.wasm +0 -0
- package/dist/wasm/quietmark_bg.wasm.d.ts +30 -0
- package/package.json +51 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Max Boksem
|
|
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
CHANGED
|
@@ -1,3 +1,277 @@
|
|
|
1
|
-
#
|
|
1
|
+
# quietmark
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The Quietmark core, compiled to WebAssembly, with TypeScript types.
|
|
4
|
+
|
|
5
|
+
Quietmark is live-preview Markdown editing that never rewrites your text.
|
|
6
|
+
This package reads Markdown and says how it looks: which spans are bold or
|
|
7
|
+
code, which lines are headings, quotes or code, and which markers hide
|
|
8
|
+
while the selection is off them. It also says what Enter, Tab and Backspace
|
|
9
|
+
do in lists, tasks and quotes. It draws nothing itself.
|
|
10
|
+
|
|
11
|
+
For an editor on a web page, use
|
|
12
|
+
[`quietmark-prosemirror`](https://www.npmjs.com/package/quietmark-prosemirror),
|
|
13
|
+
which draws this package's look in ProseMirror or TipTap. Use this package
|
|
14
|
+
on its own to build a view of your own, or to test one.
|
|
15
|
+
|
|
16
|
+
## Installing
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
npm install quietmark
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Loading the module
|
|
23
|
+
|
|
24
|
+
The core is a WebAssembly module, `quietmark_bg.wasm`, which ships in the
|
|
25
|
+
package. Load it once before anything else. Until then, every function that
|
|
26
|
+
needs it throws `quietmark: call init() or initSync() before using it`.
|
|
27
|
+
|
|
28
|
+
`init()` with no argument fetches the module from beside the package's
|
|
29
|
+
script. That works when the page serves the package's files where they are.
|
|
30
|
+
A bundler that moves files needs the module's URL passed in. With Vite:
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { init } from "quietmark";
|
|
34
|
+
import wasmUrl from "quietmark/quietmark_bg.wasm?url";
|
|
35
|
+
|
|
36
|
+
await init(wasmUrl);
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
`init` takes a URL, a `Request`, a `Response`, bytes or a compiled
|
|
40
|
+
`WebAssembly.Module`, or a promise of any of them. A second call returns
|
|
41
|
+
the same promise. If a load fails, `init` can be called again.
|
|
42
|
+
|
|
43
|
+
`initSync` takes bytes or a compiled module and loads it at once. In Node:
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import { readFileSync } from "node:fs";
|
|
47
|
+
import { fileURLToPath } from "node:url";
|
|
48
|
+
import { initSync } from "quietmark";
|
|
49
|
+
|
|
50
|
+
initSync(readFileSync(fileURLToPath(import.meta.resolve("quietmark/quietmark_bg.wasm"))));
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
The package's types use `BufferSource`, `RequestInfo` and `WebAssembly`
|
|
54
|
+
from TypeScript's `dom` library. A Node project whose `tsconfig.json` sets
|
|
55
|
+
`lib` needs `"dom"` in it, or `skipLibCheck`.
|
|
56
|
+
|
|
57
|
+
A webview that loads its page from disk may not be able to fetch a file
|
|
58
|
+
beside the script. `quietmark/inline` holds the module inside a script, as
|
|
59
|
+
base64, which makes it a third larger than the file. Nothing loads it
|
|
60
|
+
unless you import it.
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import { initInline } from "quietmark/inline";
|
|
64
|
+
|
|
65
|
+
initInline();
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
`isReady()` says whether the module has loaded.
|
|
69
|
+
|
|
70
|
+
## Offsets
|
|
71
|
+
|
|
72
|
+
Every offset counts UTF-16 code units, as a JavaScript string does, so
|
|
73
|
+
`text.slice(range.from, range.to)` is the text a range covers. A `Range`
|
|
74
|
+
runs from `from` up to but not including `to`. Lines count from 0.
|
|
75
|
+
|
|
76
|
+
The core counts UTF-8 bytes and converts at the module's edge. A lone
|
|
77
|
+
surrogate can't be written in UTF-8, so a string holding one reaches the
|
|
78
|
+
core with U+FFFD in its place. Text typed into a page never holds one.
|
|
79
|
+
|
|
80
|
+
## Reading a text
|
|
81
|
+
|
|
82
|
+
`look(text, options?)` returns how the whole text looks, for a view that
|
|
83
|
+
keeps no document. Only which markers show depends on the selection, so a
|
|
84
|
+
view reads the look once per change and asks `shows` as the selection
|
|
85
|
+
moves.
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
import { look, shows } from "quietmark";
|
|
89
|
+
|
|
90
|
+
const text = "Buy **milk**";
|
|
91
|
+
const caret = { from: 0, to: 0 };
|
|
92
|
+
const hidden = look(text).markers.filter((marker) => !shows(marker, caret));
|
|
93
|
+
// The two `**`, at 4..6 and 10..12.
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
A `Look` has these fields:
|
|
97
|
+
|
|
98
|
+
| Field | What it holds |
|
|
99
|
+
| --- | --- |
|
|
100
|
+
| `spans` | Inline styles: `strong`, `emphasis`, `strike`, `code`, `link`, `done` for a ticked task's words, `comment` for an HTML comment, and `tag` for what a paired HTML tag such as `<kbd>` holds, with the tag's name in `tag`. Spans can nest. |
|
|
101
|
+
| `lines` | The lines that have a block style: a heading's level, how many quotes deep, a line of code, a rule, a ticked task, a callout's kind, a table. |
|
|
102
|
+
| `markers` | Syntax that hides while the selection is off its `reveal` range. `glyph` is what draws in its place, and `fold` is a line that folds away while it hides. A marker with no `reveal` always shows, as an ordered list's number does. |
|
|
103
|
+
| `fences` | The first and last line of each fence. |
|
|
104
|
+
| `code` | The code of each top-level fence that names a language, and the language, for syntax colours. |
|
|
105
|
+
| `tables` | Each table's rows, cells, column alignment and delimiter row. |
|
|
106
|
+
| `links` | Each link, with its address in `url`, or the id of the definition it uses in `ref`. |
|
|
107
|
+
| `defines` | The link and footnote definitions in the text. |
|
|
108
|
+
|
|
109
|
+
A glyph is one of:
|
|
110
|
+
|
|
111
|
+
| `kind` | Draws | Fields |
|
|
112
|
+
| --- | --- | --- |
|
|
113
|
+
| `bullet` | a list's `-`, `*` or `+` as a bullet | |
|
|
114
|
+
| `taskBox` | `[ ]` or `[x]` as a box | `checked` |
|
|
115
|
+
| `rule` | a thematic break as a line | |
|
|
116
|
+
| `cellBorder` | a table's `\|` as a border | |
|
|
117
|
+
| `lineBreak` | `<br>` as a line break | |
|
|
118
|
+
| `footnote` | a footnote call as its number | `id`, `number` |
|
|
119
|
+
| `formula` | `$x$` in a sentence | `source` |
|
|
120
|
+
| `block` | a block whole, on its own lines | `block`, a `DrawnBlock` |
|
|
121
|
+
|
|
122
|
+
A `DrawnBlock` has its `source`, its `first` and `last` lines, and `caret`,
|
|
123
|
+
where the caret goes to edit it. Its `kind` is `math` for a `$$` block,
|
|
124
|
+
`image` with `url` and `alt`, `callout` with `callout`, the kind in lower
|
|
125
|
+
case, `html` for an HTML block, `fence` with `info`, or `directive` with
|
|
126
|
+
`name` and `attributes`.
|
|
127
|
+
|
|
128
|
+
`shows(marker, selection)` says whether a marker shows, and
|
|
129
|
+
`touches(range, selection)` whether a selection touches a range. Both ends
|
|
130
|
+
count, so a caret just before or just past a span shows its markers.
|
|
131
|
+
|
|
132
|
+
`view(text, selection, options?)` returns the text as it reads, the way the
|
|
133
|
+
corpus writes it: hidden markers gone, glyphs drawn as characters, and the
|
|
134
|
+
selection drawn with `‸`, which is exported as `CARET`.
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
import { view } from "quietmark";
|
|
138
|
+
|
|
139
|
+
view("Buy **milk**", { from: 0, to: 0 }); // "‸Buy milk"
|
|
140
|
+
view("Buy **milk**", { from: 6, to: 6 }); // "Buy **‸milk**"
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Keys
|
|
144
|
+
|
|
145
|
+
`key(text, selection, pressed)` says what a key does where Markdown gives
|
|
146
|
+
it a meaning, such as Enter in a list. `pressed` is `"Enter"`, `"Tab"`,
|
|
147
|
+
`"ShiftTab"` or `"Backspace"`. Where the key means nothing in Markdown,
|
|
148
|
+
`key` returns `undefined` and the view does what it always does.
|
|
149
|
+
|
|
150
|
+
An `Edit` holds `changes` and `selection`. The changes don't overlap and
|
|
151
|
+
come in text order. Each one is a range in the text before the edit and the
|
|
152
|
+
string that replaces it. `selection` is where the selection goes in the
|
|
153
|
+
text after the edit. `applyEdit(text, edit)` applies one to a string.
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
import { applyEdit, key } from "quietmark";
|
|
157
|
+
|
|
158
|
+
const text = "- [ ] milk";
|
|
159
|
+
const edit = key(text, { from: 10, to: 10 }, "Enter");
|
|
160
|
+
if (edit) {
|
|
161
|
+
const after = applyEdit(text, edit); // "- [ ] milk\n- [ ] "
|
|
162
|
+
const caret = edit.selection; // { from: 17, to: 17 }
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
`toggleTask(text, at)` ticks or clears the box on the line holding `at`.
|
|
167
|
+
It returns `undefined` when that line holds no task.
|
|
168
|
+
|
|
169
|
+
An edit changes only the characters it names. Ticking a task changes the
|
|
170
|
+
one character inside its box.
|
|
171
|
+
|
|
172
|
+
## Document
|
|
173
|
+
|
|
174
|
+
An editor keeps a `Document`. It reads the text block by block, so a change
|
|
175
|
+
reads again only the blocks it touched, and it says which blocks those
|
|
176
|
+
were.
|
|
177
|
+
|
|
178
|
+
```ts
|
|
179
|
+
import { Document } from "quietmark";
|
|
180
|
+
|
|
181
|
+
const doc = new Document("# Groceries\n\n- [ ] milk\n");
|
|
182
|
+
const blocks = doc.blocks();
|
|
183
|
+
|
|
184
|
+
const changed = doc.replace({ from: 24, to: 24 }, "- [ ] eggs\n");
|
|
185
|
+
// Blocks from changed.at up to changed.at + changed.removed are gone.
|
|
186
|
+
// doc.blocks(changed.at, changed.at + changed.added) stand in their place.
|
|
187
|
+
blocks.splice(changed.at, changed.removed, ...doc.blocks(changed.at, changed.at + changed.added));
|
|
188
|
+
// The blocks in changed.reread kept their text but read differently.
|
|
189
|
+
for (const index of changed.reread) {
|
|
190
|
+
const block = doc.block(index);
|
|
191
|
+
if (block) blocks[index] = block;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
doc.free();
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
| Member | What it does |
|
|
198
|
+
| --- | --- |
|
|
199
|
+
| `new Document(text, options?)` | Reads `text`. The options default to the note preset. |
|
|
200
|
+
| `text` | The text. |
|
|
201
|
+
| `options` | The options, every field filled in. |
|
|
202
|
+
| `blockCount` | How many blocks there are. |
|
|
203
|
+
| `blocks(start?, end?)` | The blocks from `start` up to `end`, clamped, or all of them. Each call builds new objects, so fetch only the ones a change names. |
|
|
204
|
+
| `block(index)` | One block, or `undefined` past the last. |
|
|
205
|
+
| `look()` | The whole text's look, with offsets and lines from the text's start. |
|
|
206
|
+
| `setText(text)` | Replaces the text. Returns a `Changed`. |
|
|
207
|
+
| `setOptions(options)` | Changes what the look draws. Returns a `Changed`. |
|
|
208
|
+
| `replace(range, insert)` | Replaces `range` with `insert`. A range past the end changes nothing. Returns a `Changed`. |
|
|
209
|
+
| `apply(edit)` | Applies an edit, such as one `key` returned. Throws if its changes overlap or run backwards. Returns a `Changed`. |
|
|
210
|
+
| `key(selection, pressed)` | What a key does in the document's text. It returns the edit and doesn't apply it. |
|
|
211
|
+
| `toggleTask(at)` | The edit that ticks or clears a task. It doesn't apply it. |
|
|
212
|
+
| `free()` | Releases the document's memory in the module. |
|
|
213
|
+
|
|
214
|
+
A `BlockLook` has `range`, where the block is in the whole text, `line`,
|
|
215
|
+
its first line, and `look`, which counts from the block's own start:
|
|
216
|
+
offsets from `range.from` and lines from `line`.
|
|
217
|
+
|
|
218
|
+
A `Changed` names blocks by index. The blocks from `at` up to
|
|
219
|
+
`at + removed` before the change are gone, and the ones from `at` up to
|
|
220
|
+
`at + added` after it stand in their place. `reread` lists blocks that kept
|
|
221
|
+
their text but read differently, because a link definition or a footnote's
|
|
222
|
+
number moved. The blocks after the change read as they did, but a kept
|
|
223
|
+
copy's `range` and `line` are from before it, so they move by what the
|
|
224
|
+
change added or removed.
|
|
225
|
+
|
|
226
|
+
A document holds memory inside the module. `free()` releases it at once.
|
|
227
|
+
Without it, the garbage collector releases it some time after the document
|
|
228
|
+
is dropped.
|
|
229
|
+
|
|
230
|
+
## Options
|
|
231
|
+
|
|
232
|
+
| Field | What it draws while the selection is off it |
|
|
233
|
+
| --- | --- |
|
|
234
|
+
| `foldLines` | A fence's own lines, a heading's underline and a table's delimiter row fold away. |
|
|
235
|
+
| `bullets` | A list's `-`, `*` or `+` draws as a bullet. |
|
|
236
|
+
| `tasks` | `[ ]` and `[x]` draw as boxes. |
|
|
237
|
+
| `math` | `$x$` in a sentence and `$$` blocks draw as formulas. |
|
|
238
|
+
| `images` | A picture on a line of its own draws. |
|
|
239
|
+
| `html` | A paired tag such as `<kbd>` styles what it holds, `<br>` breaks the line, a block such as `<details>` draws whole, and a comment dims. |
|
|
240
|
+
| `blocks` | Fences and `:::` containers that match a rule draw whole. `{ fence: "mermaid" }` matches a fence whose info string starts with those words, and `{ directive: "cloze" }` matches a `:::cloze` container. |
|
|
241
|
+
|
|
242
|
+
`noteOptions()` turns everything on. `messageOptions()` is for a message
|
|
243
|
+
box, where every line stays where it is and markers only hide: folding,
|
|
244
|
+
bullets, tasks, images and HTML are off, and math stays on. Both register
|
|
245
|
+
`{ fence: "mermaid" }`. Both call into the module, so they need it loaded.
|
|
246
|
+
|
|
247
|
+
A function that takes options takes any part of them. A field left out
|
|
248
|
+
takes the note preset's value, not the value it had before, so
|
|
249
|
+
`{ math: false }` is a note without formulas. To start from the message
|
|
250
|
+
preset, spread it:
|
|
251
|
+
|
|
252
|
+
```ts
|
|
253
|
+
import { Document, messageOptions } from "quietmark";
|
|
254
|
+
|
|
255
|
+
const doc = new Document("Hello", { ...messageOptions(), math: false });
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
## Types
|
|
259
|
+
|
|
260
|
+
The package exports a type for everything it hands back: `Range`, `Look`,
|
|
261
|
+
`Span`, `SpanStyle`, `LineLook`, `CodeLine`, `Marker`, `Glyph`,
|
|
262
|
+
`DrawnBlock`, `Fence`, `FenceCode`, `Table`, `Row`, `Align`, `Link`,
|
|
263
|
+
`Definitions`, `LinkDefinition`, `NoteDefinition`, `BlockLook`, `Changed`,
|
|
264
|
+
`Change`, `Edit`, `Key`, `BlockRule`, `Options` and `InitInput`.
|
|
265
|
+
|
|
266
|
+
## Testing a view
|
|
267
|
+
|
|
268
|
+
The repository's
|
|
269
|
+
[`corpus/`](https://github.com/MaxMB15/Quietmark/tree/main/corpus) holds
|
|
270
|
+
Quietmark's behaviour as Markdown cases. `view` writes a text the way a
|
|
271
|
+
case's `view` step does, so a view's tests can run the same cases. The
|
|
272
|
+
[corpus README](https://github.com/MaxMB15/Quietmark/blob/main/corpus/README.md)
|
|
273
|
+
says how a case is written.
|
|
274
|
+
|
|
275
|
+
## License
|
|
276
|
+
|
|
277
|
+
MIT. See [LICENSE](https://github.com/MaxMB15/Quietmark/blob/main/LICENSE).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { BlockLook, Changed, Edit, Key, Look, Marker, Options, Range } from "./types.js";
|
|
2
|
+
export type * from "./types.js";
|
|
3
|
+
/** Where `init` loads the module from: a URL, a response, bytes or a compiled module. */
|
|
4
|
+
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
|
|
5
|
+
/**
|
|
6
|
+
* Loads the module. With no argument it fetches `quietmark_bg.wasm` beside
|
|
7
|
+
* this script, which suits a page served over HTTP; a bundler that moves
|
|
8
|
+
* assets wants the URL passed in, such as Vite's
|
|
9
|
+
* `import url from "quietmark/quietmark_bg.wasm?url"`. Calling it again
|
|
10
|
+
* returns the same promise.
|
|
11
|
+
*/
|
|
12
|
+
export declare function init(input?: InitInput | Promise<InitInput>): Promise<void>;
|
|
13
|
+
/** Loads the module from bytes or a compiled module, without waiting. */
|
|
14
|
+
export declare function initSync(module: BufferSource | WebAssembly.Module): void;
|
|
15
|
+
/** Whether the module has loaded. */
|
|
16
|
+
export declare function isReady(): boolean;
|
|
17
|
+
/** The caret in a `view` string. */
|
|
18
|
+
export declare const CARET = "\u2038";
|
|
19
|
+
/**
|
|
20
|
+
* Whether a selection touches a range. Both ends count, so a caret just
|
|
21
|
+
* before or just past a span is in it.
|
|
22
|
+
*/
|
|
23
|
+
export declare function touches(range: Range, selection: Range): boolean;
|
|
24
|
+
/** Whether a marker shows with the selection at `selection`. */
|
|
25
|
+
export declare function shows(marker: Marker, selection: Range): boolean;
|
|
26
|
+
/** The text after an edit, applied the way the core applies it. */
|
|
27
|
+
export declare function applyEdit(text: string, edit: Edit): string;
|
|
28
|
+
/** Everything on, as a note editor wants it. */
|
|
29
|
+
export declare function noteOptions(): Options;
|
|
30
|
+
/** A message box: every line stays put and markers only hide. */
|
|
31
|
+
export declare function messageOptions(): Options;
|
|
32
|
+
/** The whole look of `text`, for a view that keeps no document. */
|
|
33
|
+
export declare function look(text: string, options?: Partial<Options>): Look;
|
|
34
|
+
/** What `key` does in `text` at `selection`, or `undefined` for the default. */
|
|
35
|
+
export declare function key(text: string, selection: Range, pressed: Key): Edit | undefined;
|
|
36
|
+
/** Ticks or clears the task box on the line of `text` holding `at`. */
|
|
37
|
+
export declare function toggleTask(text: string, at: number): Edit | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* The text as it reads with the selection at `selection`: hidden markers
|
|
40
|
+
* gone, glyphs in their place, the selection drawn with `‸`. The corpus
|
|
41
|
+
* compares views, so a binding's tests can run it.
|
|
42
|
+
*/
|
|
43
|
+
export declare function view(text: string, selection: Range, options?: Partial<Options>): string;
|
|
44
|
+
/**
|
|
45
|
+
* A text being edited, read block by block. A view keeps one per editor,
|
|
46
|
+
* gives it every change, draws from `blocks`, and after a change draws again
|
|
47
|
+
* only the blocks `Changed` names.
|
|
48
|
+
*
|
|
49
|
+
* It holds memory inside the module; `free` releases it at once, and the
|
|
50
|
+
* garbage collector releases it eventually when it is dropped.
|
|
51
|
+
*/
|
|
52
|
+
export declare class Document {
|
|
53
|
+
#private;
|
|
54
|
+
constructor(text: string, options?: Partial<Options>);
|
|
55
|
+
get text(): string;
|
|
56
|
+
get options(): Options;
|
|
57
|
+
get blockCount(): number;
|
|
58
|
+
/** The blocks from `start` up to `end`, clamped, or all of them. */
|
|
59
|
+
blocks(start?: number, end?: number): BlockLook[];
|
|
60
|
+
/** One block, or `undefined` past the last. */
|
|
61
|
+
block(index: number): BlockLook | undefined;
|
|
62
|
+
/** The whole text's look, with offsets and lines from the text's start. */
|
|
63
|
+
look(): Look;
|
|
64
|
+
setText(text: string): Changed;
|
|
65
|
+
setOptions(options: Partial<Options>): Changed;
|
|
66
|
+
/** Replaces `range` with `insert`. A range past the end changes nothing. */
|
|
67
|
+
replace(range: Range, insert: string): Changed;
|
|
68
|
+
/** Applies an edit, such as one `key` or `toggleTask` returned. */
|
|
69
|
+
apply(edit: Edit): Changed;
|
|
70
|
+
/** What a key does at `selection`, or `undefined` for the default. */
|
|
71
|
+
key(selection: Range, pressed: Key): Edit | undefined;
|
|
72
|
+
/** Ticks or clears the task box on the line holding `at`. */
|
|
73
|
+
toggleTask(at: number): Edit | undefined;
|
|
74
|
+
/** Releases the module's memory for this document. */
|
|
75
|
+
free(): void;
|
|
76
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// Copyright (c) 2026 Max Boksem
|
|
3
|
+
// The core, compiled to WebAssembly, with types. Call `init` once before
|
|
4
|
+
// anything else; it fetches the module next to this script, or takes the
|
|
5
|
+
// module from you when the page loads it some other way.
|
|
6
|
+
import loadWasm, { Document as WasmDocument, initSync as loadWasmSync, key as wasmKey, look as wasmLook, messageOptions as wasmMessageOptions, noteOptions as wasmNoteOptions, toggleTask as wasmToggleTask, view as wasmView, } from "./wasm/quietmark.js";
|
|
7
|
+
let ready = false;
|
|
8
|
+
let loading;
|
|
9
|
+
/**
|
|
10
|
+
* Loads the module. With no argument it fetches `quietmark_bg.wasm` beside
|
|
11
|
+
* this script, which suits a page served over HTTP; a bundler that moves
|
|
12
|
+
* assets wants the URL passed in, such as Vite's
|
|
13
|
+
* `import url from "quietmark/quietmark_bg.wasm?url"`. Calling it again
|
|
14
|
+
* returns the same promise.
|
|
15
|
+
*/
|
|
16
|
+
export function init(input) {
|
|
17
|
+
if (ready)
|
|
18
|
+
return Promise.resolve();
|
|
19
|
+
loading ??= loadWasm(input === undefined ? undefined : { module_or_path: input }).then(() => {
|
|
20
|
+
ready = true;
|
|
21
|
+
}, (error) => {
|
|
22
|
+
// A failed load can be tried again, with other input.
|
|
23
|
+
loading = undefined;
|
|
24
|
+
throw error;
|
|
25
|
+
});
|
|
26
|
+
return loading;
|
|
27
|
+
}
|
|
28
|
+
/** Loads the module from bytes or a compiled module, without waiting. */
|
|
29
|
+
export function initSync(module) {
|
|
30
|
+
if (ready)
|
|
31
|
+
return;
|
|
32
|
+
loadWasmSync({ module });
|
|
33
|
+
ready = true;
|
|
34
|
+
}
|
|
35
|
+
/** Whether the module has loaded. */
|
|
36
|
+
export function isReady() {
|
|
37
|
+
return ready;
|
|
38
|
+
}
|
|
39
|
+
function needReady() {
|
|
40
|
+
if (!ready) {
|
|
41
|
+
throw new Error("quietmark: call init() or initSync() before using it");
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/** The caret in a `view` string. */
|
|
45
|
+
export const CARET = "‸";
|
|
46
|
+
/**
|
|
47
|
+
* Whether a selection touches a range. Both ends count, so a caret just
|
|
48
|
+
* before or just past a span is in it.
|
|
49
|
+
*/
|
|
50
|
+
export function touches(range, selection) {
|
|
51
|
+
return selection.from <= range.to && selection.to >= range.from;
|
|
52
|
+
}
|
|
53
|
+
/** Whether a marker shows with the selection at `selection`. */
|
|
54
|
+
export function shows(marker, selection) {
|
|
55
|
+
return marker.reveal === undefined || touches(marker.reveal, selection);
|
|
56
|
+
}
|
|
57
|
+
/** The text after an edit, applied the way the core applies it. */
|
|
58
|
+
export function applyEdit(text, edit) {
|
|
59
|
+
let out = "";
|
|
60
|
+
let at = 0;
|
|
61
|
+
for (const change of edit.changes) {
|
|
62
|
+
out += text.slice(at, change.range.from) + change.insert;
|
|
63
|
+
at = change.range.to;
|
|
64
|
+
}
|
|
65
|
+
return out + text.slice(at);
|
|
66
|
+
}
|
|
67
|
+
/** Everything on, as a note editor wants it. */
|
|
68
|
+
export function noteOptions() {
|
|
69
|
+
needReady();
|
|
70
|
+
return wasmNoteOptions();
|
|
71
|
+
}
|
|
72
|
+
/** A message box: every line stays put and markers only hide. */
|
|
73
|
+
export function messageOptions() {
|
|
74
|
+
needReady();
|
|
75
|
+
return wasmMessageOptions();
|
|
76
|
+
}
|
|
77
|
+
/** The whole look of `text`, for a view that keeps no document. */
|
|
78
|
+
export function look(text, options) {
|
|
79
|
+
needReady();
|
|
80
|
+
return wasmLook(text, options);
|
|
81
|
+
}
|
|
82
|
+
/** What `key` does in `text` at `selection`, or `undefined` for the default. */
|
|
83
|
+
export function key(text, selection, pressed) {
|
|
84
|
+
needReady();
|
|
85
|
+
return wasmKey(text, selection.from, selection.to, pressed);
|
|
86
|
+
}
|
|
87
|
+
/** Ticks or clears the task box on the line of `text` holding `at`. */
|
|
88
|
+
export function toggleTask(text, at) {
|
|
89
|
+
needReady();
|
|
90
|
+
return wasmToggleTask(text, at);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The text as it reads with the selection at `selection`: hidden markers
|
|
94
|
+
* gone, glyphs in their place, the selection drawn with `‸`. The corpus
|
|
95
|
+
* compares views, so a binding's tests can run it.
|
|
96
|
+
*/
|
|
97
|
+
export function view(text, selection, options) {
|
|
98
|
+
needReady();
|
|
99
|
+
return wasmView(text, options, selection.from, selection.to);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* A text being edited, read block by block. A view keeps one per editor,
|
|
103
|
+
* gives it every change, draws from `blocks`, and after a change draws again
|
|
104
|
+
* only the blocks `Changed` names.
|
|
105
|
+
*
|
|
106
|
+
* It holds memory inside the module; `free` releases it at once, and the
|
|
107
|
+
* garbage collector releases it eventually when it is dropped.
|
|
108
|
+
*/
|
|
109
|
+
export class Document {
|
|
110
|
+
#inner;
|
|
111
|
+
// The text crosses from the module only when asked for, and only once
|
|
112
|
+
// per change.
|
|
113
|
+
#text;
|
|
114
|
+
constructor(text, options) {
|
|
115
|
+
needReady();
|
|
116
|
+
this.#inner = new WasmDocument(text, options);
|
|
117
|
+
}
|
|
118
|
+
get text() {
|
|
119
|
+
this.#text ??= this.#inner.text();
|
|
120
|
+
return this.#text;
|
|
121
|
+
}
|
|
122
|
+
get options() {
|
|
123
|
+
return this.#inner.options();
|
|
124
|
+
}
|
|
125
|
+
get blockCount() {
|
|
126
|
+
return this.#inner.blockCount();
|
|
127
|
+
}
|
|
128
|
+
/** The blocks from `start` up to `end`, clamped, or all of them. */
|
|
129
|
+
blocks(start, end) {
|
|
130
|
+
return this.#inner.blocks(start, end);
|
|
131
|
+
}
|
|
132
|
+
/** One block, or `undefined` past the last. */
|
|
133
|
+
block(index) {
|
|
134
|
+
return this.blocks(index, index + 1)[0];
|
|
135
|
+
}
|
|
136
|
+
/** The whole text's look, with offsets and lines from the text's start. */
|
|
137
|
+
look() {
|
|
138
|
+
return this.#inner.look();
|
|
139
|
+
}
|
|
140
|
+
setText(text) {
|
|
141
|
+
this.#text = undefined;
|
|
142
|
+
return this.#inner.setText(text);
|
|
143
|
+
}
|
|
144
|
+
setOptions(options) {
|
|
145
|
+
return this.#inner.setOptions(options);
|
|
146
|
+
}
|
|
147
|
+
/** Replaces `range` with `insert`. A range past the end changes nothing. */
|
|
148
|
+
replace(range, insert) {
|
|
149
|
+
this.#text = undefined;
|
|
150
|
+
return this.#inner.replace(range.from, range.to, insert);
|
|
151
|
+
}
|
|
152
|
+
/** Applies an edit, such as one `key` or `toggleTask` returned. */
|
|
153
|
+
apply(edit) {
|
|
154
|
+
this.#text = undefined;
|
|
155
|
+
return this.#inner.apply(edit);
|
|
156
|
+
}
|
|
157
|
+
/** What a key does at `selection`, or `undefined` for the default. */
|
|
158
|
+
key(selection, pressed) {
|
|
159
|
+
return this.#inner.key(selection.from, selection.to, pressed);
|
|
160
|
+
}
|
|
161
|
+
/** Ticks or clears the task box on the line holding `at`. */
|
|
162
|
+
toggleTask(at) {
|
|
163
|
+
return this.#inner.toggleTask(at);
|
|
164
|
+
}
|
|
165
|
+
/** Releases the module's memory for this document. */
|
|
166
|
+
free() {
|
|
167
|
+
this.#inner.free();
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,+BAA+B;AAC/B,gCAAgC;AAEhC,yEAAyE;AACzE,yEAAyE;AACzE,yDAAyD;AAEzD,OAAO,QAAQ,EAAE,EACf,QAAQ,IAAI,YAAY,EACxB,QAAQ,IAAI,YAAY,EACxB,GAAG,IAAI,OAAO,EACd,IAAI,IAAI,QAAQ,EAChB,cAAc,IAAI,kBAAkB,EACpC,WAAW,IAAI,eAAe,EAC9B,UAAU,IAAI,cAAc,EAC5B,IAAI,IAAI,QAAQ,GACjB,MAAM,qBAAqB,CAAC;AAiB7B,IAAI,KAAK,GAAG,KAAK,CAAC;AAClB,IAAI,OAAkC,CAAC;AAEvC;;;;;;GAMG;AACH,MAAM,UAAU,IAAI,CAAC,KAAsC;IACzD,IAAI,KAAK;QAAE,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;IACpC,OAAO,KAAK,QAAQ,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,cAAc,EAAE,KAAK,EAAE,CAAC,CAAC,IAAI,CACpF,GAAG,EAAE;QACH,KAAK,GAAG,IAAI,CAAC;IACf,CAAC,EACD,CAAC,KAAc,EAAE,EAAE;QACjB,sDAAsD;QACtD,OAAO,GAAG,SAAS,CAAC;QACpB,MAAM,KAAK,CAAC;IACd,CAAC,CACF,CAAC;IACF,OAA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S,CAAC;QACvB,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAY,CAAC;IAC5C,CAAC;IAED,sEAAsE;IACtE,GAAG,CAAC,SAAgB,EAAE,OAAY;QAChC,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,EAAE,SAAS,CAAC,EAAE,EAAE,OAAO,CAAqB,CAAC;IACpF,CAAC;IAED,6DAA6D;IAC7D,UAAU,CAAC,EAAU;QACnB,OAAO,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,EAAE,CAAqB,CAAC;IACxD,CAAC;IAED,sDAAsD;IACtD,IAAI;QACF,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;IACrB,CAAC;CACF","sourcesContent":["// SPDX-License-Identifier: MIT\n// Copyright (c) 2026 Max Boksem\n\n// The core, compiled to WebAssembly, with types. Call `init` once before\n// anything else; it fetches the module next to this script, or takes the\n// module from you when the page loads it some other way.\n\nimport loadWasm, {\n Document as WasmDocument,\n initSync as loadWasmSync,\n key as wasmKey,\n look as wasmLook,\n messageOptions as wasmMessageOptions,\n noteOptions as wasmNoteOptions,\n toggleTask as wasmToggleTask,\n view as wasmView,\n} from \"./wasm/quietmark.js\";\nimport type {\n BlockLook,\n Changed,\n Edit,\n Key,\n Look,\n Marker,\n Options,\n Range,\n} from \"./types.js\";\n\nexport type * from \"./types.js\";\n\n/** Where `init` loads the module from: a URL, a response, bytes or a compiled module. */\nexport type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;\n\nlet ready = false;\nlet loading: Promise<void> | undefined;\n\n/**\n * Loads the module. With no argument it fetches `quietmark_bg.wasm` beside\n * this script, which suits a page served over HTTP; a bundler that moves\n * assets wants the URL passed in, such as Vite's\n * `import url from \"quietmark/quietmark_bg.wasm?url\"`. Calling it again\n * returns the same promise.\n */\nexport function init(input?: InitInput | Promise<InitInput>): Promise<void> {\n if (ready) return Promise.resolve();\n loading ??= loadWasm(input === undefined ? undefined : { module_or_path: input }).then(\n () => {\n ready = true;\n },\n (error: unknown) => {\n // A failed load can be tried again, with other input.\n loading = undefined;\n throw error;\n },\n );\n return loading;\n}\n\n/** Loads the module from bytes or a compiled module, without waiting. */\nexport function initSync(module: BufferSource | WebAssembly.Module): void {\n if (ready) return;\n loadWasmSync({ module });\n ready = true;\n}\n\n/** Whether the module has loaded. */\nexport function isReady(): boolean {\n return ready;\n}\n\nfunction needReady(): void {\n if (!ready) {\n throw new Error(\"quietmark: call init() or initSync() before using it\");\n }\n}\n\n/** The caret in a `view` string. */\nexport const CARET = \"‸\";\n\n/**\n * Whether a selection touches a range. Both ends count, so a caret just\n * before or just past a span is in it.\n */\nexport function touches(range: Range, selection: Range): boolean {\n return selection.from <= range.to && selection.to >= range.from;\n}\n\n/** Whether a marker shows with the selection at `selection`. */\nexport function shows(marker: Marker, selection: Range): boolean {\n return marker.reveal === undefined || touches(marker.reveal, selection);\n}\n\n/** The text after an edit, applied the way the core applies it. */\nexport function applyEdit(text: string, edit: Edit): string {\n let out = \"\";\n let at = 0;\n for (const change of edit.changes) {\n out += text.slice(at, change.range.from) + change.insert;\n at = change.range.to;\n }\n return out + text.slice(at);\n}\n\n/** Everything on, as a note editor wants it. */\nexport function noteOptions(): Options {\n needReady();\n return wasmNoteOptions() as Options;\n}\n\n/** A message box: every line stays put and markers only hide. */\nexport function messageOptions(): Options {\n needReady();\n return wasmMessageOptions() as Options;\n}\n\n/** The whole look of `text`, for a view that keeps no document. */\nexport function look(text: string, options?: Partial<Options>): Look {\n needReady();\n return wasmLook(text, options) as Look;\n}\n\n/** What `key` does in `text` at `selection`, or `undefined` for the default. */\nexport function key(text: string, selection: Range, pressed: Key): Edit | undefined {\n needReady();\n return wasmKey(text, selection.from, selection.to, pressed) as Edit | undefined;\n}\n\n/** Ticks or clears the task box on the line of `text` holding `at`. */\nexport function toggleTask(text: string, at: number): Edit | undefined {\n needReady();\n return wasmToggleTask(text, at) as Edit | undefined;\n}\n\n/**\n * The text as it reads with the selection at `selection`: hidden markers\n * gone, glyphs in their place, the selection drawn with `‸`. The corpus\n * compares views, so a binding's tests can run it.\n */\nexport function view(text: string, selection: Range, options?: Partial<Options>): string {\n needReady();\n return wasmView(text, options, selection.from, selection.to);\n}\n\n/**\n * A text being edited, read block by block. A view keeps one per editor,\n * gives it every change, draws from `blocks`, and after a change draws again\n * only the blocks `Changed` names.\n *\n * It holds memory inside the module; `free` releases it at once, and the\n * garbage collector releases it eventually when it is dropped.\n */\nexport class Document {\n #inner: WasmDocument;\n // The text crosses from the module only when asked for, and only once\n // per change.\n #text: string | undefined;\n\n constructor(text: string, options?: Partial<Options>) {\n needReady();\n this.#inner = new WasmDocument(text, options);\n }\n\n get text(): string {\n this.#text ??= this.#inner.text();\n return this.#text;\n }\n\n get options(): Options {\n return this.#inner.options() as Options;\n }\n\n get blockCount(): number {\n return this.#inner.blockCount();\n }\n\n /** The blocks from `start` up to `end`, clamped, or all of them. */\n blocks(start?: number, end?: number): BlockLook[] {\n return this.#inner.blocks(start, end) as BlockLook[];\n }\n\n /** One block, or `undefined` past the last. */\n block(index: number): BlockLook | undefined {\n return this.blocks(index, index + 1)[0];\n }\n\n /** The whole text's look, with offsets and lines from the text's start. */\n look(): Look {\n return this.#inner.look() as Look;\n }\n\n setText(text: string): Changed {\n this.#text = undefined;\n return this.#inner.setText(text) as Changed;\n }\n\n setOptions(options: Partial<Options>): Changed {\n return this.#inner.setOptions(options) as Changed;\n }\n\n /** Replaces `range` with `insert`. A range past the end changes nothing. */\n replace(range: Range, insert: string): Changed {\n this.#text = undefined;\n return this.#inner.replace(range.from, range.to, insert) as Changed;\n }\n\n /** Applies an edit, such as one `key` or `toggleTask` returned. */\n apply(edit: Edit): Changed {\n this.#text = undefined;\n return this.#inner.apply(edit) as Changed;\n }\n\n /** What a key does at `selection`, or `undefined` for the default. */\n key(selection: Range, pressed: Key): Edit | undefined {\n return this.#inner.key(selection.from, selection.to, pressed) as Edit | undefined;\n }\n\n /** Ticks or clears the task box on the line holding `at`. */\n toggleTask(at: number): Edit | undefined {\n return this.#inner.toggleTask(at) as Edit | undefined;\n }\n\n /** Releases the module's memory for this document. */\n free(): void {\n this.#inner.free();\n }\n}\n"]}
|
package/dist/inline.d.ts
ADDED
package/dist/inline.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// Copyright (c) 2026 Max Boksem
|
|
3
|
+
// The module inside the script as base64, for a webview that can't fetch a
|
|
4
|
+
// file beside the page, such as one loading the page from disk. It costs a
|
|
5
|
+
// third more bytes than the file, and nothing loads it unless imported.
|
|
6
|
+
import { initSync } from "./index.js";
|
|
7
|
+
import { wasmBase64 } from "./wasm/inline.js";
|
|
8
|
+
/** Loads the module from the copy inside this script. */
|
|
9
|
+
export function initInline() {
|
|
10
|
+
const text = atob(wasmBase64);
|
|
11
|
+
const bytes = new Uint8Array(text.length);
|
|
12
|
+
for (let at = 0; at < text.length; at++)
|
|
13
|
+
bytes[at] = text.charCodeAt(at);
|
|
14
|
+
initSync(bytes);
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=inline.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inline.js","sourceRoot":"","sources":["../src/inline.ts"],"names":[],"mappings":"AAAA,+BAA+B;AAC/B,gCAAgC;AAEhC,2EAA2E;AAC3E,2EAA2E;AAC3E,wEAAwE;AAExE,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,yDAAyD;AACzD,MAAM,UAAU,UAAU;IACxB,MAAM,IAAI,GAAG,IAAI,CAAC,UAAU,CAAC,CAAC;IAC9B,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAC1C,KAAK,IAAI,EAAE,GAAG,CAAC,EAAE,EAAE,GAAG,IAAI,CAAC,MAAM,EAAE,EAAE,EAAE;QAAE,KAAK,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;IACzE,QAAQ,CAAC,KAAK,CAAC,CAAC;AAClB,CAAC","sourcesContent":["// SPDX-License-Identifier: MIT\n// Copyright (c) 2026 Max Boksem\n\n// The module inside the script as base64, for a webview that can't fetch a\n// file beside the page, such as one loading the page from disk. It costs a\n// third more bytes than the file, and nothing loads it unless imported.\n\nimport { initSync } from \"./index.js\";\nimport { wasmBase64 } from \"./wasm/inline.js\";\n\n/** Loads the module from the copy inside this script. */\nexport function initInline(): void {\n const text = atob(wasmBase64);\n const bytes = new Uint8Array(text.length);\n for (let at = 0; at < text.length; at++) bytes[at] = text.charCodeAt(at);\n initSync(bytes);\n}\n"]}
|