inlaycms 0.2.0 → 0.4.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/README.md +334 -1
- package/assets/admin/admin.css +1 -1
- package/assets/admin/admin.js +1 -1
- package/assets/admin/editor.js +59 -44
- package/dist/adapters/admin/screens/redirects.js +12 -3
- package/dist/adapters/admin/views/layout.js +27 -7
- package/dist/adapters/cli/remote.js +9 -1
- package/dist/adapters/mcp/tools/redirects.js +16 -2
- package/dist/adapters/site/assets.d.ts +11 -0
- package/dist/adapters/site/assets.js +71 -0
- package/dist/adapters/site/cache.d.ts +2 -1
- package/dist/adapters/site/cache.js +6 -1
- package/dist/adapters/site/context.d.ts +6 -0
- package/dist/adapters/site/context.js +38 -13
- package/dist/adapters/site/head.d.ts +30 -7
- package/dist/adapters/site/head.js +145 -20
- package/dist/adapters/site/headers.d.ts +5 -0
- package/dist/adapters/site/headers.js +31 -0
- package/dist/adapters/site/plugin.d.ts +2 -0
- package/dist/adapters/site/plugin.js +32 -9
- package/dist/adapters/site/public.js +7 -24
- package/dist/adapters/site/render.d.ts +5 -0
- package/dist/adapters/site/render.js +23 -8
- package/dist/adapters/site/routes.d.ts +6 -0
- package/dist/adapters/site/routes.js +8 -3
- package/dist/adapters/site/seo.d.ts +20 -0
- package/dist/adapters/site/seo.js +45 -0
- package/dist/adapters/site/sitemap.d.ts +1 -1
- package/dist/adapters/site/sitemap.js +1 -1
- package/dist/config.js +3 -2
- package/dist/core/agent/format.js +1 -1
- package/dist/core/agent/schema.js +3 -0
- package/dist/core/boot.js +7 -0
- package/dist/core/check.js +6 -1
- package/dist/core/content/dry-run.d.ts +7 -6
- package/dist/core/content/dry-run.js +8 -8
- package/dist/core/content/publish.d.ts +1 -1
- package/dist/core/content/publish.js +5 -3
- package/dist/core/content/read.d.ts +3 -0
- package/dist/core/content/reader.js +3 -2
- package/dist/core/content/refs.js +1 -1
- package/dist/core/content/search.js +2 -2
- package/dist/core/content/shipped-images.d.ts +32 -0
- package/dist/core/content/shipped-images.js +62 -0
- package/dist/core/content/shipped-input.d.ts +9 -3
- package/dist/core/content/shipped-input.js +165 -28
- package/dist/core/content/shipped.js +8 -3
- package/dist/core/content/validate.js +7 -1
- package/dist/core/core.d.ts +5 -1
- package/dist/core/core.js +5 -4
- package/dist/core/db/connection.d.ts +6 -0
- package/dist/core/db/connection.js +13 -1
- package/dist/core/db/migrate.js +2 -0
- package/dist/core/db/migrations/0007_published_at.d.ts +3 -0
- package/dist/core/db/migrations/0007_published_at.js +16 -0
- package/dist/core/db/schema.d.ts +1 -0
- package/dist/core/export/export.js +3 -1
- package/dist/core/export/format.d.ts +8 -1
- package/dist/core/export/format.js +3 -2
- package/dist/core/export/read.js +7 -0
- package/dist/core/export/stage.d.ts +1 -1
- package/dist/core/export/stage.js +24 -1
- package/dist/core/fields/builtins.d.ts +28 -0
- package/dist/core/fields/builtins.js +103 -11
- package/dist/core/fields/define.d.ts +6 -0
- package/dist/core/fields/widgets.d.ts +7 -0
- package/dist/core/fields/widgets.js +77 -28
- package/dist/core/media/library.js +2 -2
- package/dist/core/messages/en.d.ts +72 -6
- package/dist/core/messages/en.js +72 -6
- package/dist/core/redirects/collapse.d.ts +17 -1
- package/dist/core/redirects/collapse.js +19 -3
- package/dist/core/redirects/redirects.d.ts +13 -12
- package/dist/core/redirects/redirects.js +141 -53
- package/dist/core/redirects/rule.d.ts +25 -0
- package/dist/core/redirects/rule.js +55 -0
- package/dist/core/registry/define.d.ts +40 -0
- package/dist/core/registry/define.js +3 -0
- package/dist/core/registry/registry.d.ts +21 -0
- package/dist/core/registry/registry.js +114 -6
- package/dist/core/registry/render.d.ts +37 -1
- package/dist/core/registry/route.d.ts +4 -0
- package/dist/core/release/bundle.js +30 -5
- package/dist/core/release/manifest.d.ts +14 -0
- package/dist/core/release/manifest.js +42 -17
- package/dist/core/release/markdown-file.d.ts +21 -0
- package/dist/core/release/markdown-file.js +172 -0
- package/dist/core/release/pack.js +2 -2
- package/dist/core/release/read.js +4 -1
- package/dist/core/release/site-dir.d.ts +4 -0
- package/dist/core/release/site-dir.js +8 -0
- package/dist/core/report.d.ts +8 -2
- package/dist/core/richtext/html.d.ts +3 -1
- package/dist/core/richtext/html.js +18 -4
- package/dist/core/richtext/inline.d.ts +22 -0
- package/dist/core/richtext/inline.js +33 -0
- package/dist/core/richtext/markdown.d.ts +19 -1
- package/dist/core/richtext/markdown.js +187 -22
- package/dist/core/richtext/schema.d.ts +22 -5
- package/dist/core/richtext/schema.js +81 -4
- package/dist/core/site/live.d.ts +1 -0
- package/dist/core/site/live.js +1 -0
- package/dist/core/site/paths.d.ts +6 -0
- package/dist/core/site/paths.js +13 -0
- package/dist/core/site/resolve.d.ts +1 -1
- package/dist/core/site/resolve.js +119 -30
- package/dist/core/site/sitemap.d.ts +1 -1
- package/dist/core/site/sitemap.js +4 -0
- package/dist/core/types.d.ts +28 -3
- package/dist/host/app.d.ts +5 -0
- package/dist/host/app.js +10 -1
- package/dist/host/dev.js +3 -7
- package/dist/host/diff.d.ts +19 -1
- package/dist/host/diff.js +32 -2
- package/dist/index.d.ts +2 -2
- package/dist/markdown-modules.d.ts +8 -0
- package/dist/markdown-modules.js +1 -0
- package/dist/site.d.ts +8 -3
- package/dist/site.js +6 -0
- package/dist/testing.d.ts +22 -0
- package/dist/testing.js +155 -0
- package/package.json +7 -1
|
@@ -16,7 +16,9 @@ const OPEN_BOLD = '\uE000';
|
|
|
16
16
|
const CLOSE_BOLD = '\uE001';
|
|
17
17
|
const OPEN_ITALIC = '\uE002';
|
|
18
18
|
const CLOSE_ITALIC = '\uE003';
|
|
19
|
-
|
|
19
|
+
/** Stands in for a code span while a block's bold and italic are placed, so they never touch its text. */
|
|
20
|
+
const CODE = '\uE004';
|
|
21
|
+
const STAND_INS = /[\uE000-\uE004]/g;
|
|
20
22
|
const STAND_IN = {
|
|
21
23
|
[OPEN_BOLD]: { count: 2, open: true },
|
|
22
24
|
[CLOSE_BOLD]: { count: 2, open: false },
|
|
@@ -70,12 +72,27 @@ function atLineStart(out) {
|
|
|
70
72
|
}
|
|
71
73
|
/** Characters as numeric character references, which read back as themselves. */
|
|
72
74
|
const encodeChars = (s) => Array.from(s, (c) => `&#${c.codePointAt(0)};`).join('');
|
|
73
|
-
/**
|
|
74
|
-
|
|
75
|
+
/**
|
|
76
|
+
* URL as a link destination: `<…>` when it has spaces or brackets, escaped so markdown-it gives it back unchanged.
|
|
77
|
+
* Stand-in characters are written as character references, so they can't be taken for formatting.
|
|
78
|
+
*/
|
|
79
|
+
export function markdownDestination(url) {
|
|
75
80
|
const entity = /&(?=#?[0-9A-Za-z]+;)/g;
|
|
76
81
|
if (/[\s()<>]/.test(url))
|
|
77
|
-
return '<' + url.replace(/[\\<>]/g, '\\$&').replace(entity, '\\&') + '>';
|
|
78
|
-
return url.replace(/\\/g, '\\\\').replace(entity, '\\&');
|
|
82
|
+
return '<' + url.replace(/[\\<>]/g, '\\$&').replace(entity, '\\&').replace(STAND_INS, encodeChars) + '>';
|
|
83
|
+
return url.replace(/\\/g, '\\\\').replace(entity, '\\&').replace(STAND_INS, encodeChars);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Text as a CommonMark code span: fenced by one more backtick than its longest run of them, and padded with a space
|
|
87
|
+
* on each side where reading it back would otherwise take a backtick for the fence or trim a space.
|
|
88
|
+
*/
|
|
89
|
+
function codeSpan(text) {
|
|
90
|
+
let longest = 0;
|
|
91
|
+
for (const run of text.match(/`+/g) ?? [])
|
|
92
|
+
longest = Math.max(longest, run.length);
|
|
93
|
+
const fence = '`'.repeat(longest + 1);
|
|
94
|
+
const pad = text.startsWith('`') || text.endsWith('`') || (text.startsWith(' ') && text.endsWith(' ') && /[^ ]/.test(text));
|
|
95
|
+
return pad ? `${fence} ${text} ${fence}` : fence + text + fence;
|
|
79
96
|
}
|
|
80
97
|
/** Text with each line break, and the whitespace around it, turned into one space. */
|
|
81
98
|
function oneLine(s) {
|
|
@@ -183,6 +200,8 @@ class State extends StateBase {
|
|
|
183
200
|
lastContent = new WeakMap();
|
|
184
201
|
/** Finished lines, moved out of `out` so that rewriting a block's text stays cheap. */
|
|
185
202
|
done = [];
|
|
203
|
+
/** The code spans written as `CODE` in the block being written, in order. */
|
|
204
|
+
codes = [];
|
|
186
205
|
/** Moves every finished line out of `out`; what is still being written stays. */
|
|
187
206
|
commitLines() {
|
|
188
207
|
const end = this.out.lastIndexOf('\n') + 1;
|
|
@@ -208,7 +227,9 @@ class State extends StateBase {
|
|
|
208
227
|
out = out
|
|
209
228
|
.replace(/^(\d{1,9})([.)])(?=[ \t]|$)/, '$1\\$2')
|
|
210
229
|
.replace(/^\+(?=[ \t]|$)/, '\\+')
|
|
211
|
-
.replace(/^=/, '\\=')
|
|
230
|
+
.replace(/^=/, '\\=')
|
|
231
|
+
// After a line break, a line starting with these could be read as a table's delimiter row.
|
|
232
|
+
.replace(/^[:|]/, '\\$&');
|
|
212
233
|
}
|
|
213
234
|
// A bare `_` is safe only between two letters or digits; next to anything else it could open or close.
|
|
214
235
|
out = out.replace(/\\[\s\S]|_/g, (m, at, all) => m === '_' && !(/[A-Za-z0-9]/.test(all[at - 1] ?? '') && /[A-Za-z0-9]/.test(all[at + 1] ?? '')) ? '\\_' : m);
|
|
@@ -298,7 +319,8 @@ function tidyInvisible(node) {
|
|
|
298
319
|
function charKind(piece) {
|
|
299
320
|
if (piece === undefined)
|
|
300
321
|
return 'space';
|
|
301
|
-
|
|
322
|
+
// A code span starts and ends with a backtick.
|
|
323
|
+
if ('delim' in piece || piece.text === CODE)
|
|
302
324
|
return 'punct';
|
|
303
325
|
const { isWhiteSpace, isMdAsciiPunct, isPunctCharCode } = tokenizer.utils;
|
|
304
326
|
const code = piece.text.codePointAt(0) ?? 0x20;
|
|
@@ -502,7 +524,9 @@ function renderTextblock(state, node, fromBlockStart) {
|
|
|
502
524
|
state.commitLines();
|
|
503
525
|
const start = state.out.length;
|
|
504
526
|
state.renderInline(writing.emphasis === 'none' ? withoutEmphasis(writing.node) : tidyInvisible(writing.node), fromBlockStart);
|
|
505
|
-
const
|
|
527
|
+
const codes = state.codes;
|
|
528
|
+
state.codes = [];
|
|
529
|
+
const written = placeDelimiters(state.out.slice(start), writing.emphasis === 'strict').replace(/\uE004/g, () => codes.shift() ?? '');
|
|
506
530
|
const kept = written.trimEnd();
|
|
507
531
|
state.out = state.out.slice(0, start) + kept + encodeChars(written.slice(kept.length));
|
|
508
532
|
}
|
|
@@ -531,7 +555,26 @@ function separateList(state, node) {
|
|
|
531
555
|
if ((empty || start !== 1) && state.closed && state.closed.type !== node.type)
|
|
532
556
|
state.flushClose(2);
|
|
533
557
|
}
|
|
558
|
+
/** A table cell's content on one line, written as any paragraph is, with each `|` escaped. */
|
|
559
|
+
function cellMarkdown(cell) {
|
|
560
|
+
const state = new State(nodes, marks, SERIALIZER_OPTIONS);
|
|
561
|
+
state.renderContent(richTextSchema.nodes.doc.create(null, cell.content));
|
|
562
|
+
return oneLine(state.result()).trim().replace(/\|/g, '\\|');
|
|
563
|
+
}
|
|
534
564
|
const nodes = {
|
|
565
|
+
table(state, node) {
|
|
566
|
+
node.forEach((row, _, r) => {
|
|
567
|
+
const cells = [];
|
|
568
|
+
row.forEach((cell) => cells.push(cellMarkdown(cell)));
|
|
569
|
+
state.write(`| ${cells.join(' | ')} |`);
|
|
570
|
+
state.ensureNewLine();
|
|
571
|
+
if (r === 0) {
|
|
572
|
+
state.write(`| ${cells.map(() => '---').join(' | ')} |`);
|
|
573
|
+
state.ensureNewLine();
|
|
574
|
+
}
|
|
575
|
+
});
|
|
576
|
+
state.closeBlock(node);
|
|
577
|
+
},
|
|
535
578
|
blockquote(state, node) {
|
|
536
579
|
state.wrapBlock('> ', null, node, () => state.renderContent(node));
|
|
537
580
|
},
|
|
@@ -615,6 +658,12 @@ const nodes = {
|
|
|
615
658
|
state.write('\\\n');
|
|
616
659
|
},
|
|
617
660
|
text(state, node, parent, index) {
|
|
661
|
+
if (node.marks.some((m) => m.type.name === 'code')) {
|
|
662
|
+
state.codes.push(codeSpan(node.text ?? ''));
|
|
663
|
+
state.write();
|
|
664
|
+
state.append(CODE);
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
618
667
|
// A `!` right before a link would make it an image. prosemirror-markdown checks for
|
|
619
668
|
// this, but rewrites the whole output each time and misreads `\\!`, so it is escaped here.
|
|
620
669
|
const text = node.text ?? '';
|
|
@@ -635,12 +684,14 @@ const marks = {
|
|
|
635
684
|
open: '[',
|
|
636
685
|
close: (_state, mark) => {
|
|
637
686
|
const href = typeof mark.attrs.document === 'string' ? DOCUMENT_PREFIX + mark.attrs.document : String(mark.attrs.href);
|
|
638
|
-
return `](${
|
|
687
|
+
return `](${markdownDestination(href)})`;
|
|
639
688
|
},
|
|
640
689
|
mixable: true,
|
|
641
690
|
},
|
|
642
691
|
bold: { open: OPEN_BOLD, close: CLOSE_BOLD, mixable: true, expelEnclosingWhitespace: true },
|
|
643
692
|
italic: { open: OPEN_ITALIC, close: CLOSE_ITALIC, mixable: true, expelEnclosingWhitespace: true },
|
|
693
|
+
// Written by the text serializer, which sees the whole text the mark is on.
|
|
694
|
+
code: { open: '', close: '' },
|
|
644
695
|
};
|
|
645
696
|
/** The doc as CommonMark: tight lists, `- ` bullets, `inlay:` URIs for internal links and images. */
|
|
646
697
|
export function richTextToMarkdown(doc) {
|
|
@@ -655,9 +706,11 @@ const tokenizer = new MarkdownIt('commonmark', { html: false, linkify: false, ty
|
|
|
655
706
|
tokenizer.validateLink = () => true;
|
|
656
707
|
tokenizer.normalizeLink = (url) => url;
|
|
657
708
|
tokenizer.normalizeLinkText = (url) => url;
|
|
709
|
+
tokenizer.enable('table');
|
|
658
710
|
/**
|
|
659
711
|
* Reads the syntax that CommonMark keeps as plain text but writers mean as formatting rich text can't hold:
|
|
660
|
-
* HTML
|
|
712
|
+
* HTML and strikethrough. Used only to refuse it, so it isn't silently stored as literal characters.
|
|
713
|
+
* Tables are read here too, so syntax inside a cell is found as it is anywhere else.
|
|
661
714
|
*/
|
|
662
715
|
const detector = new MarkdownIt('commonmark', { html: true, linkify: false, typographer: false }).enable([
|
|
663
716
|
'table',
|
|
@@ -710,6 +763,43 @@ tokenizer.core.ruler.after('block', 'inlay_used_references', (state) => {
|
|
|
710
763
|
if (env.definitions)
|
|
711
764
|
env.inline = true;
|
|
712
765
|
});
|
|
766
|
+
// Table cells hold a paragraph, as the schema requires: each cell's inline content gets one.
|
|
767
|
+
tokenizer.core.ruler.after('inline', 'inlay_table_cells', (state) => {
|
|
768
|
+
const out = [];
|
|
769
|
+
let cell = false;
|
|
770
|
+
for (const tok of state.tokens) {
|
|
771
|
+
if (cell && tok.type === 'inline') {
|
|
772
|
+
const open = new state.Token('paragraph_open', 'p', 1);
|
|
773
|
+
const close = new state.Token('paragraph_close', 'p', -1);
|
|
774
|
+
open.level = close.level = tok.level;
|
|
775
|
+
tok.level++;
|
|
776
|
+
out.push(open, tok, close);
|
|
777
|
+
}
|
|
778
|
+
else {
|
|
779
|
+
out.push(tok);
|
|
780
|
+
}
|
|
781
|
+
cell = tok.type === 'th_open' || tok.type === 'td_open';
|
|
782
|
+
}
|
|
783
|
+
state.tokens = out;
|
|
784
|
+
});
|
|
785
|
+
/** Image tokens, at any depth, whose target is named in `env.images` point at that library image instead. */
|
|
786
|
+
tokenizer.core.ruler.push('inlay_images', (state) => {
|
|
787
|
+
const images = state.env.images;
|
|
788
|
+
if (!images || images.size === 0)
|
|
789
|
+
return;
|
|
790
|
+
const rewrite = (tokens) => {
|
|
791
|
+
for (const tok of tokens) {
|
|
792
|
+
if (tok.type === 'image') {
|
|
793
|
+
const id = images.get(tok.attrGet('src') ?? '');
|
|
794
|
+
if (id !== undefined)
|
|
795
|
+
tok.attrSet('src', MEDIA_PREFIX + id);
|
|
796
|
+
}
|
|
797
|
+
if (tok.children)
|
|
798
|
+
rewrite(tok.children);
|
|
799
|
+
}
|
|
800
|
+
};
|
|
801
|
+
rewrite(state.tokens);
|
|
802
|
+
});
|
|
713
803
|
/** An image's alt as plain text, the way a browser shows it. */
|
|
714
804
|
function altText(children) {
|
|
715
805
|
return (children ?? [])
|
|
@@ -739,8 +829,15 @@ const parser = new MarkdownParser(richTextSchema, tokenizer, {
|
|
|
739
829
|
}),
|
|
740
830
|
},
|
|
741
831
|
hardbreak: { node: 'hardBreak' },
|
|
832
|
+
table: { block: 'table' },
|
|
833
|
+
thead: { ignore: true },
|
|
834
|
+
tbody: { ignore: true },
|
|
835
|
+
tr: { block: 'tableRow' },
|
|
836
|
+
th: { block: 'tableHeader' },
|
|
837
|
+
td: { block: 'tableCell' },
|
|
742
838
|
em: { mark: 'italic' },
|
|
743
839
|
strong: { mark: 'bold' },
|
|
840
|
+
code_inline: { mark: 'code', noCloseToken: true },
|
|
744
841
|
link: {
|
|
745
842
|
mark: 'link',
|
|
746
843
|
getAttrs: (tok) => {
|
|
@@ -774,12 +871,13 @@ const TOKEN_FEATURE = {
|
|
|
774
871
|
blockquote_open: 'blockquote',
|
|
775
872
|
strong_open: 'bold',
|
|
776
873
|
em_open: 'italic',
|
|
874
|
+
code_inline: 'code',
|
|
777
875
|
link_open: 'link',
|
|
778
876
|
image: 'image',
|
|
779
877
|
hardbreak: 'hardBreak',
|
|
878
|
+
table_open: 'table',
|
|
780
879
|
};
|
|
781
880
|
const TOKEN_UNSUPPORTED = {
|
|
782
|
-
code_inline: 'code',
|
|
783
881
|
code_block: 'codeBlock',
|
|
784
882
|
fence: 'codeBlock',
|
|
785
883
|
hr: 'rule',
|
|
@@ -801,15 +899,25 @@ const PLAIN_TOKENS = new Set([
|
|
|
801
899
|
'text',
|
|
802
900
|
'text_special',
|
|
803
901
|
'softbreak',
|
|
902
|
+
'table_close',
|
|
903
|
+
'thead_open',
|
|
904
|
+
'thead_close',
|
|
905
|
+
'tbody_open',
|
|
906
|
+
'tbody_close',
|
|
907
|
+
'tr_open',
|
|
908
|
+
'tr_close',
|
|
909
|
+
'th_open',
|
|
910
|
+
'th_close',
|
|
911
|
+
'td_open',
|
|
912
|
+
'td_close',
|
|
804
913
|
]);
|
|
805
914
|
/** Token types of `detector` for syntax that would otherwise be kept as literal text. */
|
|
806
915
|
const TOKEN_LITERAL = {
|
|
807
916
|
html_block: 'html',
|
|
808
917
|
html_inline: 'html',
|
|
809
|
-
table_open: 'table',
|
|
810
918
|
s_open: 'strike',
|
|
811
919
|
};
|
|
812
|
-
/** One problem per line and kind for HTML
|
|
920
|
+
/** One problem per line and kind for HTML and strikethrough in `markdown`. */
|
|
813
921
|
function literalSyntaxProblems(markdown) {
|
|
814
922
|
const problems = [];
|
|
815
923
|
const seen = new Set();
|
|
@@ -847,7 +955,7 @@ function findProblems(tokens, opts, env) {
|
|
|
847
955
|
problems.push({ code, line, message: t(id, { line, ...params }) });
|
|
848
956
|
};
|
|
849
957
|
const allowed = (f) => opts.features.includes(f);
|
|
850
|
-
const check = (tok, line, inHeading) => {
|
|
958
|
+
const check = (tok, line, inHeading, inCell) => {
|
|
851
959
|
const unsupported = TOKEN_UNSUPPORTED[tok.type];
|
|
852
960
|
if (unsupported)
|
|
853
961
|
return add('unsupported', line, 'richtext.unsupported', { feature: feature(unsupported) });
|
|
@@ -870,6 +978,9 @@ function findProblems(tokens, opts, env) {
|
|
|
870
978
|
if (inHeading && (name === 'image' || name === 'hardBreak')) {
|
|
871
979
|
return add('unsupported', line, 'richtext.headingContent', { feature: feature(name) });
|
|
872
980
|
}
|
|
981
|
+
if (inCell && (name === 'image' || name === 'hardBreak')) {
|
|
982
|
+
return add('unsupported', line, 'richtext.tableCell', { feature: feature(name) });
|
|
983
|
+
}
|
|
873
984
|
if (!allowed(name))
|
|
874
985
|
return add('not_allowed', line, 'richtext.notAllowed', { feature: feature(name) });
|
|
875
986
|
if (name === 'link' || name === 'image') {
|
|
@@ -887,35 +998,53 @@ function findProblems(tokens, opts, env) {
|
|
|
887
998
|
add('image_source', line, 'richtext.imageSource', { src });
|
|
888
999
|
}
|
|
889
1000
|
};
|
|
890
|
-
/**
|
|
891
|
-
|
|
1001
|
+
/**
|
|
1002
|
+
* Walks inline tokens; line breaks move the line on, including those inside an image's alt. Code inside bold or
|
|
1003
|
+
* italic is refused, since rich text can't hold both on the same text.
|
|
1004
|
+
*/
|
|
1005
|
+
const walkInline = (children, line, inHeading, inCell, inAlt) => {
|
|
1006
|
+
let emphasis = 0;
|
|
892
1007
|
for (const child of children) {
|
|
893
1008
|
if (!inAlt)
|
|
894
|
-
check(child, line, inHeading);
|
|
1009
|
+
check(child, line, inHeading, inCell);
|
|
1010
|
+
if (child.type === 'strong_open' || child.type === 'em_open')
|
|
1011
|
+
emphasis++;
|
|
1012
|
+
if (child.type === 'strong_close' || child.type === 'em_close')
|
|
1013
|
+
emphasis--;
|
|
1014
|
+
if (!inAlt && child.type === 'code_inline' && emphasis > 0 && allowed('code')) {
|
|
1015
|
+
add('unsupported', line, 'richtext.codeEmphasis', {});
|
|
1016
|
+
}
|
|
895
1017
|
if (child.type === 'softbreak' || child.type === 'hardbreak')
|
|
896
1018
|
line++;
|
|
897
1019
|
if (child.type === 'image' && child.children)
|
|
898
|
-
line = walkInline(child.children, line, inHeading, true);
|
|
1020
|
+
line = walkInline(child.children, line, inHeading, inCell, true);
|
|
899
1021
|
}
|
|
900
1022
|
return line;
|
|
901
1023
|
};
|
|
902
1024
|
let line = 1;
|
|
903
1025
|
let inHeading = false;
|
|
1026
|
+
let inCell = false;
|
|
904
1027
|
for (const tok of tokens) {
|
|
905
1028
|
if (tok.map)
|
|
906
1029
|
line = tok.map[0] + 1;
|
|
907
1030
|
if (tok.type === 'inline') {
|
|
908
|
-
walkInline(tok.children ?? [], line, inHeading, false);
|
|
1031
|
+
walkInline(tok.children ?? [], line, inHeading, inCell, false);
|
|
909
1032
|
continue;
|
|
910
1033
|
}
|
|
911
1034
|
if (tok.type === 'heading_open')
|
|
912
1035
|
inHeading = true;
|
|
913
1036
|
if (tok.type === 'heading_close')
|
|
914
1037
|
inHeading = false;
|
|
1038
|
+
if (tok.type === 'th_open' || tok.type === 'td_open')
|
|
1039
|
+
inCell = true;
|
|
1040
|
+
if (tok.type === 'th_close' || tok.type === 'td_close')
|
|
1041
|
+
inCell = false;
|
|
915
1042
|
if ((tok.type === 'blockquote_open' || tok.type === 'list_item_open') && tok.level + 1 >= MAX_NESTING) {
|
|
916
1043
|
add('unsupported', line, 'richtext.tooDeep', {});
|
|
917
1044
|
}
|
|
918
|
-
|
|
1045
|
+
if (tok.type === 'table_open' && tok.level > 0)
|
|
1046
|
+
add('unsupported', line, 'richtext.tableNested', {});
|
|
1047
|
+
check(tok, line, false, false);
|
|
919
1048
|
}
|
|
920
1049
|
// A definition no link uses would vanish; one that is used ends up in its links.
|
|
921
1050
|
for (const def of env.definitions ?? []) {
|
|
@@ -925,11 +1054,44 @@ function findProblems(tokens, opts, env) {
|
|
|
925
1054
|
}
|
|
926
1055
|
return problems.sort((a, b) => a.line - b.line);
|
|
927
1056
|
}
|
|
1057
|
+
/** The line (from 1) each top-level block of `markdown` starts on. */
|
|
1058
|
+
export function blockLines(markdown) {
|
|
1059
|
+
return tokenizer
|
|
1060
|
+
.parse(markdown, {})
|
|
1061
|
+
.flatMap((tok) => (tok.level === 0 && tok.nesting !== -1 && tok.map ? [tok.map[0] + 1] : []));
|
|
1062
|
+
}
|
|
1063
|
+
/**
|
|
1064
|
+
* Every image target in `markdown`, in order, as markdown-it reads it (escapes and `<…>` undone), with its line,
|
|
1065
|
+
* counted the way problems are. Images inside another image's alt, which become alt text, aren't listed.
|
|
1066
|
+
*/
|
|
1067
|
+
export function markdownImages(markdown) {
|
|
1068
|
+
const found = [];
|
|
1069
|
+
const walk = (children, line, inAlt) => {
|
|
1070
|
+
for (const child of children) {
|
|
1071
|
+
if (child.type === 'image' && !inAlt)
|
|
1072
|
+
found.push({ src: child.attrGet('src') ?? '', line });
|
|
1073
|
+
if (child.type === 'softbreak' || child.type === 'hardbreak')
|
|
1074
|
+
line++;
|
|
1075
|
+
if (child.type === 'image' && child.children)
|
|
1076
|
+
line = walk(child.children, line, true);
|
|
1077
|
+
}
|
|
1078
|
+
return line;
|
|
1079
|
+
};
|
|
1080
|
+
let line = 1;
|
|
1081
|
+
for (const tok of tokenizer.parse(markdown, {})) {
|
|
1082
|
+
if (tok.map)
|
|
1083
|
+
line = tok.map[0] + 1;
|
|
1084
|
+
if (tok.type === 'inline')
|
|
1085
|
+
walk(tok.children ?? [], line, false);
|
|
1086
|
+
}
|
|
1087
|
+
return found;
|
|
1088
|
+
}
|
|
928
1089
|
/**
|
|
929
1090
|
* Reads CommonMark into a normalized rich text doc allowed by `opts`. Anything the
|
|
930
1091
|
* format or the field can't hold is reported as a line-numbered problem. Never throws.
|
|
1092
|
+
* An image whose target is a key of `extra.images` reads as the library image it maps to.
|
|
931
1093
|
*/
|
|
932
|
-
export function markdownToRichText(markdown, opts) {
|
|
1094
|
+
export function markdownToRichText(markdown, opts, extra = {}) {
|
|
933
1095
|
const options = { ...DEFAULT_RICH_TEXT, ...opts };
|
|
934
1096
|
const unreadable = () => ({
|
|
935
1097
|
ok: false,
|
|
@@ -937,11 +1099,14 @@ export function markdownToRichText(markdown, opts) {
|
|
|
937
1099
|
});
|
|
938
1100
|
try {
|
|
939
1101
|
const env = trackingEnv();
|
|
1102
|
+
if (extra.images)
|
|
1103
|
+
env.images = extra.images;
|
|
940
1104
|
const tokens = tokenizer.parse(markdown, env);
|
|
941
1105
|
const problems = [...findProblems(tokens, options, env), ...literalSyntaxProblems(markdown)];
|
|
942
1106
|
if (problems.length > 0)
|
|
943
1107
|
return { ok: false, problems: problems.sort((a, b) => a.line - b.line) };
|
|
944
|
-
const
|
|
1108
|
+
const parseEnv = extra.images ? { images: extra.images } : {};
|
|
1109
|
+
const node = parser.parse(markdown, parseEnv);
|
|
945
1110
|
const parsed = parseRichText(node.toJSON(), options);
|
|
946
1111
|
return parsed.ok ? parsed : unreadable();
|
|
947
1112
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Schema } from 'prosemirror-model';
|
|
2
2
|
import type { ContentRef } from '../types.js';
|
|
3
|
-
export declare const RICH_TEXT_FEATURES: readonly ['heading', 'bold', 'italic', 'link', 'bulletList', 'orderedList', 'blockquote', 'image', 'hardBreak'];
|
|
3
|
+
export declare const RICH_TEXT_FEATURES: readonly ['heading', 'bold', 'italic', 'code', 'link', 'bulletList', 'orderedList', 'blockquote', 'image', 'hardBreak', 'table'];
|
|
4
4
|
export type RichTextFeature = (typeof RICH_TEXT_FEATURES)[number];
|
|
5
5
|
export interface RichTextOptions {
|
|
6
6
|
features: readonly RichTextFeature[];
|
|
@@ -18,6 +18,8 @@ export type RichTextMark = {
|
|
|
18
18
|
type: 'bold';
|
|
19
19
|
} | {
|
|
20
20
|
type: 'italic';
|
|
21
|
+
} | {
|
|
22
|
+
type: 'code';
|
|
21
23
|
};
|
|
22
24
|
export type RichTextText = {
|
|
23
25
|
type: 'text';
|
|
@@ -67,14 +69,29 @@ export type RichTextBlockquote = {
|
|
|
67
69
|
type: 'blockquote';
|
|
68
70
|
content: RichTextBlock[];
|
|
69
71
|
};
|
|
70
|
-
export type
|
|
71
|
-
|
|
72
|
+
export type RichTextTableCell = {
|
|
73
|
+
type: 'tableHeader' | 'tableCell';
|
|
74
|
+
content: [RichTextParagraph];
|
|
75
|
+
};
|
|
76
|
+
export type RichTextTableRow = {
|
|
77
|
+
type: 'tableRow';
|
|
78
|
+
content: RichTextTableCell[];
|
|
79
|
+
};
|
|
80
|
+
export type RichTextTable = {
|
|
81
|
+
type: 'table';
|
|
82
|
+
content: RichTextTableRow[];
|
|
83
|
+
};
|
|
84
|
+
export type RichTextBlock = RichTextParagraph | RichTextHeading | RichTextBulletList | RichTextOrderedList | RichTextBlockquote | RichTextTable;
|
|
85
|
+
export type RichTextNode = RichTextBlock | RichTextListItem | RichTextTableRow | RichTextTableCell | RichTextInline;
|
|
72
86
|
export type RichTextDoc = {
|
|
73
87
|
type: 'doc';
|
|
74
88
|
content: RichTextNode[];
|
|
75
89
|
};
|
|
76
|
-
/**
|
|
77
|
-
|
|
90
|
+
/**
|
|
91
|
+
* Tiptap's node and mark names. The mark order is the canonical order of marks on a node. Code comes last, inside a
|
|
92
|
+
* link, and never with bold or italic: Markdown can't put either inside a code span.
|
|
93
|
+
*/
|
|
94
|
+
export declare const richTextSchema: Schema<"blockquote" | "bulletList" | "doc" | "hardBreak" | "heading" | "image" | "listItem" | "orderedList" | "paragraph" | "table" | "tableCell" | "tableHeader" | "tableRow" | "text", "bold" | "code" | "italic" | "link">;
|
|
78
95
|
/**
|
|
79
96
|
* A link address rich text can hold: a safe URL without control characters, which
|
|
80
97
|
* Markdown can't carry and so wouldn't read back.
|
|
@@ -6,15 +6,20 @@ export const RICH_TEXT_FEATURES = [
|
|
|
6
6
|
'heading',
|
|
7
7
|
'bold',
|
|
8
8
|
'italic',
|
|
9
|
+
'code',
|
|
9
10
|
'link',
|
|
10
11
|
'bulletList',
|
|
11
12
|
'orderedList',
|
|
12
13
|
'blockquote',
|
|
13
14
|
'image',
|
|
14
15
|
'hardBreak',
|
|
16
|
+
'table',
|
|
15
17
|
];
|
|
16
18
|
export const DEFAULT_RICH_TEXT = { features: RICH_TEXT_FEATURES, headings: [2, 3, 4] };
|
|
17
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* Tiptap's node and mark names. The mark order is the canonical order of marks on a node. Code comes last, inside a
|
|
21
|
+
* link, and never with bold or italic: Markdown can't put either inside a code span.
|
|
22
|
+
*/
|
|
18
23
|
export const richTextSchema = new Schema({
|
|
19
24
|
nodes: {
|
|
20
25
|
doc: { content: 'block*' },
|
|
@@ -24,6 +29,10 @@ export const richTextSchema = new Schema({
|
|
|
24
29
|
orderedList: { group: 'block', content: 'listItem+', attrs: { start: { default: 1 } } },
|
|
25
30
|
listItem: { content: 'paragraph block*' },
|
|
26
31
|
blockquote: { group: 'block', content: 'block+' },
|
|
32
|
+
table: { group: 'block', content: 'tableRow+' },
|
|
33
|
+
tableRow: { content: '(tableHeader | tableCell)+' },
|
|
34
|
+
tableHeader: { content: 'paragraph' },
|
|
35
|
+
tableCell: { content: 'paragraph' },
|
|
27
36
|
text: { group: 'inline' },
|
|
28
37
|
hardBreak: { group: 'inline', inline: true },
|
|
29
38
|
image: { group: 'inline', inline: true, attrs: { media: {}, alt: { default: null } } },
|
|
@@ -32,6 +41,7 @@ export const richTextSchema = new Schema({
|
|
|
32
41
|
link: { attrs: { href: { default: null }, document: { default: null } }, inclusive: false },
|
|
33
42
|
bold: {},
|
|
34
43
|
italic: {},
|
|
44
|
+
code: { excludes: 'bold italic code' },
|
|
35
45
|
},
|
|
36
46
|
});
|
|
37
47
|
const NODE_FEATURE = {
|
|
@@ -41,6 +51,7 @@ const NODE_FEATURE = {
|
|
|
41
51
|
blockquote: 'blockquote',
|
|
42
52
|
image: 'image',
|
|
43
53
|
hardBreak: 'hardBreak',
|
|
54
|
+
table: 'table',
|
|
44
55
|
};
|
|
45
56
|
const LEAVES = new Set(['text', 'hardBreak', 'image']);
|
|
46
57
|
const HEX64 = /^[0-9a-f]{64}$/;
|
|
@@ -126,8 +137,10 @@ function cleanNode(raw, opts) {
|
|
|
126
137
|
if (!absent(raw.marks)) {
|
|
127
138
|
if (!Array.isArray(raw.marks))
|
|
128
139
|
reject();
|
|
129
|
-
|
|
130
|
-
|
|
140
|
+
// Code shows only on text: a line break or an image would lose it in Markdown.
|
|
141
|
+
const marks = raw.marks.map((m) => cleanMark(m, opts)).filter((m) => type === 'text' || m.type !== 'code');
|
|
142
|
+
if (marks.length > 0)
|
|
143
|
+
out.marks = marks;
|
|
131
144
|
}
|
|
132
145
|
if (type === 'text') {
|
|
133
146
|
if (typeof raw.text !== 'string')
|
|
@@ -152,6 +165,67 @@ function cleanNode(raw, opts) {
|
|
|
152
165
|
out.content = content;
|
|
153
166
|
return out;
|
|
154
167
|
}
|
|
168
|
+
/** A cell: one paragraph, several joined with a space; no image or line break. */
|
|
169
|
+
function cleanCell(raw, type, opts) {
|
|
170
|
+
if (!isRecord(raw) || (raw.type !== 'tableHeader' && raw.type !== 'tableCell'))
|
|
171
|
+
reject();
|
|
172
|
+
if (absent(raw.content))
|
|
173
|
+
return { type, content: [{ type: 'paragraph' }] };
|
|
174
|
+
if (!Array.isArray(raw.content))
|
|
175
|
+
reject();
|
|
176
|
+
const inline = [];
|
|
177
|
+
for (const child of raw.content) {
|
|
178
|
+
if (!isRecord(child) || child.type !== 'paragraph')
|
|
179
|
+
reject();
|
|
180
|
+
if (!absent(child.marks))
|
|
181
|
+
reject();
|
|
182
|
+
if (absent(child.content))
|
|
183
|
+
continue;
|
|
184
|
+
if (!Array.isArray(child.content))
|
|
185
|
+
reject();
|
|
186
|
+
const part = [];
|
|
187
|
+
for (const item of child.content) {
|
|
188
|
+
if (isRecord(item) && (item.type === 'image' || item.type === 'hardBreak'))
|
|
189
|
+
reject();
|
|
190
|
+
const node = cleanNode(item, opts);
|
|
191
|
+
if (node !== null)
|
|
192
|
+
part.push(node);
|
|
193
|
+
}
|
|
194
|
+
if (part.length === 0)
|
|
195
|
+
continue;
|
|
196
|
+
if (inline.length > 0)
|
|
197
|
+
inline.push({ type: 'text', text: ' ' });
|
|
198
|
+
inline.push(...part);
|
|
199
|
+
}
|
|
200
|
+
return { type, content: [inline.length > 0 ? { type: 'paragraph', content: inline } : { type: 'paragraph' }] };
|
|
201
|
+
}
|
|
202
|
+
/** A table whose first row is its header and whose rows all have the header's width, or `null` if it has no cells. */
|
|
203
|
+
function cleanTable(raw, parent, opts) {
|
|
204
|
+
if (parent !== 'doc' || !opts.features.includes('table'))
|
|
205
|
+
reject();
|
|
206
|
+
if (absent(raw.content))
|
|
207
|
+
return null;
|
|
208
|
+
if (!Array.isArray(raw.content))
|
|
209
|
+
reject();
|
|
210
|
+
const rows = raw.content.map((row) => {
|
|
211
|
+
if (!isRecord(row) || row.type !== 'tableRow')
|
|
212
|
+
reject();
|
|
213
|
+
if (absent(row.content))
|
|
214
|
+
return [];
|
|
215
|
+
return Array.isArray(row.content) ? row.content : reject();
|
|
216
|
+
});
|
|
217
|
+
const width = rows[0]?.length ?? 0;
|
|
218
|
+
if (width === 0)
|
|
219
|
+
return null;
|
|
220
|
+
const content = rows.map((cells, r) => {
|
|
221
|
+
const type = r === 0 ? 'tableHeader' : 'tableCell';
|
|
222
|
+
const padded = cells.slice(0, width).map((c) => cleanCell(c, type, opts));
|
|
223
|
+
while (padded.length < width)
|
|
224
|
+
padded.push({ type, content: [{ type: 'paragraph' }] });
|
|
225
|
+
return { type: 'tableRow', content: padded };
|
|
226
|
+
});
|
|
227
|
+
return { type: 'table', content };
|
|
228
|
+
}
|
|
155
229
|
/**
|
|
156
230
|
* Empty paragraphs (including ones left empty once trailing line breaks are dropped) are
|
|
157
231
|
* dropped, except a list item's first; so are blockquotes left empty.
|
|
@@ -163,7 +237,7 @@ function cleanContent(raw, parent, opts) {
|
|
|
163
237
|
reject();
|
|
164
238
|
const out = [];
|
|
165
239
|
for (const child of raw) {
|
|
166
|
-
const node = cleanNode(child, opts);
|
|
240
|
+
const node = isRecord(child) && child.type === 'table' ? cleanTable(child, parent, opts) : cleanNode(child, opts);
|
|
167
241
|
if (node === null)
|
|
168
242
|
continue;
|
|
169
243
|
const empty = node.content === undefined;
|
|
@@ -228,12 +302,15 @@ function inlineText(node) {
|
|
|
228
302
|
})
|
|
229
303
|
.join('');
|
|
230
304
|
}
|
|
305
|
+
const inlineCell = (cell) => inlineText(childrenOf(cell)[0]);
|
|
231
306
|
function blockTexts(node, out) {
|
|
232
307
|
for (const child of childrenOf(node)) {
|
|
233
308
|
if (!isRecord(child))
|
|
234
309
|
continue;
|
|
235
310
|
if (child.type === 'paragraph' || child.type === 'heading')
|
|
236
311
|
out.push(inlineText(child));
|
|
312
|
+
else if (child.type === 'tableRow')
|
|
313
|
+
out.push(childrenOf(child).map(inlineCell).join('\t'));
|
|
237
314
|
else
|
|
238
315
|
blockTexts(child, out);
|
|
239
316
|
}
|
package/dist/core/site/live.d.ts
CHANGED
|
@@ -24,6 +24,7 @@ export declare function liveQuery(deps: CoreDeps, state: LiveState): import("kys
|
|
|
24
24
|
created_at: string;
|
|
25
25
|
deleted_at: string | null;
|
|
26
26
|
edition: number | null;
|
|
27
|
+
first_published_at: string | null;
|
|
27
28
|
group_id: string;
|
|
28
29
|
head_path: string | null;
|
|
29
30
|
head_rev: number | null;
|
package/dist/core/site/live.js
CHANGED
|
@@ -4,6 +4,12 @@ export declare const RESERVED_PATHS: readonly string[];
|
|
|
4
4
|
export declare function isReserved(path: string): boolean;
|
|
5
5
|
/** The decoded, slash-trimmed form of a site path, or `null` if it isn't a clean site-relative path. */
|
|
6
6
|
export declare function normalizePath(raw: string): string | null;
|
|
7
|
+
/**
|
|
8
|
+
* The lowercase form of a decoded, slash-trimmed path, for the fallback tried just before a 404.
|
|
9
|
+
* `null` when the path has no uppercase letter, lowercases to a reserved path, or ends in a segment
|
|
10
|
+
* that looks like a file (contains a `.`).
|
|
11
|
+
*/
|
|
12
|
+
export declare function lowercaseFallback(path: string): string | null;
|
|
7
13
|
export declare function encodePath(path: string): string;
|
|
8
14
|
export declare function splitLocale(registry: Registry, path: string): {
|
|
9
15
|
locale: string;
|
package/dist/core/site/paths.js
CHANGED
|
@@ -32,6 +32,19 @@ export function normalizePath(raw) {
|
|
|
32
32
|
return null;
|
|
33
33
|
return decoded.length > 1 && decoded.endsWith('/') ? decoded.slice(0, -1) : decoded;
|
|
34
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* The lowercase form of a decoded, slash-trimmed path, for the fallback tried just before a 404.
|
|
37
|
+
* `null` when the path has no uppercase letter, lowercases to a reserved path, or ends in a segment
|
|
38
|
+
* that looks like a file (contains a `.`).
|
|
39
|
+
*/
|
|
40
|
+
export function lowercaseFallback(path) {
|
|
41
|
+
const lower = path.toLowerCase();
|
|
42
|
+
if (lower === path || isReserved(lower))
|
|
43
|
+
return null;
|
|
44
|
+
if ((lower.split('/').at(-1) ?? '').includes('.'))
|
|
45
|
+
return null;
|
|
46
|
+
return lower;
|
|
47
|
+
}
|
|
35
48
|
export function encodePath(path) {
|
|
36
49
|
return path.split('/').map(encodeURIComponent).join('/');
|
|
37
50
|
}
|
|
@@ -3,7 +3,7 @@ import { type LiveOpts } from './live.js';
|
|
|
3
3
|
/** Resolves a still-encoded URL path (no query string) to a page, a redirect or not-found. */
|
|
4
4
|
export declare function resolvePath(deps: CoreDeps, rawPath: string, opts?: LiveOpts): Promise<RouteMatch>;
|
|
5
5
|
/**
|
|
6
|
-
* What `resolvePath` answers before it looks at redirect rules (steps 1–6, then
|
|
6
|
+
* What `resolvePath` answers before it looks at redirect rules (steps 1–6, then 9): a redirect rule
|
|
7
7
|
* can only fire where this is `not_found`.
|
|
8
8
|
*/
|
|
9
9
|
export declare function resolveWithoutRules(deps: CoreDeps, rawPath: string, opts?: LiveOpts): Promise<RouteMatch>;
|