linopress-cli 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +136 -2
- package/bin/linopress.js +3 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +627 -0
- package/dist/config-B5ZjA9MO.js +143 -0
- package/dist/config.d.ts +68 -0
- package/dist/config.js +1 -0
- package/dist/index.d.ts +261 -0
- package/dist/index.js +3 -0
- package/dist/linear.d.ts +66 -0
- package/dist/linear.js +1 -0
- package/dist/markdown.d.ts +113 -0
- package/dist/markdown.js +1 -0
- package/dist/model-CgSIC_VE.d.ts +42 -0
- package/dist/model-DAbyGE8k.js +185 -0
- package/dist/src-6dU0mCpU.js +227 -0
- package/dist/src-BU0LDvW9.js +593 -0
- package/dist/src-vhdzyr4k.js +160 -0
- package/dist/src-zk0Kzn3v.js +249 -0
- package/dist/target-Cb9cQbDZ.d.ts +48 -0
- package/dist/transforms-BBEyouxz.d.ts +81 -0
- package/dist/typedoc.d.ts +38 -0
- package/dist/typedoc.js +1 -0
- package/package.json +86 -3
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { posix } from "node:path";
|
|
3
|
+
// ../core/src/prose.ts
|
|
4
|
+
const CONTAINER = /^(?:[ \t]*>)*[ \t]*(?:(?:[-*+]|\d{1,9}[.)])[ \t]+)?/;
|
|
5
|
+
const FENCE = /^(`{3,}|~{3,})(.*)$/;
|
|
6
|
+
const INLINE_CODE = /(?<!`)(`+)(?!`)[\s\S]*?(?<!`)\1(?!`)/g;
|
|
7
|
+
const MASK_OPEN = "";
|
|
8
|
+
const MASK_CLOSE = "";
|
|
9
|
+
const MASKED = new RegExp(`${MASK_OPEN}(\\d+)${MASK_CLOSE}`, "g");
|
|
10
|
+
/**
|
|
11
|
+
* Applies `fn` to the prose of a Markdown document, line by line. Fenced code blocks are skipped,
|
|
12
|
+
* and inline code spans are masked with opaque tokens. That way a transform can still match
|
|
13
|
+
* across them (e.g. a link whose text is `` `Money` ``) but can't change what's inside.
|
|
14
|
+
*/
|
|
15
|
+
function mapProse(markdown, fn) {
|
|
16
|
+
let fence = null;
|
|
17
|
+
return markdown.split("\n").map((line) => {
|
|
18
|
+
const match = FENCE.exec(line.replace(CONTAINER, ""));
|
|
19
|
+
const opener = match?.[1];
|
|
20
|
+
if (fence !== null) {
|
|
21
|
+
if (opener !== void 0 && opener[0] === fence[0] && opener.length >= fence.length && (match?.[2] ?? "").trim() === "") fence = null;
|
|
22
|
+
return line;
|
|
23
|
+
}
|
|
24
|
+
if (opener && !(opener[0] === "`" && (match?.[2] ?? "").includes("`"))) {
|
|
25
|
+
fence = opener;
|
|
26
|
+
return line;
|
|
27
|
+
}
|
|
28
|
+
const spans = [];
|
|
29
|
+
return fn(line.replace(INLINE_CODE, (span) => {
|
|
30
|
+
spans.push(span);
|
|
31
|
+
return `${MASK_OPEN}${spans.length - 1}${MASK_CLOSE}`;
|
|
32
|
+
})).replace(MASKED, (_, i) => spans[Number(i)] ?? "");
|
|
33
|
+
}).join("\n");
|
|
34
|
+
}
|
|
35
|
+
// ../core/src/links.ts
|
|
36
|
+
const LINK = /\]\((?<target>[^)\s#]+\.md)(?<anchor>#[^)\s]*)?(?<title>\s+"[^"]*")?\)/g;
|
|
37
|
+
const SCHEME = /^[a-z][a-z0-9+.-]*:/i;
|
|
38
|
+
/**
|
|
39
|
+
* Resolves a relative link `target` written in `fromPath` to an edition-relative path.
|
|
40
|
+
* Returns `null` for absolute URLs, root-relative paths and paths escaping the edition.
|
|
41
|
+
*/
|
|
42
|
+
function resolveLinkPath(fromPath, target) {
|
|
43
|
+
if (SCHEME.test(target) || target.startsWith("/")) return null;
|
|
44
|
+
let decoded = target;
|
|
45
|
+
try {
|
|
46
|
+
decoded = decodeURI(target);
|
|
47
|
+
} catch {}
|
|
48
|
+
const resolved = posix.normalize(posix.join(posix.dirname(fromPath), decoded));
|
|
49
|
+
return resolved.startsWith("../") || resolved === ".." ? null : resolved;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Rewrites relative `.md` links (`[x](./money.md#anchor)`) to target URLs, dropping the fragment
|
|
53
|
+
* unless `keepFragments` is set. Links in code are left untouched, as are links that `urlFor`
|
|
54
|
+
* can't resolve (they are reported in `unresolved`).
|
|
55
|
+
*/
|
|
56
|
+
function rewriteLinks(markdown, fromPath, urlFor, options = {}) {
|
|
57
|
+
const unresolved = /* @__PURE__ */ new Set();
|
|
58
|
+
return {
|
|
59
|
+
content: mapProse(markdown, (line) => line.replace(LINK, (match, target, anchor = "", title = "") => {
|
|
60
|
+
const path = resolveLinkPath(fromPath, target);
|
|
61
|
+
if (path === null) return match;
|
|
62
|
+
const url = urlFor(path);
|
|
63
|
+
if (url === void 0) {
|
|
64
|
+
unresolved.add(path);
|
|
65
|
+
return match;
|
|
66
|
+
}
|
|
67
|
+
return `](${url}${options.keepFragments ? anchor : ""}${title})`;
|
|
68
|
+
})),
|
|
69
|
+
unresolved: [...unresolved].sort()
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
// ../core/src/assets.ts
|
|
73
|
+
const TEXT = String.raw`(?:[^\[\]\\\n]|\\.|\[(?:[^\[\]\\\n]|\\.)*\])*`;
|
|
74
|
+
const DEST = String.raw`<[^>\n]*>|(?:[^()\s\\]|\\.|\((?:[^()\s\\]|\\.)*\))+`;
|
|
75
|
+
const TITLE = String.raw`[ \t]+(?:"[^"]*"|'[^']*')`;
|
|
76
|
+
const IMAGE_OR_LINK = new RegExp(String.raw`(?<bang>!?)\[(?<text>${TEXT})\]\([ \t]*(?<dest>${DEST})(?<title>${TITLE})?[ \t]*\)`, "g");
|
|
77
|
+
const IMG_TAG = /<img\b[^>]*>/gi;
|
|
78
|
+
const IMG_ATTR = /(?<pre>\s(?<name>src|srcset)\s*=\s*)(?:"(?<dq>[^"]*)"|'(?<sq>[^']*)'|(?<uq>[^\s"'=<>`]+))/gi;
|
|
79
|
+
const CONTENT_TYPES = {
|
|
80
|
+
".png": "image/png",
|
|
81
|
+
".jpg": "image/jpeg",
|
|
82
|
+
".jpeg": "image/jpeg",
|
|
83
|
+
".gif": "image/gif",
|
|
84
|
+
".webp": "image/webp",
|
|
85
|
+
".avif": "image/avif",
|
|
86
|
+
".svg": "image/svg+xml",
|
|
87
|
+
".bmp": "image/bmp",
|
|
88
|
+
".ico": "image/x-icon",
|
|
89
|
+
".pdf": "application/pdf",
|
|
90
|
+
".txt": "text/plain",
|
|
91
|
+
".csv": "text/csv",
|
|
92
|
+
".json": "application/json",
|
|
93
|
+
".zip": "application/zip",
|
|
94
|
+
".mp4": "video/mp4",
|
|
95
|
+
".webm": "video/webm"
|
|
96
|
+
};
|
|
97
|
+
/** MIME type from the file extension; `application/octet-stream` when unknown. */
|
|
98
|
+
function contentTypeFor(path) {
|
|
99
|
+
return CONTENT_TYPES[posix.extname(path).toLowerCase()] ?? "application/octet-stream";
|
|
100
|
+
}
|
|
101
|
+
/** Short, stable hash of an asset's bytes. */
|
|
102
|
+
function assetHash(data) {
|
|
103
|
+
return createHash("sha256").update(data).digest("hex").slice(0, 16);
|
|
104
|
+
}
|
|
105
|
+
/** URL used in dry runs for an asset that isn't uploaded yet (differs per content). */
|
|
106
|
+
const pendingAssetUrl = (asset) => `linopress://pending-asset/${asset.hash}/${posix.basename(asset.path)}`;
|
|
107
|
+
/** Edition-relative path of a destination, or `null` for URLs and paths outside the edition. */
|
|
108
|
+
function assetPath(fromPath, dest) {
|
|
109
|
+
const target = (dest.startsWith("<") ? dest.slice(1, -1) : dest.replace(/\\(.)/g, "$1")).replace(/[?#].*$/, "");
|
|
110
|
+
if (!target) return null;
|
|
111
|
+
return resolveLinkPath(fromPath, target);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* A plain link is an asset when it points at a local file that isn't a page: it has an extension
|
|
115
|
+
* other than `.md`. (`.md` links are the doc links `rewriteLinks` handles.)
|
|
116
|
+
*/
|
|
117
|
+
function isFileLink(path) {
|
|
118
|
+
const ext = posix.extname(path).toLowerCase();
|
|
119
|
+
return ext !== "" && ext !== ".md";
|
|
120
|
+
}
|
|
121
|
+
/** `srcset` is `url [descriptor], url [descriptor], …`; maps each url. */
|
|
122
|
+
function mapSrcset(value, fn) {
|
|
123
|
+
return value.split(",").map((candidate) => candidate.replace(/^(\s*)(\S+)/, (_m, space, url) => space + fn(url))).join(",");
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Visits every local file a line of prose embeds or links to: Markdown images, `<img src/srcset>`,
|
|
127
|
+
* and plain links to non-page files (`[spec](_media/spec.pdf)`). `visit` gets the edition-relative
|
|
128
|
+
* path and returns the URL to put there, or `undefined` to leave it as written.
|
|
129
|
+
*/
|
|
130
|
+
function mapAssetRefs(line, fromPath, visit) {
|
|
131
|
+
return line.replace(IMAGE_OR_LINK, (match, bang, text, dest, title = "") => {
|
|
132
|
+
const inner = bang ? text : mapAssetRefs(text, fromPath, visit);
|
|
133
|
+
const path = assetPath(fromPath, dest);
|
|
134
|
+
const url = path === null || !bang && !isFileLink(path) ? void 0 : visit(path);
|
|
135
|
+
if (url === void 0 && inner === text) return match;
|
|
136
|
+
return `${bang}[${inner}](${url ?? dest}${title})`;
|
|
137
|
+
}).replace(IMG_TAG, (tag) => tag.replace(IMG_ATTR, (match, pre, name, dq, sq, uq) => {
|
|
138
|
+
const value = dq ?? sq ?? uq ?? "";
|
|
139
|
+
const one = (url) => {
|
|
140
|
+
const path = assetPath(fromPath, url.replaceAll("&", "&"));
|
|
141
|
+
return path !== null && visit(path) || url;
|
|
142
|
+
};
|
|
143
|
+
const next = name.toLowerCase() === "srcset" ? mapSrcset(value, one) : one(value);
|
|
144
|
+
if (next === value) return match;
|
|
145
|
+
const quote = sq !== void 0 ? "'" : "\"";
|
|
146
|
+
return `${pre}${quote}${next}${quote}`;
|
|
147
|
+
}));
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Edition-relative paths of the local files `markdown` embeds or links to (``,
|
|
151
|
+
* `<img src="_media/flow.png">`, `[spec](_media/spec.pdf)`), in order of first use. Code, absolute
|
|
152
|
+
* URLs, paths outside the edition and links to pages are ignored.
|
|
153
|
+
*/
|
|
154
|
+
function findImages(markdown, fromPath) {
|
|
155
|
+
const found = /* @__PURE__ */ new Set();
|
|
156
|
+
mapProse(markdown, (line) => mapAssetRefs(line, fromPath, (path) => {
|
|
157
|
+
found.add(path);
|
|
158
|
+
}));
|
|
159
|
+
return [...found];
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Points local images (and file links) at their uploaded URLs. Code is left untouched, as are
|
|
163
|
+
* files `urlFor` can't resolve (reported in `missing`).
|
|
164
|
+
*/
|
|
165
|
+
function rewriteImages(markdown, fromPath, urlFor) {
|
|
166
|
+
const missing = /* @__PURE__ */ new Set();
|
|
167
|
+
return {
|
|
168
|
+
content: mapProse(markdown, (line) => mapAssetRefs(line, fromPath, (path) => {
|
|
169
|
+
const url = urlFor(path);
|
|
170
|
+
if (url === void 0) missing.add(path);
|
|
171
|
+
return url;
|
|
172
|
+
})),
|
|
173
|
+
missing: [...missing].sort()
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
/** Finds the images `pages` (or doc nodes) embed and reads them through `read`. */
|
|
177
|
+
async function collectAssets(pages, read) {
|
|
178
|
+
const paths = new Set(pages.flatMap((p) => findImages(p.content, p.path)));
|
|
179
|
+
const assets = /* @__PURE__ */ new Map();
|
|
180
|
+
const missing = [];
|
|
181
|
+
for (const path of [...paths].sort()) {
|
|
182
|
+
const data = await read(path);
|
|
183
|
+
if (data === void 0) {
|
|
184
|
+
missing.push(path);
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
assets.set(path, {
|
|
188
|
+
path,
|
|
189
|
+
hash: assetHash(data),
|
|
190
|
+
contentType: contentTypeFor(path),
|
|
191
|
+
data
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
return {
|
|
195
|
+
assets,
|
|
196
|
+
missing
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* URL of an already-uploaded copy of `asset`: its own entry when the bytes are unchanged, otherwise
|
|
201
|
+
* any entry with the same bytes (a moved or duplicated image).
|
|
202
|
+
*/
|
|
203
|
+
function uploadedUrl(entries, asset) {
|
|
204
|
+
const own = entries[asset.path];
|
|
205
|
+
if (own?.hash === asset.hash) return own.url;
|
|
206
|
+
return Object.values(entries).find((e) => e.hash === asset.hash)?.url;
|
|
207
|
+
}
|
|
208
|
+
// ../core/src/hash.ts
|
|
209
|
+
/**
|
|
210
|
+
* Normalizes Markdown so that cosmetic differences (CRLF checkouts, trailing blank lines)
|
|
211
|
+
* don't count as content changes. Leading indentation and trailing spaces inside the document
|
|
212
|
+
* are kept: they can be meaningful (indented code, two-space hard line breaks).
|
|
213
|
+
*/
|
|
214
|
+
function normalizeMarkdown(markdown) {
|
|
215
|
+
return `${markdown.replace(/\r\n?/g, "\n").replace(/\s+$/, "")}\n`;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Short, stable hash of a document body. Title and position are tracked separately in the
|
|
219
|
+
* manifest, so a rename shows up as a title change, not a content change.
|
|
220
|
+
*/
|
|
221
|
+
function contentHash(body) {
|
|
222
|
+
return createHash("sha256").update(normalizeMarkdown(body)).digest("hex").slice(0, 16);
|
|
223
|
+
}
|
|
224
|
+
// ../core/src/transforms.ts
|
|
225
|
+
/**
|
|
226
|
+
* Removes empty HTML anchors (`<a id="property-apikey"></a>`) and the space after them.
|
|
227
|
+
* TypeDoc puts them in table cells; Linear shows them as literal text.
|
|
228
|
+
*/
|
|
229
|
+
const stripHtmlAnchors = (markdown) => mapProse(markdown, (line) => line.replace(/<a\s+(?:id|name)="[^"]*"\s*><\/a>[ \t]*/gi, ""));
|
|
230
|
+
/**
|
|
231
|
+
* Turns in-page links `[text](#fragment)` into their plain text (inline code in the text is kept).
|
|
232
|
+
* Linear heading ids are UUIDs, so `#slug` targets don't exist and the links would be dead.
|
|
233
|
+
*/
|
|
234
|
+
const unlinkInPageLinks = (markdown) => mapProse(markdown, (line) => line.replace(/(?<!!)\[([^\]]*)\]\(#[^)\s]*\)/g, "$1"));
|
|
235
|
+
/**
|
|
236
|
+
* The clean-up `plan`, `apply` and `renderDoc` use when given no `transforms`: the Linear one, since
|
|
237
|
+
* those sync to a `DocumentTarget`. Pass `[]` for none.
|
|
238
|
+
* @deprecated Import `linearTransforms` from `@linopress/linear` (`linopress-cli/linear`).
|
|
239
|
+
*/
|
|
240
|
+
const defaultTransforms = [stripHtmlAnchors, unlinkInPageLinks];
|
|
241
|
+
/** Runs `transforms` left to right. */
|
|
242
|
+
function applyTransforms(markdown, transforms) {
|
|
243
|
+
return transforms.reduce((md, transform) => transform(md), markdown);
|
|
244
|
+
}
|
|
245
|
+
// ../core/src/render.ts
|
|
246
|
+
/**
|
|
247
|
+
* Produces the final body for a doc: transforms applied, links and images rewritten, footer appended.
|
|
248
|
+
*
|
|
249
|
+
* The hash is computed here, over exactly the body we send. Never hash or diff the content a target
|
|
250
|
+
* returns: Linear's `document.content` is a lossy re-serialization (it unescapes `\|` in table
|
|
251
|
+
* cells and shortens `| ------ |` separators), so it would never match. Image URLs are part of the
|
|
252
|
+
* body, and a changed image gets a new URL, so it counts as a content change.
|
|
253
|
+
*/
|
|
254
|
+
function renderDoc(node, options) {
|
|
255
|
+
const links = rewriteLinks(applyTransforms(node.content, options.transforms ?? defaultTransforms), node.path, options.urlFor);
|
|
256
|
+
const images = options.assetUrlFor ? rewriteImages(links.content, node.path, options.assetUrlFor) : {
|
|
257
|
+
content: links.content,
|
|
258
|
+
missing: []
|
|
259
|
+
};
|
|
260
|
+
const body = images.content;
|
|
261
|
+
const footer = options.footer?.(node) ?? "";
|
|
262
|
+
return {
|
|
263
|
+
content: footer ? `${body.trimEnd()}\n\n${footer}` : body,
|
|
264
|
+
hash: contentHash(body),
|
|
265
|
+
unresolvedLinks: links.unresolved,
|
|
266
|
+
missingImages: images.missing
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
// ../core/src/plan.ts
|
|
270
|
+
/** URL used in dry runs for docs that don't exist yet. */
|
|
271
|
+
const pendingUrl = (path) => `linopress://pending/${path}`;
|
|
272
|
+
/** Depth of a node in its tree; used to order creates (parents first) and archives (children first). */
|
|
273
|
+
function depthOf(path, parentOf) {
|
|
274
|
+
let depth = 0;
|
|
275
|
+
const seen = /* @__PURE__ */ new Set();
|
|
276
|
+
for (let p = parentOf(path); p !== null && !seen.has(p); p = parentOf(p)) {
|
|
277
|
+
seen.add(p);
|
|
278
|
+
depth++;
|
|
279
|
+
}
|
|
280
|
+
return depth;
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Diffs the desired docs against the manifest. Pure: no I/O, safe for dry runs.
|
|
284
|
+
*
|
|
285
|
+
* - not in the manifest → **create**
|
|
286
|
+
* - content or title hash differs → **update** (may also move)
|
|
287
|
+
* - only parent / sort order differs → **move**
|
|
288
|
+
* - in the manifest but no longer produced → **archive**
|
|
289
|
+
*/
|
|
290
|
+
function plan(nodes, entries, options = {}) {
|
|
291
|
+
const byPath = new Map(nodes.map((n) => [n.path, n]));
|
|
292
|
+
const urlFor = (path) => byPath.has(path) ? entries[path]?.url ?? pendingUrl(path) : void 0;
|
|
293
|
+
const assets = options.assets;
|
|
294
|
+
const assetUrlFor = assets ? (path) => {
|
|
295
|
+
const asset = assets.files.get(path);
|
|
296
|
+
return asset && (uploadedUrl(assets.entries, asset) ?? pendingAssetUrl(asset));
|
|
297
|
+
} : void 0;
|
|
298
|
+
const creates = [];
|
|
299
|
+
const changes = [];
|
|
300
|
+
const unchanged = [];
|
|
301
|
+
const unresolvedLinks = {};
|
|
302
|
+
const missingImages = {};
|
|
303
|
+
for (const node of nodes) {
|
|
304
|
+
const rendered = renderDoc(node, {
|
|
305
|
+
urlFor,
|
|
306
|
+
...assetUrlFor && { assetUrlFor },
|
|
307
|
+
...options.footer && { footer: options.footer },
|
|
308
|
+
...options.transforms && { transforms: options.transforms }
|
|
309
|
+
});
|
|
310
|
+
if (rendered.unresolvedLinks.length > 0) unresolvedLinks[node.path] = rendered.unresolvedLinks;
|
|
311
|
+
if (rendered.missingImages.length > 0) missingImages[node.path] = rendered.missingImages;
|
|
312
|
+
const entry = entries[node.path];
|
|
313
|
+
if (!entry) {
|
|
314
|
+
creates.push({
|
|
315
|
+
kind: "create",
|
|
316
|
+
path: node.path,
|
|
317
|
+
title: node.title,
|
|
318
|
+
parentPath: node.parentPath,
|
|
319
|
+
sortOrder: node.sortOrder
|
|
320
|
+
});
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
const diff = [];
|
|
324
|
+
if (entry.hash !== rendered.hash) diff.push("content");
|
|
325
|
+
if (entry.title !== node.title) diff.push("title");
|
|
326
|
+
if (entry.parentPath !== node.parentPath) diff.push("parent");
|
|
327
|
+
if (entry.sortOrder !== node.sortOrder) diff.push("order");
|
|
328
|
+
const base = {
|
|
329
|
+
path: node.path,
|
|
330
|
+
title: node.title,
|
|
331
|
+
id: entry.id,
|
|
332
|
+
url: entry.url
|
|
333
|
+
};
|
|
334
|
+
if (diff.length === 0) {
|
|
335
|
+
unchanged.push({
|
|
336
|
+
kind: "unchanged",
|
|
337
|
+
...base
|
|
338
|
+
});
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
changes.push({
|
|
342
|
+
kind: diff.includes("content") || diff.includes("title") ? "update" : "move",
|
|
343
|
+
...base,
|
|
344
|
+
changes: diff,
|
|
345
|
+
from: {
|
|
346
|
+
title: entry.title,
|
|
347
|
+
parentPath: entry.parentPath,
|
|
348
|
+
sortOrder: entry.sortOrder
|
|
349
|
+
},
|
|
350
|
+
parentPath: node.parentPath,
|
|
351
|
+
sortOrder: node.sortOrder
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
const nodeParent = (p) => byPath.get(p)?.parentPath ?? null;
|
|
355
|
+
const entryParent = (p) => entries[p]?.parentPath ?? null;
|
|
356
|
+
creates.sort((a, b) => depthOf(a.path, nodeParent) - depthOf(b.path, nodeParent));
|
|
357
|
+
const archives = Object.entries(entries).filter(([path]) => !byPath.has(path)).map(([path, e]) => ({
|
|
358
|
+
kind: "archive",
|
|
359
|
+
path,
|
|
360
|
+
title: e.title,
|
|
361
|
+
id: e.id,
|
|
362
|
+
url: e.url
|
|
363
|
+
})).sort((a, b) => depthOf(b.path, entryParent) - depthOf(a.path, entryParent));
|
|
364
|
+
const actions = [
|
|
365
|
+
...creates,
|
|
366
|
+
...changes,
|
|
367
|
+
...archives,
|
|
368
|
+
...unchanged
|
|
369
|
+
];
|
|
370
|
+
const counts = {
|
|
371
|
+
create: 0,
|
|
372
|
+
update: 0,
|
|
373
|
+
move: 0,
|
|
374
|
+
archive: 0,
|
|
375
|
+
unchanged: 0
|
|
376
|
+
};
|
|
377
|
+
for (const a of actions) counts[a.kind]++;
|
|
378
|
+
return {
|
|
379
|
+
actions,
|
|
380
|
+
counts,
|
|
381
|
+
unresolvedLinks,
|
|
382
|
+
missingImages,
|
|
383
|
+
uploads: pendingUploads(assets)
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
/** Assets with no uploaded copy, one per distinct content hash. */
|
|
387
|
+
function pendingUploads(assets) {
|
|
388
|
+
if (!assets) return [];
|
|
389
|
+
const seen = /* @__PURE__ */ new Set();
|
|
390
|
+
const out = [];
|
|
391
|
+
for (const asset of [...assets.files.values()].sort((a, b) => a.path < b.path ? -1 : 1)) {
|
|
392
|
+
if (seen.has(asset.hash) || uploadedUrl(assets.entries, asset) !== void 0) continue;
|
|
393
|
+
seen.add(asset.hash);
|
|
394
|
+
out.push(asset.path);
|
|
395
|
+
}
|
|
396
|
+
return out;
|
|
397
|
+
}
|
|
398
|
+
/** True when applying the plan would change nothing. */
|
|
399
|
+
function isEmptyPlan(p) {
|
|
400
|
+
return p.uploads.length === 0 && p.actions.every((a) => a.kind === "unchanged");
|
|
401
|
+
}
|
|
402
|
+
// ../core/src/target.ts
|
|
403
|
+
/**
|
|
404
|
+
* Thrown by a target when a doc is gone: deleted, or archived outside Linopress.
|
|
405
|
+
* `apply` treats it as recoverable (re-creates the doc, or skips an archive that already happened).
|
|
406
|
+
*/
|
|
407
|
+
var DocNotFoundError = class extends Error {
|
|
408
|
+
docId;
|
|
409
|
+
constructor(docId, message = `Doc ${docId} not found or archived`) {
|
|
410
|
+
super(message);
|
|
411
|
+
this.name = "DocNotFoundError";
|
|
412
|
+
this.docId = docId;
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
// ../core/src/apply.ts
|
|
416
|
+
/** Body written in pass 1, replaced in pass 2 once every doc has a URL. */
|
|
417
|
+
const PLACEHOLDER_CONTENT = "_Generating…_";
|
|
418
|
+
/**
|
|
419
|
+
* Syncs `nodes` into `target`.
|
|
420
|
+
*
|
|
421
|
+
* 0. **Upload** images whose bytes weren't uploaded before, so a failed upload changes no doc.
|
|
422
|
+
* 1. **Ensure:** create every missing doc (parents first) with a placeholder body, so every
|
|
423
|
+
* page has a URL.
|
|
424
|
+
* 2. **Write:** render every doc with links rewritten to those URLs; update the ones whose
|
|
425
|
+
* hash, title or position changed.
|
|
426
|
+
* 3. **Archive** docs that are no longer produced (children first). Never hard-deletes.
|
|
427
|
+
*/
|
|
428
|
+
async function apply(options) {
|
|
429
|
+
const { nodes, target } = options;
|
|
430
|
+
const entries = { ...options.entries };
|
|
431
|
+
const events = [];
|
|
432
|
+
/** Records `event` (if any) and, when `changed`, reports the entries. */
|
|
433
|
+
const emit = async (event, changed) => {
|
|
434
|
+
if (event) {
|
|
435
|
+
events.push(event);
|
|
436
|
+
options.onEvent?.(event);
|
|
437
|
+
}
|
|
438
|
+
if (changed) await options.onEntriesChange?.({ ...entries });
|
|
439
|
+
};
|
|
440
|
+
const idOf = (path) => {
|
|
441
|
+
if (path === null) return null;
|
|
442
|
+
const entry = entries[path];
|
|
443
|
+
if (!entry) throw new Error(`Parent "${path}" has no document yet`);
|
|
444
|
+
return entry.id;
|
|
445
|
+
};
|
|
446
|
+
const initial = plan(nodes, entries);
|
|
447
|
+
let assetEntries = { ...options.assets?.entries };
|
|
448
|
+
if (options.assets) {
|
|
449
|
+
const { files } = options.assets;
|
|
450
|
+
for (const path of pendingUploads(options.assets)) {
|
|
451
|
+
const asset = files.get(path);
|
|
452
|
+
if (!asset) continue;
|
|
453
|
+
if (!target.uploadAsset) throw new Error(`Target can't upload assets (needed for "${path}")`);
|
|
454
|
+
const url = await target.uploadAsset(asset);
|
|
455
|
+
assetEntries[path] = {
|
|
456
|
+
hash: asset.hash,
|
|
457
|
+
url
|
|
458
|
+
};
|
|
459
|
+
await emit({
|
|
460
|
+
kind: "uploaded",
|
|
461
|
+
path,
|
|
462
|
+
url
|
|
463
|
+
}, false);
|
|
464
|
+
await options.onAssetEntriesChange?.({ ...assetEntries });
|
|
465
|
+
}
|
|
466
|
+
const resolved = {};
|
|
467
|
+
for (const asset of files.values()) {
|
|
468
|
+
const url = uploadedUrl(assetEntries, asset);
|
|
469
|
+
if (url !== void 0) resolved[asset.path] = {
|
|
470
|
+
hash: asset.hash,
|
|
471
|
+
url
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
assetEntries = resolved;
|
|
475
|
+
}
|
|
476
|
+
const assetUrlFor = options.assets ? (path) => assetEntries[path]?.url : void 0;
|
|
477
|
+
const created = /* @__PURE__ */ new Set();
|
|
478
|
+
for (const action of initial.actions) {
|
|
479
|
+
if (action.kind !== "create") continue;
|
|
480
|
+
const doc = await target.create({
|
|
481
|
+
title: action.title,
|
|
482
|
+
content: PLACEHOLDER_CONTENT,
|
|
483
|
+
parentId: idOf(action.parentPath),
|
|
484
|
+
sortOrder: action.sortOrder
|
|
485
|
+
});
|
|
486
|
+
entries[action.path] = {
|
|
487
|
+
id: doc.id,
|
|
488
|
+
url: doc.url,
|
|
489
|
+
title: action.title,
|
|
490
|
+
parentPath: action.parentPath,
|
|
491
|
+
sortOrder: action.sortOrder,
|
|
492
|
+
hash: null
|
|
493
|
+
};
|
|
494
|
+
created.add(action.path);
|
|
495
|
+
await emit({
|
|
496
|
+
kind: "created",
|
|
497
|
+
path: action.path,
|
|
498
|
+
url: doc.url
|
|
499
|
+
}, true);
|
|
500
|
+
}
|
|
501
|
+
const produced = new Set(nodes.map((n) => n.path));
|
|
502
|
+
const urlFor = (path) => produced.has(path) ? entries[path]?.url : void 0;
|
|
503
|
+
const recreated = /* @__PURE__ */ new Set();
|
|
504
|
+
for (const node of nodes) {
|
|
505
|
+
const entry = entries[node.path];
|
|
506
|
+
if (!entry) throw new Error(`Doc for "${node.path}" was not created`);
|
|
507
|
+
const rendered = renderDoc(node, {
|
|
508
|
+
urlFor,
|
|
509
|
+
...assetUrlFor && { assetUrlFor },
|
|
510
|
+
...options.footer && { footer: options.footer },
|
|
511
|
+
...options.transforms && { transforms: options.transforms }
|
|
512
|
+
});
|
|
513
|
+
const contentChanged = entry.hash !== rendered.hash;
|
|
514
|
+
const titleChanged = entry.title !== node.title;
|
|
515
|
+
const moved = entry.parentPath !== node.parentPath || entry.sortOrder !== node.sortOrder || node.parentPath !== null && recreated.has(node.parentPath);
|
|
516
|
+
if (!contentChanged && !titleChanged && !moved) {
|
|
517
|
+
await emit({
|
|
518
|
+
kind: "unchanged",
|
|
519
|
+
path: node.path,
|
|
520
|
+
url: entry.url
|
|
521
|
+
}, false);
|
|
522
|
+
continue;
|
|
523
|
+
}
|
|
524
|
+
const input = {
|
|
525
|
+
...contentChanged && { content: rendered.content },
|
|
526
|
+
...titleChanged && { title: node.title },
|
|
527
|
+
...moved && { position: {
|
|
528
|
+
parentId: idOf(node.parentPath),
|
|
529
|
+
sortOrder: node.sortOrder
|
|
530
|
+
} }
|
|
531
|
+
};
|
|
532
|
+
try {
|
|
533
|
+
await target.update(entry.id, input);
|
|
534
|
+
} catch (error) {
|
|
535
|
+
if (!(error instanceof DocNotFoundError)) throw error;
|
|
536
|
+
const doc = await target.create({
|
|
537
|
+
title: node.title,
|
|
538
|
+
content: rendered.content,
|
|
539
|
+
parentId: idOf(node.parentPath),
|
|
540
|
+
sortOrder: node.sortOrder
|
|
541
|
+
});
|
|
542
|
+
entries[node.path] = {
|
|
543
|
+
id: doc.id,
|
|
544
|
+
url: doc.url,
|
|
545
|
+
title: node.title,
|
|
546
|
+
parentPath: node.parentPath,
|
|
547
|
+
sortOrder: node.sortOrder,
|
|
548
|
+
hash: rendered.hash
|
|
549
|
+
};
|
|
550
|
+
recreated.add(node.path);
|
|
551
|
+
await emit({
|
|
552
|
+
kind: "created",
|
|
553
|
+
path: node.path,
|
|
554
|
+
url: doc.url
|
|
555
|
+
}, true);
|
|
556
|
+
continue;
|
|
557
|
+
}
|
|
558
|
+
entries[node.path] = {
|
|
559
|
+
...entry,
|
|
560
|
+
title: node.title,
|
|
561
|
+
parentPath: node.parentPath,
|
|
562
|
+
sortOrder: node.sortOrder,
|
|
563
|
+
hash: rendered.hash
|
|
564
|
+
};
|
|
565
|
+
const kind = contentChanged || titleChanged ? "updated" : "moved";
|
|
566
|
+
await emit(created.has(node.path) ? null : {
|
|
567
|
+
kind,
|
|
568
|
+
path: node.path,
|
|
569
|
+
url: entry.url
|
|
570
|
+
}, true);
|
|
571
|
+
}
|
|
572
|
+
for (const action of initial.actions) {
|
|
573
|
+
if (action.kind !== "archive") continue;
|
|
574
|
+
try {
|
|
575
|
+
await target.archive(action.id);
|
|
576
|
+
} catch (error) {
|
|
577
|
+
if (!(error instanceof DocNotFoundError)) throw error;
|
|
578
|
+
}
|
|
579
|
+
delete entries[action.path];
|
|
580
|
+
await emit({
|
|
581
|
+
kind: "archived",
|
|
582
|
+
path: action.path,
|
|
583
|
+
url: action.url
|
|
584
|
+
}, true);
|
|
585
|
+
}
|
|
586
|
+
if (options.assets) await options.onAssetEntriesChange?.({ ...assetEntries });
|
|
587
|
+
return {
|
|
588
|
+
entries,
|
|
589
|
+
events,
|
|
590
|
+
assets: options.assets ? assetEntries : {}
|
|
591
|
+
};
|
|
592
|
+
}
|
|
593
|
+
export { rewriteLinks as C, resolveLinkPath as S, contentTypeFor as _, pendingUploads as a, rewriteImages as b, renderDoc as c, stripHtmlAnchors as d, unlinkInPageLinks as f, collectAssets as g, assetHash as h, isEmptyPlan as i, applyTransforms as l, normalizeMarkdown as m, apply as n, pendingUrl as o, contentHash as p, DocNotFoundError as r, plan as s, PLACEHOLDER_CONTENT as t, defaultTransforms as u, findImages as v, mapProse as w, uploadedUrl as x, pendingAssetUrl as y };
|