inlaycms 0.1.0 → 0.3.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.
Files changed (121) hide show
  1. package/README.md +344 -8
  2. package/assets/admin/admin.css +1 -1
  3. package/assets/admin/admin.js +1 -1
  4. package/assets/admin/editor.js +59 -44
  5. package/dist/adapters/admin/screens/redirects.js +12 -3
  6. package/dist/adapters/admin/views/layout.js +27 -7
  7. package/dist/adapters/cli/remote.js +9 -1
  8. package/dist/adapters/mcp/tools/redirects.js +16 -2
  9. package/dist/adapters/site/assets.d.ts +11 -0
  10. package/dist/adapters/site/assets.js +71 -0
  11. package/dist/adapters/site/cache.d.ts +2 -1
  12. package/dist/adapters/site/cache.js +6 -1
  13. package/dist/adapters/site/context.d.ts +6 -0
  14. package/dist/adapters/site/context.js +38 -13
  15. package/dist/adapters/site/head.d.ts +30 -7
  16. package/dist/adapters/site/head.js +145 -20
  17. package/dist/adapters/site/headers.d.ts +5 -0
  18. package/dist/adapters/site/headers.js +31 -0
  19. package/dist/adapters/site/plugin.d.ts +2 -0
  20. package/dist/adapters/site/plugin.js +32 -9
  21. package/dist/adapters/site/public.js +7 -24
  22. package/dist/adapters/site/render.d.ts +5 -0
  23. package/dist/adapters/site/render.js +23 -8
  24. package/dist/adapters/site/routes.d.ts +6 -0
  25. package/dist/adapters/site/routes.js +8 -3
  26. package/dist/adapters/site/seo.d.ts +20 -0
  27. package/dist/adapters/site/seo.js +45 -0
  28. package/dist/adapters/site/sitemap.d.ts +1 -1
  29. package/dist/adapters/site/sitemap.js +1 -1
  30. package/dist/config.js +3 -2
  31. package/dist/core/agent/format.js +1 -1
  32. package/dist/core/agent/schema.js +3 -0
  33. package/dist/core/boot.js +7 -0
  34. package/dist/core/check.js +6 -1
  35. package/dist/core/content/dry-run.d.ts +7 -6
  36. package/dist/core/content/dry-run.js +8 -8
  37. package/dist/core/content/publish.d.ts +1 -1
  38. package/dist/core/content/publish.js +5 -3
  39. package/dist/core/content/read.d.ts +3 -0
  40. package/dist/core/content/reader.js +3 -2
  41. package/dist/core/content/refs.js +1 -1
  42. package/dist/core/content/search.js +2 -2
  43. package/dist/core/content/shipped-images.d.ts +32 -0
  44. package/dist/core/content/shipped-images.js +62 -0
  45. package/dist/core/content/shipped-input.d.ts +9 -3
  46. package/dist/core/content/shipped-input.js +165 -28
  47. package/dist/core/content/shipped.js +8 -3
  48. package/dist/core/content/validate.js +7 -1
  49. package/dist/core/core.d.ts +5 -1
  50. package/dist/core/core.js +5 -4
  51. package/dist/core/db/connection.d.ts +6 -0
  52. package/dist/core/db/connection.js +13 -1
  53. package/dist/core/db/migrate.js +2 -0
  54. package/dist/core/db/migrations/0007_published_at.d.ts +3 -0
  55. package/dist/core/db/migrations/0007_published_at.js +16 -0
  56. package/dist/core/db/schema.d.ts +1 -0
  57. package/dist/core/export/export.js +3 -1
  58. package/dist/core/export/format.d.ts +8 -1
  59. package/dist/core/export/format.js +3 -2
  60. package/dist/core/export/read.js +7 -0
  61. package/dist/core/export/stage.d.ts +1 -1
  62. package/dist/core/export/stage.js +24 -1
  63. package/dist/core/fields/builtins.d.ts +28 -0
  64. package/dist/core/fields/builtins.js +103 -11
  65. package/dist/core/fields/define.d.ts +6 -0
  66. package/dist/core/fields/widgets.d.ts +7 -0
  67. package/dist/core/fields/widgets.js +76 -28
  68. package/dist/core/messages/en.d.ts +70 -6
  69. package/dist/core/messages/en.js +70 -6
  70. package/dist/core/redirects/collapse.d.ts +17 -1
  71. package/dist/core/redirects/collapse.js +19 -3
  72. package/dist/core/redirects/redirects.d.ts +13 -12
  73. package/dist/core/redirects/redirects.js +141 -53
  74. package/dist/core/redirects/rule.d.ts +25 -0
  75. package/dist/core/redirects/rule.js +55 -0
  76. package/dist/core/registry/define.d.ts +40 -0
  77. package/dist/core/registry/define.js +3 -0
  78. package/dist/core/registry/registry.d.ts +21 -0
  79. package/dist/core/registry/registry.js +114 -6
  80. package/dist/core/registry/render.d.ts +37 -1
  81. package/dist/core/registry/route.d.ts +4 -0
  82. package/dist/core/release/bundle.js +30 -5
  83. package/dist/core/release/manifest.d.ts +14 -0
  84. package/dist/core/release/manifest.js +42 -17
  85. package/dist/core/release/markdown-file.d.ts +21 -0
  86. package/dist/core/release/markdown-file.js +172 -0
  87. package/dist/core/release/pack.js +2 -2
  88. package/dist/core/release/read.js +4 -1
  89. package/dist/core/release/site-dir.d.ts +4 -0
  90. package/dist/core/release/site-dir.js +8 -0
  91. package/dist/core/report.d.ts +8 -2
  92. package/dist/core/richtext/html.d.ts +3 -1
  93. package/dist/core/richtext/html.js +16 -4
  94. package/dist/core/richtext/inline.d.ts +22 -0
  95. package/dist/core/richtext/inline.js +33 -0
  96. package/dist/core/richtext/markdown.d.ts +16 -1
  97. package/dist/core/richtext/markdown.js +137 -14
  98. package/dist/core/richtext/schema.d.ts +16 -4
  99. package/dist/core/richtext/schema.js +71 -1
  100. package/dist/core/site/live.d.ts +1 -0
  101. package/dist/core/site/live.js +1 -0
  102. package/dist/core/site/paths.d.ts +6 -0
  103. package/dist/core/site/paths.js +13 -0
  104. package/dist/core/site/resolve.d.ts +1 -1
  105. package/dist/core/site/resolve.js +119 -30
  106. package/dist/core/site/sitemap.d.ts +1 -1
  107. package/dist/core/site/sitemap.js +4 -0
  108. package/dist/core/types.d.ts +28 -3
  109. package/dist/host/app.d.ts +5 -0
  110. package/dist/host/app.js +8 -0
  111. package/dist/host/dev.js +3 -7
  112. package/dist/host/diff.d.ts +19 -1
  113. package/dist/host/diff.js +32 -2
  114. package/dist/index.d.ts +2 -2
  115. package/dist/markdown-modules.d.ts +8 -0
  116. package/dist/markdown-modules.js +1 -0
  117. package/dist/site.d.ts +8 -3
  118. package/dist/site.js +6 -0
  119. package/dist/testing.d.ts +22 -0
  120. package/dist/testing.js +155 -0
  121. package/package.json +6 -1
