@humanonlyweb/writer 0.2.0 → 0.2.2
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/dist/components/WriterContent.d.ts +35 -0
- package/dist/components/WriterContent.js +85 -40
- package/dist/components/WriterEditor.js +86 -57
- package/dist/components/WriterEditor.vue.d.ts +3 -1
- package/dist/config.d.ts +21 -6
- package/dist/config.js +4 -1
- package/dist/docs/blocks.md +8 -3
- package/dist/docs/changelog.md +9 -0
- package/dist/docs/components/content.md +11 -8
- package/dist/docs/components/editor.md +15 -14
- package/dist/docs/images.md +3 -3
- package/dist/docs/parsing.md +8 -7
- package/dist/docs/security.md +21 -5
- package/dist/docs/storing.md +1 -1
- package/dist/editor/commands.js +26 -10
- package/dist/editor/images.d.ts +2 -0
- package/dist/editor/images.js +94 -46
- package/dist/editor/live.js +67 -34
- package/dist/editor/setup.js +151 -84
- package/dist/index.d.ts +5 -5
- package/dist/load-parser.js +20 -9
- package/dist/nuxt/module.d.ts +5 -2
- package/dist/nuxt/module.js +16 -1
- package/dist/package.js +1 -1
- package/dist/parse.d.ts +3 -3
- package/dist/parse.js +35 -17
- package/dist/parser-options.d.ts +17 -3
- package/dist/render.d.ts +7 -0
- package/dist/render.js +194 -42
- package/dist/sanitize.d.ts +3 -1
- package/dist/sanitize.js +214 -23
- package/dist/stats.js +28 -2
- package/package.json +13 -12
package/dist/editor/setup.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { activeCommands, commands, insertBlock, lineBreak, newParagraph } from "./commands.js";
|
|
2
2
|
import { uploadFiles } from "./images.js";
|
|
3
3
|
import { focused, livePreview } from "./live.js";
|
|
4
|
+
import * as Effect from "effect/Effect";
|
|
5
|
+
import * as Exit from "effect/Exit";
|
|
6
|
+
import * as Scope from "effect/Scope";
|
|
4
7
|
import { autocompletion, completionKeymap, snippet } from "@codemirror/autocomplete";
|
|
5
8
|
import { defaultKeymap, history, historyKeymap, redo, redoDepth, undo, undoDepth } from "@codemirror/commands";
|
|
6
9
|
import { markdownKeymap, markdownLanguage } from "@codemirror/lang-markdown";
|
|
@@ -50,6 +53,14 @@ const highlightStyle = HighlightStyle.define([
|
|
|
50
53
|
]);
|
|
51
54
|
const HEADING = /^(?:ATX|Setext)Heading([1-6])$/;
|
|
52
55
|
const codeLine = Decoration.line({ class: "cm-md-codeblock" });
|
|
56
|
+
const HEADING_LINES = {
|
|
57
|
+
"1": Decoration.line({ class: "cm-md-h1" }),
|
|
58
|
+
"2": Decoration.line({ class: "cm-md-h2" }),
|
|
59
|
+
"3": Decoration.line({ class: "cm-md-h3" }),
|
|
60
|
+
"4": Decoration.line({ class: "cm-md-h4" }),
|
|
61
|
+
"5": Decoration.line({ class: "cm-md-h5" }),
|
|
62
|
+
"6": Decoration.line({ class: "cm-md-h6" })
|
|
63
|
+
};
|
|
53
64
|
/**
|
|
54
65
|
* Line classes for headings and code blocks, so their marks (`#`, fences) take the same
|
|
55
66
|
* size and font as their text. Visible lines only.
|
|
@@ -64,20 +75,21 @@ const blockLines = ViewPlugin.fromClass(class {
|
|
|
64
75
|
}
|
|
65
76
|
build(view) {
|
|
66
77
|
const lines = [];
|
|
67
|
-
|
|
78
|
+
const { doc } = view.state;
|
|
79
|
+
const tree = syntaxTree(view.state);
|
|
80
|
+
for (const { from, to } of view.visibleRanges) tree.iterate({
|
|
68
81
|
from,
|
|
69
82
|
to,
|
|
70
83
|
enter(node) {
|
|
71
|
-
const { doc } = view.state;
|
|
72
84
|
if (node.name === "FencedCode" || node.name === "CodeBlock") {
|
|
73
85
|
const last = doc.lineAt(node.to).number;
|
|
74
86
|
for (let n = doc.lineAt(node.from).number; n <= last; n++) lines.push(codeLine.range(doc.line(n).from));
|
|
75
87
|
return false;
|
|
76
88
|
}
|
|
77
89
|
const level = HEADING.exec(node.name)?.[1];
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
lines.push(
|
|
90
|
+
const deco = level ? HEADING_LINES[level] : void 0;
|
|
91
|
+
if (!deco) return void 0;
|
|
92
|
+
lines.push(deco.range(doc.lineAt(node.from).from));
|
|
81
93
|
return false;
|
|
82
94
|
}
|
|
83
95
|
});
|
|
@@ -143,18 +155,28 @@ const snapshot = (state) => ({
|
|
|
143
155
|
undo: undoDepth(state) > 0,
|
|
144
156
|
redo: redoDepth(state) > 0
|
|
145
157
|
});
|
|
158
|
+
function sameSnapshot(a, b) {
|
|
159
|
+
if (!a || a.undo !== b.undo || a.redo !== b.redo || a.active.size !== b.active.size) return false;
|
|
160
|
+
for (const cmd of b.active) if (!a.active.has(cmd)) return false;
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
146
163
|
function createEditor(options) {
|
|
147
164
|
const readonly = new Compartment();
|
|
148
165
|
const live = new Compartment();
|
|
149
|
-
|
|
166
|
+
const placeholder$1 = new Compartment();
|
|
167
|
+
const attributes = new Compartment();
|
|
168
|
+
let currentPreview = options.preview;
|
|
169
|
+
let t = options.translations;
|
|
170
|
+
let currentUpload = options.upload;
|
|
171
|
+
let currentLinks = options.links;
|
|
172
|
+
let currentAccept = options.accept;
|
|
173
|
+
const editorScope = Scope.makeUnsafe("parallel");
|
|
150
174
|
/**
|
|
151
175
|
* In live preview, an empty image card to drop or choose a file into (or type a URL);
|
|
152
176
|
* in source mode, the Markdown to fill in.
|
|
153
177
|
*/
|
|
154
|
-
const imageCommand = (state) =>
|
|
155
|
-
const attributes = new Compartment();
|
|
178
|
+
const imageCommand = (state) => currentPreview ? insertBlock("![]()\n")(state) : insertBlock("", 2)(state);
|
|
156
179
|
const blockNames = new Set(options.blocks.map((block) => block.name));
|
|
157
|
-
const { translations: t } = options;
|
|
158
180
|
function run(command) {
|
|
159
181
|
if (view.state.readOnly) return false;
|
|
160
182
|
const spec = command(view.state);
|
|
@@ -163,91 +185,105 @@ function createEditor(options) {
|
|
|
163
185
|
return true;
|
|
164
186
|
}
|
|
165
187
|
let uploading = 0;
|
|
166
|
-
const uploadOptions =
|
|
167
|
-
upload:
|
|
188
|
+
const uploadOptions = () => currentUpload ? {
|
|
189
|
+
upload: currentUpload,
|
|
168
190
|
onError: options.onUploadError,
|
|
169
|
-
onProgress: (change) => options.onUploading(uploading += change)
|
|
191
|
+
onProgress: (change) => options.onUploading(uploading += change),
|
|
192
|
+
scope: editorScope
|
|
170
193
|
} : void 0;
|
|
171
194
|
/** Uploads files to the cursor (or `at`), each on its own line. */
|
|
172
195
|
function insertFiles(files, at = view.state.selection.main.head) {
|
|
173
|
-
|
|
174
|
-
|
|
196
|
+
const opts = uploadOptions();
|
|
197
|
+
if (!opts || view.state.readOnly || !files.length) return false;
|
|
198
|
+
uploadFiles(view, files, opts, { at });
|
|
175
199
|
return true;
|
|
176
200
|
}
|
|
177
201
|
const liveOptions = (render) => ({
|
|
178
202
|
render,
|
|
179
203
|
translations: t,
|
|
204
|
+
accept: currentAccept,
|
|
180
205
|
icons: options.imageIcons,
|
|
181
|
-
uploadInto:
|
|
206
|
+
uploadInto: currentUpload ? (target, file, range) => {
|
|
207
|
+
const opts = uploadOptions();
|
|
208
|
+
if (opts) uploadFiles(target, [file], opts, { replace: range });
|
|
209
|
+
} : void 0
|
|
182
210
|
});
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
211
|
+
function reconfigureLive() {
|
|
212
|
+
view.dispatch({ effects: live.reconfigure(currentPreview ? livePreview(liveOptions(currentPreview)) : []) });
|
|
213
|
+
}
|
|
214
|
+
function buildSlashItems() {
|
|
215
|
+
return [
|
|
216
|
+
...[
|
|
217
|
+
"heading1",
|
|
218
|
+
"heading2",
|
|
219
|
+
"heading3"
|
|
220
|
+
].map((name, i) => ({
|
|
221
|
+
name,
|
|
222
|
+
label: t.commands[name],
|
|
223
|
+
snippet: `${"#".repeat(i + 1)} \${}`
|
|
224
|
+
})),
|
|
225
|
+
{
|
|
226
|
+
name: "bulletList",
|
|
227
|
+
label: t.commands.bulletList,
|
|
228
|
+
snippet: "- ${}"
|
|
229
|
+
},
|
|
230
|
+
{
|
|
231
|
+
name: "orderedList",
|
|
232
|
+
label: t.commands.orderedList,
|
|
233
|
+
snippet: "1. ${}"
|
|
234
|
+
},
|
|
235
|
+
{
|
|
236
|
+
name: "taskList",
|
|
237
|
+
label: t.commands.taskList,
|
|
238
|
+
snippet: "- [ ] ${}"
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
name: "quote",
|
|
242
|
+
label: t.commands.quote,
|
|
243
|
+
snippet: "> ${}"
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
name: "codeBlock",
|
|
247
|
+
label: t.commands.codeBlock,
|
|
248
|
+
snippet: "```${language}\n${}\n```"
|
|
249
|
+
},
|
|
250
|
+
{
|
|
251
|
+
name: "table",
|
|
252
|
+
label: t.commands.table,
|
|
253
|
+
snippet: "| ${Column} | ${Column} |\n| --- | --- |\n| ${} | |"
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
name: "divider",
|
|
257
|
+
label: t.commands.divider,
|
|
258
|
+
snippet: "---\n${}"
|
|
259
|
+
},
|
|
260
|
+
{
|
|
261
|
+
name: "image",
|
|
262
|
+
label: t.commands.image,
|
|
263
|
+
snippet: ""
|
|
264
|
+
},
|
|
265
|
+
{
|
|
266
|
+
name: "footnote",
|
|
267
|
+
label: t.commands.footnote,
|
|
268
|
+
snippet: ""
|
|
269
|
+
},
|
|
270
|
+
...options.blocks
|
|
271
|
+
];
|
|
272
|
+
}
|
|
273
|
+
let slashItems = buildSlashItems();
|
|
240
274
|
/** `/` on an otherwise empty line lists blocks to insert. */
|
|
241
275
|
const slash = (context) => {
|
|
242
276
|
const line = context.state.doc.lineAt(context.pos);
|
|
243
277
|
const match = /^\s*\/([\w-]*)$/.exec(line.text.slice(0, context.pos - line.from));
|
|
244
278
|
if (!match || line.to !== context.pos) return null;
|
|
245
279
|
const query = match[1];
|
|
280
|
+
const lowerQuery = query.toLowerCase();
|
|
246
281
|
return {
|
|
247
282
|
from: context.pos - query.length,
|
|
248
|
-
|
|
283
|
+
filter: false,
|
|
284
|
+
options: (lowerQuery ? slashItems.filter((item) => item.label.toLowerCase().includes(lowerQuery) || item.name.toLowerCase().includes(lowerQuery) || item.keywords?.some((kw) => kw.toLowerCase().includes(lowerQuery))) : slashItems).map((item) => ({
|
|
249
285
|
label: item.label,
|
|
250
|
-
detail: blockNames.has(item.name) ? `::${item.name}` : void 0,
|
|
286
|
+
detail: item.description ?? (blockNames.has(item.name) ? `::${item.name}` : void 0),
|
|
251
287
|
type: "text",
|
|
252
288
|
apply: (view, completion, start, end) => {
|
|
253
289
|
const command = item.name === "footnote" ? commands.footnote : item.name === "image" ? imageCommand : void 0;
|
|
@@ -279,9 +315,9 @@ function createEditor(options) {
|
|
|
279
315
|
/** `](` followed by a query suggests the app's pages. */
|
|
280
316
|
const linkSource = async (context) => {
|
|
281
317
|
const match = context.matchBefore(/\]\([^)\s]*/);
|
|
282
|
-
if (!match || !
|
|
318
|
+
if (!match || !currentLinks) return null;
|
|
283
319
|
const query = match.text.slice(2);
|
|
284
|
-
const suggestions = await
|
|
320
|
+
const suggestions = await currentLinks(query);
|
|
285
321
|
if (context.aborted || !suggestions.length) return null;
|
|
286
322
|
return {
|
|
287
323
|
from: match.from + 2,
|
|
@@ -345,6 +381,13 @@ function createEditor(options) {
|
|
|
345
381
|
}
|
|
346
382
|
];
|
|
347
383
|
let doc = options.doc;
|
|
384
|
+
let lastSnapshot;
|
|
385
|
+
const emitState = (state) => {
|
|
386
|
+
const next = snapshot(state);
|
|
387
|
+
if (sameSnapshot(lastSnapshot, next)) return;
|
|
388
|
+
lastSnapshot = next;
|
|
389
|
+
options.onState(next);
|
|
390
|
+
};
|
|
348
391
|
const state = EditorState.create({
|
|
349
392
|
doc,
|
|
350
393
|
selection: options.selection ? EditorSelection.single(Math.min(options.selection.anchor, doc.length), Math.min(options.selection.head, doc.length)) : void 0,
|
|
@@ -368,8 +411,8 @@ function createEditor(options) {
|
|
|
368
411
|
}),
|
|
369
412
|
readonly.of([EditorState.readOnly.of(options.readonly)]),
|
|
370
413
|
focused,
|
|
371
|
-
live.of(
|
|
372
|
-
options.placeholder ? placeholder(options.placeholder) : [],
|
|
414
|
+
live.of(currentPreview ? livePreview(liveOptions(currentPreview)) : []),
|
|
415
|
+
placeholder$1.of(options.placeholder ? placeholder(options.placeholder) : []),
|
|
373
416
|
attributes.of(EditorView.contentAttributes.of(options.attributes)),
|
|
374
417
|
keymap.of([
|
|
375
418
|
...formatting,
|
|
@@ -406,7 +449,7 @@ function createEditor(options) {
|
|
|
406
449
|
},
|
|
407
450
|
drop(event, view) {
|
|
408
451
|
const files = [...event.dataTransfer?.files ?? []];
|
|
409
|
-
if (!files.length || !
|
|
452
|
+
if (!files.length || !currentUpload) return false;
|
|
410
453
|
const at = view.posAtCoords({
|
|
411
454
|
x: event.clientX,
|
|
412
455
|
y: event.clientY
|
|
@@ -422,7 +465,7 @@ function createEditor(options) {
|
|
|
422
465
|
doc = update.state.doc.toString();
|
|
423
466
|
options.onChange(doc);
|
|
424
467
|
}
|
|
425
|
-
if (update.docChanged || update.selectionSet || syntaxTree(update.state) !== syntaxTree(update.startState))
|
|
468
|
+
if (update.docChanged || update.selectionSet || syntaxTree(update.state) !== syntaxTree(update.startState)) emitState(update.state);
|
|
426
469
|
}),
|
|
427
470
|
theme,
|
|
428
471
|
options.extensions ?? []
|
|
@@ -432,7 +475,7 @@ function createEditor(options) {
|
|
|
432
475
|
state,
|
|
433
476
|
parent: options.parent
|
|
434
477
|
});
|
|
435
|
-
|
|
478
|
+
emitState(view.state);
|
|
436
479
|
return {
|
|
437
480
|
view,
|
|
438
481
|
run(name) {
|
|
@@ -471,8 +514,8 @@ function createEditor(options) {
|
|
|
471
514
|
/** Switches between live preview (with a renderer) and plain source. */
|
|
472
515
|
insertImage: () => run(imageCommand),
|
|
473
516
|
setPreview(render) {
|
|
474
|
-
|
|
475
|
-
|
|
517
|
+
currentPreview = render;
|
|
518
|
+
reconfigureLive();
|
|
476
519
|
},
|
|
477
520
|
setReadonly(value) {
|
|
478
521
|
view.dispatch({ effects: readonly.reconfigure(EditorState.readOnly.of(value)) });
|
|
@@ -480,7 +523,31 @@ function createEditor(options) {
|
|
|
480
523
|
setAttributes(value) {
|
|
481
524
|
view.dispatch({ effects: attributes.reconfigure(EditorView.contentAttributes.of(value)) });
|
|
482
525
|
},
|
|
483
|
-
|
|
526
|
+
setPlaceholder(value) {
|
|
527
|
+
view.dispatch({ effects: placeholder$1.reconfigure(value ? placeholder(value) : []) });
|
|
528
|
+
},
|
|
529
|
+
setTranslations(next) {
|
|
530
|
+
t = next;
|
|
531
|
+
slashItems = buildSlashItems();
|
|
532
|
+
if (currentPreview) reconfigureLive();
|
|
533
|
+
},
|
|
534
|
+
setUpload(next) {
|
|
535
|
+
const toggled = !!currentUpload !== !!next;
|
|
536
|
+
currentUpload = next;
|
|
537
|
+
if (toggled && currentPreview) reconfigureLive();
|
|
538
|
+
},
|
|
539
|
+
setAccept(next) {
|
|
540
|
+
if (currentAccept === next) return;
|
|
541
|
+
currentAccept = next;
|
|
542
|
+
if (currentPreview) reconfigureLive();
|
|
543
|
+
},
|
|
544
|
+
setLinks(next) {
|
|
545
|
+
currentLinks = next;
|
|
546
|
+
},
|
|
547
|
+
destroy() {
|
|
548
|
+
Effect.runFork(Scope.close(editorScope, Exit.void));
|
|
549
|
+
view.destroy();
|
|
550
|
+
}
|
|
484
551
|
};
|
|
485
552
|
}
|
|
486
553
|
export { createEditor };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
+
import { ParserOptions, SecurityOptions } from "./parser-options.js";
|
|
2
|
+
import { countWords, readingMinutes } from "./stats.js";
|
|
3
|
+
import { MarkdownDocument } from "./parse.js";
|
|
1
4
|
import _default from "./components/WriterContent.js";
|
|
2
5
|
import { CommandName, WriterTranslations, WriterTranslationsInput, defaultTranslations } from "./translations.js";
|
|
3
6
|
import _default$1 from "./components/WriterEditor.vue.js";
|
|
4
7
|
import { ToolbarEditor } from "./types.js";
|
|
5
8
|
import _default$2 from "./components/WriterToolbar.vue.js";
|
|
6
|
-
import {
|
|
7
|
-
import { countWords, readingMinutes } from "./stats.js";
|
|
8
|
-
import { MarkdownDocument } from "./parse.js";
|
|
9
|
-
import { WRITER_CONFIG_KEY, WriterBlock, WriterConfig, WriterOptions, createWriter, useTranslations, useWriterConfig } from "./config.js";
|
|
9
|
+
import { ResolvedWriterBlock, WRITER_CONFIG_KEY, WriterBlock, WriterConfig, WriterOptions, createWriter, useTranslations, useWriterConfig } from "./config.js";
|
|
10
10
|
import { RenderOptions, renderDocument } from "./render.js";
|
|
11
|
-
export { type CommandName, type MarkdownDocument, type ParserOptions, type RenderOptions, type ToolbarEditor, WRITER_CONFIG_KEY, type WriterBlock, type WriterConfig, _default as WriterContent, _default$1 as WriterEditor, type WriterOptions, _default$2 as WriterToolbar, type WriterTranslations, type WriterTranslationsInput, countWords, createWriter, defaultTranslations, readingMinutes, renderDocument, useTranslations, useWriterConfig };
|
|
11
|
+
export { type CommandName, type MarkdownDocument, type ParserOptions, type RenderOptions, type ResolvedWriterBlock, type SecurityOptions, type ToolbarEditor, WRITER_CONFIG_KEY, type WriterBlock, type WriterConfig, _default as WriterContent, _default$1 as WriterEditor, type WriterOptions, _default$2 as WriterToolbar, type WriterTranslations, type WriterTranslationsInput, countWords, createWriter, defaultTranslations, readingMinutes, renderDocument, useTranslations, useWriterConfig };
|
package/dist/load-parser.js
CHANGED
|
@@ -1,12 +1,23 @@
|
|
|
1
|
+
import * as Effect from "effect/Effect";
|
|
2
|
+
import * as Fiber from "effect/Fiber";
|
|
1
3
|
// src/load-parser.ts
|
|
2
4
|
const parsers = /* @__PURE__ */ new WeakMap();
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Loads and initializes the Markdown parser for `options`, deduplicating concurrent
|
|
7
|
+
* callers and evicting failed attempts so transient download failures retry.
|
|
8
|
+
*/
|
|
9
|
+
const loadParserEffect = Effect.fnUntraced(function* (options) {
|
|
10
|
+
let fiber = parsers.get(options);
|
|
11
|
+
if (!fiber) {
|
|
12
|
+
const task = Effect.tryPromise({
|
|
13
|
+
try: () => import("./parse.js").then(({ createParser }) => createParser(options)),
|
|
14
|
+
catch: (error) => error
|
|
15
|
+
}).pipe(Effect.tapError(() => Effect.sync(() => {
|
|
16
|
+
parsers.delete(options);
|
|
17
|
+
})));
|
|
18
|
+
fiber = Effect.runFork(task);
|
|
19
|
+
parsers.set(options, fiber);
|
|
9
20
|
}
|
|
10
|
-
return
|
|
11
|
-
}
|
|
12
|
-
export {
|
|
21
|
+
return yield* Fiber.join(fiber);
|
|
22
|
+
});
|
|
23
|
+
export { loadParserEffect };
|
package/dist/nuxt/module.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { WriterTranslationsInput } from "../translations.js";
|
|
2
1
|
import { ParserOptions } from "../parser-options.js";
|
|
2
|
+
import { WriterTranslationsInput } from "../translations.js";
|
|
3
3
|
/** Auto-registered components. A test keeps it in step with `src/index.ts`. */
|
|
4
4
|
export declare const COMPONENTS: readonly ["WriterContent", "WriterEditor", "WriterToolbar"];
|
|
5
5
|
export interface ModuleOptions {
|
|
@@ -18,11 +18,14 @@ export interface ModuleOptions {
|
|
|
18
18
|
/** Parser options for Markdown strings. Plugins aren't serializable: pre-parse on the server for those. */
|
|
19
19
|
parser: Omit<ParserOptions, "plugins">;
|
|
20
20
|
}
|
|
21
|
-
/** Labels and
|
|
21
|
+
/** Labels, snippets, and metadata for blocks, and the components' text, in `app.config.ts` under `writer`. */
|
|
22
22
|
export interface WriterAppConfig {
|
|
23
23
|
blocks?: Record<string, {
|
|
24
24
|
label?: string;
|
|
25
|
+
description?: string;
|
|
26
|
+
keywords?: readonly string[];
|
|
25
27
|
snippet?: string;
|
|
28
|
+
node?: boolean;
|
|
26
29
|
}>;
|
|
27
30
|
translations?: WriterTranslationsInput;
|
|
28
31
|
}
|
package/dist/nuxt/module.js
CHANGED
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
import { version } from "../package.js";
|
|
2
2
|
import { kebabCase } from "comark/utils";
|
|
3
3
|
import { createHash } from "node:crypto";
|
|
4
|
+
import { createRequire } from "node:module";
|
|
4
5
|
import { addComponent, addPlugin, addServerImports, addTemplate, addTypeTemplate, createResolver, defineNuxtModule, getLayerDirectories, resolveFiles, updateTemplates } from "@nuxt/kit";
|
|
5
6
|
// src/nuxt/module.ts
|
|
6
7
|
const PACKAGE = "@humanonlyweb/writer";
|
|
7
8
|
const TEMPLATE = "humanonlyweb-writer.mjs";
|
|
9
|
+
const require = createRequire(import.meta.url);
|
|
10
|
+
function installedVersion(name) {
|
|
11
|
+
try {
|
|
12
|
+
const meta = require(`${name}/package.json`);
|
|
13
|
+
return typeof meta === "object" && meta !== null && "version" in meta && typeof meta.version === "string" ? meta.version : "";
|
|
14
|
+
} catch {
|
|
15
|
+
return "";
|
|
16
|
+
}
|
|
17
|
+
}
|
|
8
18
|
/** Auto-registered components. A test keeps it in step with `src/index.ts`. */
|
|
9
19
|
const COMPONENTS = [
|
|
10
20
|
"WriterContent",
|
|
@@ -72,7 +82,12 @@ var module_default = defineNuxtModule({
|
|
|
72
82
|
addPlugin(resolver.resolve("./runtime/plugin"));
|
|
73
83
|
nuxt.hook("nitro:config", (config) => {
|
|
74
84
|
config.virtual ??= {};
|
|
75
|
-
const
|
|
85
|
+
const fingerprint = JSON.stringify({
|
|
86
|
+
parser: options.parser,
|
|
87
|
+
comark: installedVersion("comark"),
|
|
88
|
+
rangi: options.parser.highlight ? installedVersion("rangi") : ""
|
|
89
|
+
});
|
|
90
|
+
const hash = createHash("sha256").update(fingerprint).digest("hex");
|
|
76
91
|
const version$1 = `${version}+${hash.slice(0, 8)}`;
|
|
77
92
|
config.virtual["#humanonlyweb-writer/parser"] = [`export default ${JSON.stringify(options.parser)};`, `export const version = ${JSON.stringify(version$1)};`].join("\n");
|
|
78
93
|
});
|
package/dist/package.js
CHANGED
package/dist/parse.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { ParserOptions } from "./parser-options.js";
|
|
1
|
+
import { ParserOptions, SecurityOptions } from "./parser-options.js";
|
|
2
2
|
import { NATIVE_TAGS, isSafeUrl, sanitize } from "./sanitize.js";
|
|
3
3
|
import { countWords, readingMinutes } from "./stats.js";
|
|
4
|
-
import { MarkdownDocument, MarkdownDocument as MarkdownDocument$1 } from "comark";
|
|
5
4
|
import { Toc, TocLink } from "comark/plugins/toc";
|
|
5
|
+
import { MarkdownDocument, MarkdownDocument as MarkdownDocument$1 } from "comark";
|
|
6
6
|
import { RangiOptions } from "comark/plugins/rangi";
|
|
7
7
|
export type Parse = (markdown: string) => Promise<MarkdownDocument$1>;
|
|
8
8
|
/**
|
|
@@ -13,4 +13,4 @@ export type Parse = (markdown: string) => Promise<MarkdownDocument$1>;
|
|
|
13
13
|
export declare function createParser(options?: ParserOptions): Parse;
|
|
14
14
|
/** `createParser()` with the defaults. */
|
|
15
15
|
export declare const parse: Parse;
|
|
16
|
-
export { type MarkdownDocument, NATIVE_TAGS, type ParserOptions, type RangiOptions, type Toc, type TocLink, countWords, isSafeUrl, readingMinutes, sanitize };
|
|
16
|
+
export { type MarkdownDocument, NATIVE_TAGS, type ParserOptions, type RangiOptions, type SecurityOptions, type Toc, type TocLink, countWords, isSafeUrl, readingMinutes, sanitize };
|
package/dist/parse.js
CHANGED
|
@@ -1,58 +1,76 @@
|
|
|
1
1
|
import { NATIVE_TAGS, isSafeUrl, sanitize } from "./sanitize.js";
|
|
2
2
|
import { countWords, readingMinutes } from "./stats.js";
|
|
3
|
-
import {
|
|
3
|
+
import { TREE_WALK_MAX_DEPTH } from "comark/utils";
|
|
4
|
+
import { createMarkdownParser } from "comark/core";
|
|
4
5
|
import alert from "comark/plugins/alert";
|
|
5
6
|
import attributes from "comark/plugins/attributes";
|
|
6
7
|
import components from "comark/plugins/components";
|
|
7
8
|
import footnotes from "comark/plugins/footnotes";
|
|
8
9
|
import frontmatter from "comark/plugins/frontmatter";
|
|
10
|
+
import linkifyPlugin from "comark/plugins/linkify";
|
|
9
11
|
import taskList from "comark/plugins/task-list";
|
|
10
12
|
import toc from "comark/plugins/toc";
|
|
11
13
|
// src/parse.ts
|
|
12
|
-
/** Whether `node`
|
|
13
|
-
function
|
|
14
|
-
if (
|
|
15
|
-
|
|
14
|
+
/** Whether `node` holds a `<pre>` block or (when enabled) inline `<code>` declaring a language. */
|
|
15
|
+
function hasHighlightableCode(node, checkInline, depth = 0) {
|
|
16
|
+
if (typeof node === "string" || node[0] === null || depth >= TREE_WALK_MAX_DEPTH) return false;
|
|
17
|
+
const [tag, attrs, ...children] = node;
|
|
18
|
+
if (tag === "pre") return true;
|
|
19
|
+
if (checkInline && tag === "code") {
|
|
20
|
+
if (typeof attrs.lang === "string" || typeof attrs.language === "string") return true;
|
|
21
|
+
}
|
|
22
|
+
for (let i = 0; i < children.length; i++) if (hasHighlightableCode(children[i], checkInline, depth + 1)) return true;
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
let rangiModule;
|
|
26
|
+
function loadRangi() {
|
|
27
|
+
if (!rangiModule) rangiModule = import("comark/plugins/rangi").catch((error) => {
|
|
28
|
+
rangiModule = void 0;
|
|
29
|
+
throw error;
|
|
30
|
+
});
|
|
31
|
+
return rangiModule;
|
|
16
32
|
}
|
|
17
33
|
function highlighter(options) {
|
|
34
|
+
const checkInline = options.inlineCode !== false;
|
|
18
35
|
return {
|
|
19
36
|
name: "writer-highlight",
|
|
20
37
|
async post(state) {
|
|
21
|
-
if (!state.tree.nodes.some((node) =>
|
|
22
|
-
const { rangiCodeBlocks } = await
|
|
38
|
+
if (!state.tree.nodes.some((node) => hasHighlightableCode(node, checkInline))) return;
|
|
39
|
+
const { rangiCodeBlocks } = await loadRangi();
|
|
23
40
|
await rangiCodeBlocks(state.tree, options);
|
|
24
41
|
}
|
|
25
42
|
};
|
|
26
43
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
44
|
+
function sanitizer(security) {
|
|
45
|
+
return {
|
|
46
|
+
name: "writer-sanitize",
|
|
47
|
+
post(state) {
|
|
48
|
+
state.tree.nodes = sanitize(state.tree.nodes, void 0, security);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
33
52
|
/**
|
|
34
53
|
* A parser for stored Markdown. Raw HTML is text, never markup. Run it on the server
|
|
35
54
|
* when saving to store the document alongside its source, with the same options the
|
|
36
55
|
* app renders with.
|
|
37
56
|
*/
|
|
38
57
|
function createParser(options = {}) {
|
|
39
|
-
const { footnotes: notes = true, linkify = true, highlight = false, plugins = [] } = options;
|
|
58
|
+
const { footnotes: notes = true, linkify = true, highlight = false, plugins = [], security } = options;
|
|
40
59
|
const contents = options.toc ?? false;
|
|
41
60
|
const parser = createMarkdownParser({
|
|
42
|
-
registerDefaultPlugins: false,
|
|
43
61
|
autoUnwrap: false,
|
|
44
|
-
linkify,
|
|
45
62
|
plugins: [
|
|
46
63
|
frontmatter(),
|
|
47
64
|
components(),
|
|
48
65
|
attributes(),
|
|
49
66
|
taskList(),
|
|
50
67
|
alert(),
|
|
68
|
+
...linkify ? [linkifyPlugin({ fuzzyLink: true })] : [],
|
|
51
69
|
...contents ? [toc(contents === true ? {} : contents)] : [],
|
|
52
70
|
...notes ? [footnotes(notes === true ? {} : notes)] : [],
|
|
53
71
|
...highlight ? [highlighter(highlight === true ? {} : highlight)] : [],
|
|
54
72
|
...plugins,
|
|
55
|
-
sanitizer
|
|
73
|
+
sanitizer(security)
|
|
56
74
|
]
|
|
57
75
|
});
|
|
58
76
|
return (markdown) => parser(markdown);
|
package/dist/parser-options.d.ts
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
1
|
-
import { ComarkPlugin } from "comark";
|
|
2
1
|
import { FootnotesConfig } from "comark/plugins/footnotes";
|
|
3
2
|
import { Toc } from "comark/plugins/toc";
|
|
3
|
+
import { ComarkPlugin } from "comark";
|
|
4
4
|
import { RangiOptions } from "comark/plugins/rangi";
|
|
5
|
+
export interface SecurityOptions {
|
|
6
|
+
/** Strip `class` and `className` attributes (recommended for untrusted authors with utility CSS). */
|
|
7
|
+
stripClasses?: boolean;
|
|
8
|
+
/** Strip `id` attributes (prevents DOM clobbering on untrusted content). */
|
|
9
|
+
stripIds?: boolean;
|
|
10
|
+
/** Allow `data:image/…;base64,` URLs on `<img>` elements. Defaults to `true`. */
|
|
11
|
+
allowDataImages?: boolean;
|
|
12
|
+
/** Restrict absolute link URLs (`href`, `to`, …) to matching origins/prefixes. Relative URLs remain allowed. */
|
|
13
|
+
allowedLinkPrefixes?: readonly string[];
|
|
14
|
+
/** Restrict absolute image URLs (`src`, `srcset`, `poster`) to matching origins/prefixes. Relative URLs remain allowed. */
|
|
15
|
+
allowedImagePrefixes?: readonly string[];
|
|
16
|
+
}
|
|
5
17
|
export interface ParserOptions {
|
|
6
18
|
/** `[^1]` footnotes. Pass an object to change the section label or back link. */
|
|
7
19
|
footnotes?: boolean | FootnotesConfig;
|
|
@@ -10,10 +22,12 @@ export interface ParserOptions {
|
|
|
10
22
|
/** Turn URL-like text into links. */
|
|
11
23
|
linkify?: boolean;
|
|
12
24
|
/**
|
|
13
|
-
* Highlight fenced code with rangi (light, no WASM).
|
|
14
|
-
*
|
|
25
|
+
* Highlight fenced code (and inline code with `{lang="…"}`) with rangi (light, no WASM).
|
|
26
|
+
* Pass options for themes, line numbers or pre styles. Its code loads only when a document has code.
|
|
15
27
|
*/
|
|
16
28
|
highlight?: boolean | RangiOptions;
|
|
17
29
|
/** Extra Comark plugins, e.g. `comark/plugins/shiki`. They run before sanitizing. */
|
|
18
30
|
plugins?: ComarkPlugin[];
|
|
31
|
+
/** Extra sanitization rules for untrusted authors (e.g. stripping classes/ids or restricting URL prefixes). */
|
|
32
|
+
security?: SecurityOptions;
|
|
19
33
|
}
|
package/dist/render.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { SecurityOptions } from "./parser-options.js";
|
|
1
2
|
import { Component, VNodeArrayChildren } from "vue";
|
|
2
3
|
import { MarkdownDocument } from "comark";
|
|
3
4
|
export interface RenderOptions {
|
|
@@ -5,6 +6,12 @@ export interface RenderOptions {
|
|
|
5
6
|
blocks: Readonly<Record<string, Component>>;
|
|
6
7
|
/** Overrides for native elements. */
|
|
7
8
|
components: Readonly<Record<string, Component>>;
|
|
9
|
+
/** Blocks that opted in to receive the raw Comark AST node as `__node` (e.g. async Nuxt blocks). */
|
|
10
|
+
nodeBlocks?: ReadonlySet<string>;
|
|
11
|
+
/** Extra sanitization rules applied before rendering. */
|
|
12
|
+
security?: SecurityOptions;
|
|
13
|
+
/** Extra render context exposed as `data` for `:prop="data.key"` bindings (sanitized). */
|
|
14
|
+
data?: Record<string, unknown>;
|
|
8
15
|
}
|
|
9
16
|
/** Renders a parsed document as Vue nodes. Only native Markdown elements and `blocks` render. */
|
|
10
17
|
export declare function renderDocument(document: MarkdownDocument, options: RenderOptions): VNodeArrayChildren;
|