@@ -0,0 +1,172 @@
1
+ import { pathToFileURL } from 'node:url';
2
+ import MarkdownIt from 'markdown-it';
3
+ import { imageTarget } from '../content/shipped-images.js';
4
+ import { markdownDestination } from '../richtext/markdown.js';
5
+ /** Reads the way `markdownToRichText` does, with each image's place in the source kept on its token's `meta`. */
6
+ const reader = new MarkdownIt('commonmark', { html: false, linkify: false, typographer: false }).enable('table');
7
+ reader.validateLink = () => true;
8
+ reader.normalizeLink = (url) => url;
9
+ reader.normalizeLinkText = (url) => url;
10
+ const imageRule = reader.inline.ruler.getRules('').find((rule) => rule.name === 'image');
11
+ if (imageRule) {
12
+ reader.inline.ruler.at('image', (state, silent) => {
13
+ const start = state.pos;
14
+ if (silent || state.src.charCodeAt(start) !== 0x21 /* ! */)
15
+ return imageRule(state, silent);
16
+ const labelEnd = state.md.helpers.parseLinkLabel(state, start + 1, false);
17
+ const inline = labelEnd >= 0 && state.src.charCodeAt(labelEnd + 1) === 0x28; /* ( */
18
+ let dest = null;
19
+ if (inline) {
20
+ let pos = labelEnd + 2;
21
+ while (pos < state.posMax) {
22
+ const code = state.src.charCodeAt(pos);
23
+ if (!state.md.utils.isSpace(code) && code !== 0x0a)
24
+ break;
25
+ pos++;
26
+ }
27
+ const res = state.md.helpers.parseLinkDestination(state.src, pos, state.posMax);
28
+ if (res.ok)
29
+ dest = { start: pos, end: res.pos, src: res.str };
30
+ }
31
+ if (!imageRule(state, silent))
32
+ return false;
33
+ const token = state.tokens[state.tokens.length - 1];
34
+ if (token?.type === 'image')
35
+ token.meta = { start, inline, dest };
36
+ return true;
37
+ });
38
+ }
39
+ /** True if `src` names a file by a path relative to where it is written, by `imageTarget`'s rules. */
40
+ function isRelative(src) {
41
+ return src !== '' && !/^file:/i.test(src) && imageTarget(src, null).kind === 'outside';
42
+ }
43
+ /**
44
+ * The inline content of each block, with the source line (from 0) it starts on and its images. `escapedPipe` is true
45
+ * for a table cell on a line with an escaped pipe (`\|`), which markdown-it reads without its backslash.
46
+ */
47
+ function inlineBlocks(text) {
48
+ const blocks = [];
49
+ const lines = text.split(/\r\n?|\n/);
50
+ // A table cell's inline content has no lines of its own: it sits on its row's line.
51
+ let line = 0;
52
+ let cell = false;
53
+ for (const tok of reader.parse(text, {})) {
54
+ if (tok.map)
55
+ line = tok.map[0];
56
+ if (tok.type === 'td_open' || tok.type === 'th_open')
57
+ cell = true;
58
+ if (tok.type === 'td_close' || tok.type === 'th_close')
59
+ cell = false;
60
+ if (tok.type !== 'inline')
61
+ continue;
62
+ const images = (tok.children ?? []).flatMap((child) => (child.type === 'image' && child.meta ? [child.meta] : []));
63
+ blocks.push({ line, content: tok.content, images, escapedPipe: cell && (lines[line] ?? '').includes('\\|') });
64
+ }
65
+ return blocks;
66
+ }
67
+ /** How many lines into `content` the offset `at` is. */
68
+ function lineIndex(content, at) {
69
+ let k = 0;
70
+ for (let i = content.indexOf('\n'); i !== -1 && i < at; i = content.indexOf('\n', i + 1))
71
+ k++;
72
+ return k;
73
+ }
74
+ /**
75
+ * `text` with each inline image target for which `to` gives a new target rewritten to it, everything else, line
76
+ * endings included, left as written, and every line where it was. `references` lists the line (from 1) of each
77
+ * reference-style image (`![alt][name]`, `![alt][]`, `![alt]`), which is never rewritten. `unplaced` lists each
78
+ * image `to` gave a new target for but whose place in the source couldn't be found, so it was left as written.
79
+ */
80
+ function rewriteTargets(text, to) {
81
+ const blocks = inlineBlocks(text);
82
+ // Lines at even indexes, each followed by the line ending it had.
83
+ const parts = text.split(/(\r\n?|\n)/);
84
+ const references = [];
85
+ const unplaced = [];
86
+ for (const block of blocks) {
87
+ for (const image of block.images) {
88
+ if (!image.inline)
89
+ references.push(block.line + lineIndex(block.content, image.start) + 1);
90
+ }
91
+ }
92
+ // Cells of one table row share a line: going right to left, each is looked for before the one after it.
93
+ const bound = new Map();
94
+ const edits = new Map();
95
+ for (let b = blocks.length - 1; b >= 0; b--) {
96
+ const block = blocks[b];
97
+ if (block === undefined)
98
+ continue;
99
+ const contentLines = block.content.split('\n');
100
+ // Where each line of the content starts in the source line, as an offset; null when it isn't found. A tab past a
101
+ // list item's indent reaches the content as spaces, so a line not found as it is is looked for without them.
102
+ const columns = contentLines.map((contentLine, k) => {
103
+ const n = block.line + k;
104
+ const source = parts[2 * n];
105
+ if (source === undefined)
106
+ return null;
107
+ const limit = bound.get(n);
108
+ const find = (s) => limit === undefined ? source.lastIndexOf(s) : limit < s.length ? -1 : source.lastIndexOf(s, limit - s.length);
109
+ let at = find(contentLine);
110
+ let stripped = 0;
111
+ if (at === -1) {
112
+ const trimmed = contentLine.trimStart();
113
+ stripped = contentLine.length - trimmed.length;
114
+ if (stripped > 0 && trimmed !== '')
115
+ at = find(trimmed);
116
+ }
117
+ if (at === -1)
118
+ return null;
119
+ bound.set(n, at);
120
+ return at - stripped;
121
+ });
122
+ for (const image of block.images) {
123
+ const dest = image.dest;
124
+ const target = dest === null ? null : to(dest.src);
125
+ if (dest === null || target === null)
126
+ continue;
127
+ const k = lineIndex(block.content, dest.start);
128
+ const n = block.line + k;
129
+ const column = columns[k] ?? null;
130
+ const lineStart = k === 0 ? 0 : block.content.lastIndexOf('\n', dest.start - 1) + 1;
131
+ const from = column === null ? -1 : column + dest.start - lineStart;
132
+ const end = column === null ? -1 : column + dest.end - lineStart;
133
+ if (from < 0 || parts[2 * n]?.slice(from, end) !== block.content.slice(dest.start, dest.end)) {
134
+ unplaced.push({ line: block.line + lineIndex(block.content, image.start) + 1, escapedPipe: block.escapedPipe });
135
+ continue;
136
+ }
137
+ const list = edits.get(n) ?? [];
138
+ list.push({ from, to: end, text: markdownDestination(target) });
139
+ edits.set(n, list);
140
+ }
141
+ }
142
+ for (const [n, list] of edits) {
143
+ let source = parts[2 * n] ?? '';
144
+ for (const edit of list.sort((a, b) => b.from - a.from)) {
145
+ source = source.slice(0, edit.from) + edit.text + source.slice(edit.to);
146
+ }
147
+ parts[2 * n] = source;
148
+ }
149
+ return { text: parts.join(''), references, unplaced: unplaced.sort((a, b) => a.line - b.line) };
150
+ }
151
+ /**
152
+ * The Markdown file `text`, read from `file`, with each relative inline image target rewritten to the `file:` URL it
153
+ * names from the file's own folder, so the image resolves the same once the text is bundled. Everything else, line
154
+ * endings included, is left as written, and every line stays where it was. `references` lists the line (from 1) of
155
+ * each reference-style image, which is never resolved. `unplaced` lists the line (from 1) of each relative image
156
+ * whose place in the source can't be found (a line with a NUL character, say), left as written; one in a table cell
157
+ * with an escaped pipe isn't listed, since such a cell is refused when the text is read as rich text.
158
+ */
159
+ export function rewriteImageTargets(text, file) {
160
+ const base = pathToFileURL(file);
161
+ const result = rewriteTargets(text, (src) => (isRelative(src) ? new URL(src, base).href : null));
162
+ const unplaced = result.unplaced.filter((u) => !u.escapedPipe).map((u) => u.line);
163
+ return { text: result.text, references: result.references, unplaced };
164
+ }
165
+ /**
166
+ * `markdown` with each inline image target for which `to` gives a new target rewritten to it, the way
167
+ * `rewriteImageTargets` rewrites them. `complete` is false if one of them couldn't be placed and was left as written.
168
+ */
169
+ export function replaceImageTargets(markdown, to) {
170
+ const result = rewriteTargets(markdown, to);
171
+ return { text: result.text, complete: result.unplaced.length === 0 };
172
+ }
@@ -57,10 +57,10 @@ export async function packRelease(opts) {
57
57
  const sources = shippedFiles(registry);
58
58
  if (sources.length > 0)
59
59
  await mkdir(join(tmp, 'content'));
60
- for (const { source, path } of sources) {
60
+ for (const { source, path, read } of sources) {
61
61
  let bytes;
62
62
  try {
63
- bytes = await readFile(path);
63
+ bytes = await readFile(read);
64
64
  }
65
65
  catch {
66
66
  throw new InlayError('release', 'error.packFile', { path });
@@ -10,6 +10,7 @@ import { buildRegistry } from '../registry/registry.js';
10
10
  import { inlayVersion as currentVersion } from '../version.js';
11
11
  import { fingerprint, verify } from './keys.js';
12
12
  import { manifestSchema, satisfies, sha256Hex } from './manifest.js';
13
+ import { SITE_DIR_EXPORT } from './site-dir.js';
13
14
  const SIGNED = new Set(['manifest.json', 'manifest.sig']);
14
15
  /** `.ts` when running from `src/` (tsx, vitest), `.js` from `dist/`. */
15
16
  const EXT = import.meta.url.endsWith('.ts') ? '.ts' : '.js';
@@ -23,6 +24,7 @@ export const INLAY_ENTRIES = {
23
24
  'inlaycms/site': entry('site'),
24
25
  'inlaycms/jsx-runtime': entry('jsx/jsx-runtime'),
25
26
  'inlaycms/jsx-dev-runtime': entry('jsx/jsx-dev-runtime'),
27
+ 'inlaycms/testing': entry('testing'),
26
28
  };
27
29
  /** The files of the `inlaycms` shim package, by name. */
28
30
  function shimFiles() {
@@ -233,7 +235,8 @@ export async function loadRelease(dir, opts = {}) {
233
235
  if (!config || typeof config !== 'object' || !config.site || typeof config.site !== 'object') {
234
236
  throw new InlayError('release', 'error.releaseNoSite');
235
237
  }
236
- const site = config.site;
238
+ const dirUrl = mod?.[SITE_DIR_EXPORT];
239
+ const site = typeof dirUrl === 'string' ? { ...config.site, siteDir: dirUrl } : config.site;
237
240
  buildRegistry(site);
238
241
  if (!isDeepStrictEqual([...site.secrets].sort(), [...manifest.secrets].sort())) {
239
242
  throw new InlayError('release', 'error.releaseSecretsMismatch', {
@@ -0,0 +1,4 @@
1
+ /** The name of the export a bundled `site.js` carries to say which folder it was built from. */
2
+ export declare const SITE_DIR_EXPORT = "__inlaySiteDir";
3
+ /** The folder `dir` as a `file:` URL ending in one `/`. */
4
+ export declare function siteDirUrl(dir: string): string;
@@ -0,0 +1,8 @@
1
+ import { pathToFileURL } from 'node:url';
2
+ /** The name of the export a bundled `site.js` carries to say which folder it was built from. */
3
+ export const SITE_DIR_EXPORT = '__inlaySiteDir';
4
+ /** The folder `dir` as a `file:` URL ending in one `/`. */
5
+ export function siteDirUrl(dir) {
6
+ const href = pathToFileURL(dir).href;
7
+ return href.endsWith('/') ? href : `${href}/`;
8
+ }
@@ -8,7 +8,7 @@ export interface CheckProblem {
8
8
  issues?: ContentIssue[];
9
9
  }
10
10
  /** Something the developer should fix that doesn't stop boot. `message` is written for the developer. */
11
- export interface CheckWarning {
11
+ export type CheckWarning = {
12
12
  code: 'slug_shadowed';
13
13
  message: string;
14
14
  document: {
@@ -17,7 +17,13 @@ export interface CheckWarning {
17
17
  locale: string;
18
18
  title: string;
19
19
  };
20
- }
20
+ } | {
21
+ code: 'redirect_hidden' | 'redirect_loop';
22
+ message: string;
23
+ redirect: {
24
+ from: string;
25
+ };
26
+ };
21
27
  /** What boot or `inlaycms check` did (or would do) to a database. */
22
28
  export interface CheckReport {
23
29
  ok: boolean;
@@ -1,4 +1,4 @@
1
- import { type RichTextDoc } from './schema.js';
1
+ import { type RichTextDoc, type RichTextInline } from './schema.js';
2
2
  export interface RichTextImageHtml {
3
3
  src: string;
4
4
  srcset: string;
@@ -11,5 +11,7 @@ export interface RichTextResolver {
11
11
  link(groupId: string): string | null;
12
12
  image(id: string, alt: string | undefined): RichTextImageHtml | null;
13
13
  }
14
+ /** Inline content, keeping a mark open across neighbors that share it. */
15
+ export declare function richTextInlineHtml(nodes: RichTextInline[], resolve: RichTextResolver): string;
14
16
  /** The doc as HTML. Every text and attribute is escaped; only Inlay's own tags are emitted. */
15
17
  export declare function richTextToHtml(doc: RichTextDoc, resolve: RichTextResolver): string;
@@ -32,7 +32,7 @@ function leaf(node, resolve) {
32
32
  `${attr('width', img.width)}${attr('height', img.height)} loading="lazy">`);
33
33
  }
34
34
  /** Inline content, keeping a mark open across neighbors that share it. */
35
- function inline(nodes, resolve) {
35
+ export function richTextInlineHtml(nodes, resolve) {
36
36
  let out = '';
37
37
  let open = [];
38
38
  for (const node of nodes) {
@@ -55,11 +55,11 @@ function block(node, resolve) {
55
55
  const children = (nodes) => (nodes ?? []).map((n) => block(n, resolve)).join('');
56
56
  switch (node.type) {
57
57
  case 'paragraph':
58
- return `<p>${inline(node.content ?? [], resolve)}</p>`;
58
+ return `<p>${richTextInlineHtml(node.content ?? [], resolve)}</p>`;
59
59
  case 'heading': {
60
60
  const level = node.attrs.level;
61
61
  const tag = Number.isInteger(level) && level >= 1 && level <= 6 ? `h${level}` : 'p';
62
- return `<${tag}>${inline(node.content ?? [], resolve)}</${tag}>`;
62
+ return `<${tag}>${richTextInlineHtml(node.content ?? [], resolve)}</${tag}>`;
63
63
  }
64
64
  case 'bulletList':
65
65
  return `<ul>${children(node.content)}</ul>`;
@@ -71,8 +71,20 @@ function block(node, resolve) {
71
71
  return `<li>${children(node.content)}</li>`;
72
72
  case 'blockquote':
73
73
  return `<blockquote>${children(node.content)}</blockquote>`;
74
+ case 'table': {
75
+ const cells = (row, tag) => `<tr>${row.content.map((c) => `<${tag}>${richTextInlineHtml(c.content[0].content ?? [], resolve)}</${tag}>`).join('')}</tr>`;
76
+ const [head, ...body] = node.content;
77
+ return ('<table>' +
78
+ (head ? `<thead>${cells(head, 'th')}</thead>` : '') +
79
+ (body.length > 0 ? `<tbody>${body.map((r) => cells(r, 'td')).join('')}</tbody>` : '') +
80
+ '</table>');
81
+ }
82
+ case 'tableRow':
83
+ case 'tableHeader':
84
+ case 'tableCell':
85
+ return '';
74
86
  default:
75
- return inline([node], resolve);
87
+ return richTextInlineHtml([node], resolve);
76
88
  }
77
89
  }
78
90
  /** The doc as HTML. Every text and attribute is escaped; only Inlay's own tags are emitted. */
@@ -0,0 +1,22 @@
1
+ import { type RichTextResolver } from './html.js';
2
+ import { type RichTextProblem } from './markdown.js';
3
+ import { type RichTextDoc, type RichTextOptions } from './schema.js';
4
+ /** What an inline-text field allows: one paragraph of bold, italic and linked text. */
5
+ export declare const INLINE_TEXT: RichTextOptions;
6
+ /** `value` as a normalized inline-text doc: at most one block, and that a paragraph. */
7
+ export declare function parseInlineText(value: unknown): {
8
+ ok: true;
9
+ doc: RichTextDoc;
10
+ } | {
11
+ ok: false;
12
+ };
13
+ /** Reads one line of Markdown into an inline-text doc; a second paragraph is reported at the line it starts on. */
14
+ export declare function markdownToInlineText(markdown: string): {
15
+ ok: true;
16
+ doc: RichTextDoc;
17
+ } | {
18
+ ok: false;
19
+ problems: RichTextProblem[];
20
+ };
21
+ /** The first top-level paragraph's inline HTML, without its `<p>`; `''` when there is none. */
22
+ export declare function inlineTextToHtml(doc: RichTextDoc, resolve: RichTextResolver): string;
@@ -0,0 +1,33 @@
1
+ import { t } from '../messages/index.js';
2
+ import { richTextInlineHtml } from './html.js';
3
+ import { blockLines, markdownToRichText } from './markdown.js';
4
+ import { parseRichText } from './schema.js';
5
+ /** What an inline-text field allows: one paragraph of bold, italic and linked text. */
6
+ export const INLINE_TEXT = { features: ['bold', 'italic', 'link'], headings: [] };
7
+ /** `value` as a normalized inline-text doc: at most one block, and that a paragraph. */
8
+ export function parseInlineText(value) {
9
+ const parsed = parseRichText(value, INLINE_TEXT);
10
+ if (!parsed.ok)
11
+ return parsed;
12
+ const [first, ...rest] = parsed.doc.content;
13
+ return rest.length === 0 && (first === undefined || first.type === 'paragraph') ? parsed : { ok: false };
14
+ }
15
+ /** Reads one line of Markdown into an inline-text doc; a second paragraph is reported at the line it starts on. */
16
+ export function markdownToInlineText(markdown) {
17
+ const result = markdownToRichText(markdown, INLINE_TEXT);
18
+ const second = blockLines(markdown)[1] ?? (result.ok && result.doc.content.length > 1 ? 1 : undefined);
19
+ if (second === undefined)
20
+ return result;
21
+ const paragraphs = {
22
+ code: 'not_allowed',
23
+ line: second,
24
+ message: t('richtext.inlineParagraphs', { line: second }),
25
+ };
26
+ const problems = result.ok ? [paragraphs] : [...result.problems, paragraphs].sort((a, b) => a.line - b.line);
27
+ return { ok: false, problems };
28
+ }
29
+ /** The first top-level paragraph's inline HTML, without its `<p>`; `''` when there is none. */
30
+ export function inlineTextToHtml(doc, resolve) {
31
+ const first = doc.content.find((node) => node.type === 'paragraph');
32
+ return first?.type === 'paragraph' ? richTextInlineHtml(first.content ?? [], resolve) : '';
33
+ }
@@ -4,13 +4,28 @@ export interface RichTextProblem {
4
4
  line: number;
5
5
  message: string;
6
6
  }
7
+ /** URL as a link destination: `<…>` when it has spaces or brackets, escaped so markdown-it gives it back unchanged. */
8
+ export declare function markdownDestination(url: string): string;
7
9
  /** The doc as CommonMark: tight lists, `- ` bullets, `inlay:` URIs for internal links and images. */
8
10
  export declare function richTextToMarkdown(doc: RichTextDoc): string;
11
+ /** The line (from 1) each top-level block of `markdown` starts on. */
12
+ export declare function blockLines(markdown: string): number[];
13
+ /**
14
+ * Every image target in `markdown`, in order, as markdown-it reads it (escapes and `<…>` undone), with its line,
15
+ * counted the way problems are. Images inside another image's alt, which become alt text, aren't listed.
16
+ */
17
+ export declare function markdownImages(markdown: string): Array<{
18
+ src: string;
19
+ line: number;
20
+ }>;
9
21
  /**
10
22
  * Reads CommonMark into a normalized rich text doc allowed by `opts`. Anything the
11
23
  * format or the field can't hold is reported as a line-numbered problem. Never throws.
24
+ * An image whose target is a key of `extra.images` reads as the library image it maps to.
12
25
  */
13
- export declare function markdownToRichText(markdown: string, opts?: Partial<RichTextOptions>): {
26
+ export declare function markdownToRichText(markdown: string, opts?: Partial<RichTextOptions>, extra?: {
27
+ images?: ReadonlyMap<string, string>;
28
+ }): {
14
29
  ok: true;
15
30
  doc: RichTextDoc;
16
31
  } | {