@open-document/core 0.15.1 → 0.17.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/dist/{build-CBMroMqd.js → build-BzNZnIhF.js} +1 -1
- package/dist/{check-C2kIZr3f.js → check-C7kxlAFW.js} +11 -4
- package/dist/cli/bin.js +24 -11
- package/dist/{context-DGp1jwN5.js → context-CFPnmNCR.js} +1 -1
- package/dist/{dev-DmzGncKf.js → dev-BUo7OESU.js} +1 -1
- package/dist/{diff-CMI_PVzz.js → diff-B950T0O6.js} +2 -2
- package/dist/{export-BQMUBoBB.js → export-CHN1CiGv.js} +3 -3
- package/dist/{flow-CS5apebG.d.ts → flow-DlV3t5ML.d.ts} +24 -4
- package/dist/{import-CiSRqZyo.js → import-CLSnIxvN.js} +11 -7
- package/dist/index.d.ts +26 -2
- package/dist/index.js +55 -1
- package/dist/ops/index.d.ts +200 -4
- package/dist/ops/index.js +3 -3
- package/dist/{ops-DQbZmYRF.js → ops-BgbM5Irm.js} +364 -9
- package/dist/{preview-CERhZ_FX.js → preview-BVwVqS-I.js} +1 -1
- package/dist/proofread-BpR_3GZz.js +35 -0
- package/dist/{session-DdaKKd7_.js → session-DmbXf3u7.js} +5179 -106
- package/dist/{templates-CBeC-v7e.js → templates-BsWxs7oo.js} +3 -3
- package/dist/translations-CW5OVM0L.js +42 -0
- package/dist/vite/index.js +1 -1
- package/package.json +1 -1
- package/skills/doc-authoring/references/pagination.md +1 -1
- package/skills/fill-form/SKILL.md +42 -0
- package/skills/fit-pages/SKILL.md +48 -0
- package/skills/import-document/SKILL.md +58 -0
- package/skills/proofread/SKILL.md +44 -0
- package/skills/refresh-report/SKILL.md +48 -0
- package/skills/sync-translation/SKILL.md +55 -0
- package/src/agent/prompt.ts +1 -1
- package/src/agent/types.ts +9 -2
- package/src/app/components/agent/agent-panel.tsx +3 -2
- package/src/app/components/checkbox.tsx +68 -0
- package/src/app/components/doc-sidebar.tsx +5 -3
- package/src/app/components/flow-page.tsx +7 -2
- package/src/app/components/host-bridge.tsx +49 -0
- package/src/app/components/panel/edit-save-card.tsx +23 -1
- package/src/app/lib/agent-bridge.ts +37 -4
- package/src/app/lib/diagnostics.ts +46 -5
- package/src/app/lib/export-docx.ts +5 -2
- package/src/app/lib/export-dom.ts +13 -0
- package/src/app/lib/export-html.ts +6 -1
- package/src/app/lib/export-image.ts +9 -4
- package/src/app/lib/export-pdf.ts +17 -4
- package/src/app/lib/flow-measure.ts +9 -5
- package/src/app/lib/flow.ts +3 -2
- package/src/app/lib/host.ts +71 -0
- package/src/app/lib/i18n-ja/browser.ts +1 -0
- package/src/app/lib/i18n-ko/browser.ts +1 -0
- package/src/app/lib/i18n-zh-cn/browser.ts +1 -0
- package/src/app/lib/i18n-zh-tw/browser.ts +1 -0
- package/src/app/lib/page-budget.ts +81 -0
- package/src/app/lib/proofread-script.ts +61 -0
- package/src/app/lib/proofread.ts +399 -0
- package/src/app/lib/recent-docs.ts +27 -0
- package/src/app/lib/sdk.ts +44 -2
- package/src/app/lib/text-runs.ts +83 -0
- package/src/app/lib/use-doc-pages.ts +10 -3
- package/src/app/main.tsx +2 -0
- package/src/app/routes/doc-preview.tsx +1 -1
- package/src/app/routes/doc.tsx +26 -2
- package/src/app/routes/home.tsx +39 -5
package/dist/ops/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { i as ORIENTATIONS, o as PAGE_SIZE_NAMES, r as EXPORT_FORMATS } from "../sdk-CqNZzbkz.js";
|
|
2
|
-
import { A as
|
|
3
|
-
import {
|
|
4
|
-
export { EXPORT_FORMATS, ORIENTATIONS, OpsError, PAGE_SIZE_NAMES, addComment, assertExpectedSource, checkLayout, closeRenderSession, createDocument, createFolder, createFromTemplate, deleteAsset, deleteDocument, diffDocument, docDir, duplicateDocument, exportDocument, fileDocument, findAssetUsages, hashSource, importMarkdown, listAssets, listDocIds, listDocuments, listFolders, listRevisions, listTemplates, listThemes, makeContext, readChanges, readDocument, readText, readTheme, renameDocument, renderDocPage, resolveEntry, revertChange, slugify, suggestDocId, withFileLock, writeAsset, writeDocument, writeEdits, writeText, writeTexts };
|
|
2
|
+
import { A as readChanges, B as listDocIds, C as exportDocument, D as importMarkdown, E as importDocx, F as OpsError, G as resolveEntry, H as readDocument, I as createDocument, J as makeContext, K as writeDocument, L as deleteDocument, M as assertExpectedSource, N as hashSource, O as slugify, P as withFileLock, R as docDir, S as closeRenderSession, T as renderDocPage, V as listDocuments, W as renameDocument, _ as listFolders, a as readText, b as writeAsset, c as writeTexts, d as suggestDocId, f as createFolder, g as listAssets, h as findAssetUsages, i as addComment, j as revertChange, k as listRevisions, l as createFromTemplate, m as fileDocument, o as writeEdits, p as deleteAsset, s as writeText, u as listTemplates, v as listThemes, w as proofreadDocument, x as checkLayout, y as readTheme, z as duplicateDocument } from "../session-DmbXf3u7.js";
|
|
3
|
+
import { a as outlineSource, c as diffDocument, i as outlineDocument, n as translationStatus, o as readDocumentLines, r as editDocument, s as searchDocument, t as markTranslated } from "../ops-BgbM5Irm.js";
|
|
4
|
+
export { EXPORT_FORMATS, ORIENTATIONS, OpsError, PAGE_SIZE_NAMES, addComment, assertExpectedSource, checkLayout, closeRenderSession, createDocument, createFolder, createFromTemplate, deleteAsset, deleteDocument, diffDocument, docDir, duplicateDocument, editDocument, exportDocument, fileDocument, findAssetUsages, hashSource, importDocx, importMarkdown, listAssets, listDocIds, listDocuments, listFolders, listRevisions, listTemplates, listThemes, makeContext, markTranslated, outlineDocument, outlineSource, proofreadDocument, readChanges, readDocument, readDocumentLines, readText, readTheme, renameDocument, renderDocPage, resolveEntry, revertChange, searchDocument, slugify, suggestDocId, translationStatus, withFileLock, writeAsset, writeDocument, writeEdits, writeText, writeTexts };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { B as listDocIds, F as OpsError, G as resolveEntry, M as assertExpectedSource, N as hashSource, P as withFileLock, U as readMetaField, n as createRenderSession, q as setMetaString, t as RenderUnavailableError } from "./session-DmbXf3u7.js";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import fs from "node:fs/promises";
|
|
4
4
|
import { execFile } from "node:child_process";
|
|
@@ -219,17 +219,17 @@ ${body}
|
|
|
219
219
|
}
|
|
220
220
|
//#endregion
|
|
221
221
|
//#region src/ops/diff.ts
|
|
222
|
-
const run = promisify(execFile);
|
|
222
|
+
const run$1 = promisify(execFile);
|
|
223
223
|
const MAX_BUFFER = 67108864;
|
|
224
|
-
async function git(cwd, args) {
|
|
225
|
-
const { stdout } = await run("git", args, {
|
|
224
|
+
async function git$1(cwd, args) {
|
|
225
|
+
const { stdout } = await run$1("git", args, {
|
|
226
226
|
cwd,
|
|
227
227
|
maxBuffer: MAX_BUFFER
|
|
228
228
|
});
|
|
229
229
|
return stdout;
|
|
230
230
|
}
|
|
231
231
|
async function gitBytes(cwd, args) {
|
|
232
|
-
const { stdout } = await run("git", args, {
|
|
232
|
+
const { stdout } = await run$1("git", args, {
|
|
233
233
|
cwd,
|
|
234
234
|
maxBuffer: MAX_BUFFER,
|
|
235
235
|
encoding: "buffer"
|
|
@@ -243,7 +243,7 @@ async function gitBytes(cwd, args) {
|
|
|
243
243
|
* the current version renders with.
|
|
244
244
|
*/
|
|
245
245
|
async function checkout(ctx, docId, commit) {
|
|
246
|
-
const prefix = (await git(ctx.userCwd, ["rev-parse", "--show-prefix"])).trim();
|
|
246
|
+
const prefix = (await git$1(ctx.userCwd, ["rev-parse", "--show-prefix"])).trim();
|
|
247
247
|
const dir = path.join(ctx.userCwd, ".open-doc-diff", `${commit.slice(0, 12)}-${docId}`);
|
|
248
248
|
await fs.rm(dir, {
|
|
249
249
|
recursive: true,
|
|
@@ -257,7 +257,7 @@ async function checkout(ctx, docId, commit) {
|
|
|
257
257
|
];
|
|
258
258
|
let exists = false;
|
|
259
259
|
for (const rel of wanted) {
|
|
260
|
-
const listed = await git(ctx.userCwd, [
|
|
260
|
+
const listed = await git$1(ctx.userCwd, [
|
|
261
261
|
"ls-tree",
|
|
262
262
|
"-r",
|
|
263
263
|
"--full-tree",
|
|
@@ -319,7 +319,7 @@ async function diffDocument(ctx, docId, opts = {}) {
|
|
|
319
319
|
const since = opts.since ?? "HEAD";
|
|
320
320
|
let commit;
|
|
321
321
|
try {
|
|
322
|
-
commit = (await git(ctx.userCwd, [
|
|
322
|
+
commit = (await git$1(ctx.userCwd, [
|
|
323
323
|
"rev-parse",
|
|
324
324
|
"--verify",
|
|
325
325
|
`${since}^{commit}`
|
|
@@ -398,4 +398,359 @@ async function diffDocument(ctx, docId, opts = {}) {
|
|
|
398
398
|
}
|
|
399
399
|
}
|
|
400
400
|
//#endregion
|
|
401
|
-
|
|
401
|
+
//#region src/ops/source.ts
|
|
402
|
+
/**
|
|
403
|
+
* Working on part of a document's source: its outline, a range of lines, the
|
|
404
|
+
* lines that contain some text, and an exact find-and-replace. A long document
|
|
405
|
+
* (hundreds of lines) need not be read whole to change a sentence or a colour,
|
|
406
|
+
* which matters to an agent: whatever it reads is sent again on every later
|
|
407
|
+
* model call.
|
|
408
|
+
*/
|
|
409
|
+
async function sourceOf(ctx, docId) {
|
|
410
|
+
const entry = resolveEntry(ctx, docId);
|
|
411
|
+
if (!entry) throw new OpsError(404, `document not found: ${docId}`);
|
|
412
|
+
return {
|
|
413
|
+
entry,
|
|
414
|
+
source: await fs.readFile(entry, "utf8")
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
const DECLARATION = /^(?:export\s+)?(?:const|let|function|class)\s+([A-Za-z_$][\w$]*)/;
|
|
418
|
+
/** The text inside a heading, without tags or `{…}` expressions, spaces collapsed. */
|
|
419
|
+
function headingText(inner) {
|
|
420
|
+
return inner.replace(/\{[^{}]*\}/g, " ").replace(/<[^>]*>/g, " ").replace(/\s+/g, " ").trim();
|
|
421
|
+
}
|
|
422
|
+
/** Where things are in a document's source, without the source itself. */
|
|
423
|
+
async function outlineDocument(ctx, docId) {
|
|
424
|
+
const { source } = await sourceOf(ctx, docId);
|
|
425
|
+
return {
|
|
426
|
+
id: docId,
|
|
427
|
+
hash: hashSource(source),
|
|
428
|
+
...outlineSource(source)
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
function outlineSource(source) {
|
|
432
|
+
const lines = source.split("\n");
|
|
433
|
+
const starts = [];
|
|
434
|
+
lines.forEach((l, i) => {
|
|
435
|
+
const m = DECLARATION.exec(l);
|
|
436
|
+
if (m) starts.push({
|
|
437
|
+
name: m[1] ?? "",
|
|
438
|
+
line: i + 1
|
|
439
|
+
});
|
|
440
|
+
else if (/^export\s+default\b/.test(l)) starts.push({
|
|
441
|
+
name: "default",
|
|
442
|
+
line: i + 1
|
|
443
|
+
});
|
|
444
|
+
});
|
|
445
|
+
const blocks = starts.map((s, i) => {
|
|
446
|
+
let end = (starts[i + 1]?.line ?? lines.length + 1) - 1;
|
|
447
|
+
while (end > s.line && !lines[end - 1]?.trim()) end -= 1;
|
|
448
|
+
return {
|
|
449
|
+
name: s.name,
|
|
450
|
+
lines: [s.line, end]
|
|
451
|
+
};
|
|
452
|
+
});
|
|
453
|
+
const headings = [];
|
|
454
|
+
const lineAt = (offset) => source.slice(0, offset).split("\n").length;
|
|
455
|
+
for (const m of source.matchAll(/<h([1-3])\b(?:[^>{]|\{[^}]*\})*>([\s\S]*?)<\/h\1>/g)) {
|
|
456
|
+
const text = headingText(m[2] ?? "");
|
|
457
|
+
if (text) headings.push({
|
|
458
|
+
level: Number(m[1]),
|
|
459
|
+
text: text.slice(0, 160),
|
|
460
|
+
line: lineAt(m.index)
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
return {
|
|
464
|
+
totalLines: lines.length,
|
|
465
|
+
blocks,
|
|
466
|
+
headings
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
/** Lines `fromLine`–`toLine` (1-based, inclusive) exactly as they are; either end may be left out. */
|
|
470
|
+
async function readDocumentLines(ctx, docId, fromLine, toLine) {
|
|
471
|
+
const { entry, source } = await sourceOf(ctx, docId);
|
|
472
|
+
const all = source.split("\n");
|
|
473
|
+
const from = Math.max(1, Math.floor(fromLine ?? 1));
|
|
474
|
+
const to = Math.min(all.length, Math.floor(toLine ?? all.length));
|
|
475
|
+
if (from > to) throw new OpsError(400, `lines ${from}–${to} are outside the document (${all.length} lines)`);
|
|
476
|
+
return {
|
|
477
|
+
id: docId,
|
|
478
|
+
entry: path.relative(ctx.userCwd, entry),
|
|
479
|
+
hash: hashSource(source),
|
|
480
|
+
lines: [from, to],
|
|
481
|
+
totalLines: all.length,
|
|
482
|
+
text: all.slice(from - 1, to).join("\n")
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
/** Every line that contains `text` (exact, case-sensitive), at most `limit` of them; `total` counts all. */
|
|
486
|
+
async function searchDocument(ctx, docId, text, limit = 60) {
|
|
487
|
+
if (!text) throw new OpsError(400, "text to search for is required");
|
|
488
|
+
const { source } = await sourceOf(ctx, docId);
|
|
489
|
+
const matches = [];
|
|
490
|
+
let total = 0;
|
|
491
|
+
source.split("\n").forEach((l, i) => {
|
|
492
|
+
if (!l.includes(text)) return;
|
|
493
|
+
total += 1;
|
|
494
|
+
if (matches.length < limit) matches.push({
|
|
495
|
+
line: i + 1,
|
|
496
|
+
text: l.length > 240 ? `${l.slice(0, 240)}…` : l
|
|
497
|
+
});
|
|
498
|
+
});
|
|
499
|
+
return {
|
|
500
|
+
id: docId,
|
|
501
|
+
hash: hashSource(source),
|
|
502
|
+
matches,
|
|
503
|
+
total
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
/** Text a model copied out of a JSON string, escapes and all: `\\n`, `\\t`, `\\"` back to what they stand for. */
|
|
507
|
+
function unescapeCopied(text) {
|
|
508
|
+
return text.replace(/\\(["\\nt])/g, (_m, c) => c === "n" ? "\n" : c === "t" ? " " : c);
|
|
509
|
+
}
|
|
510
|
+
const occurrences = (text, find) => text.split(find).length - 1;
|
|
511
|
+
/**
|
|
512
|
+
* Replaces `find` with `replace` in the source. `find` must occur exactly once
|
|
513
|
+
* (within `fromLine`–`toLine` when given), unless `all` is set, which replaces
|
|
514
|
+
* every occurrence. Text copied with its JSON escapes (`\\n` for a line break)
|
|
515
|
+
* is matched as what it stands for when only that reading occurs. Refused when
|
|
516
|
+
* the file changed since `expectedHash` was read.
|
|
517
|
+
*/
|
|
518
|
+
async function editDocument(ctx, docId, find, replace, opts = {}) {
|
|
519
|
+
if (!find) throw new OpsError(400, "`find` is empty: copy the exact text to change from the source");
|
|
520
|
+
const entry = resolveEntry(ctx, docId);
|
|
521
|
+
if (!entry) throw new OpsError(404, `document not found: ${docId}`);
|
|
522
|
+
return withFileLock(entry, async () => {
|
|
523
|
+
const source = await fs.readFile(entry, "utf8");
|
|
524
|
+
assertExpectedSource(source, { expectedHash: opts.expectedHash });
|
|
525
|
+
const lines = source.split("\n");
|
|
526
|
+
const from = Math.max(1, Math.floor(opts.fromLine ?? 1));
|
|
527
|
+
const to = Math.min(lines.length, Math.floor(opts.toLine ?? lines.length));
|
|
528
|
+
if (from > to) throw new OpsError(400, `lines ${from}–${to} are outside the document (${lines.length} lines)`);
|
|
529
|
+
const scope = lines.slice(from - 1, to).join("\n");
|
|
530
|
+
let needle = find;
|
|
531
|
+
let replacement = replace;
|
|
532
|
+
let count = occurrences(scope, needle);
|
|
533
|
+
let unescaped = false;
|
|
534
|
+
if (count === 0) {
|
|
535
|
+
const plain = unescapeCopied(find);
|
|
536
|
+
if (plain !== find && occurrences(scope, plain) > 0) {
|
|
537
|
+
needle = plain;
|
|
538
|
+
replacement = unescapeCopied(replace);
|
|
539
|
+
count = occurrences(scope, plain);
|
|
540
|
+
unescaped = true;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
const where = opts.fromLine !== void 0 || opts.toLine !== void 0 ? ` in lines ${from}–${to}` : "";
|
|
544
|
+
if (count === 0) throw new OpsError(404, `\`find\` is not in the document${where}: copy it exactly, spacing and quotes included`);
|
|
545
|
+
if (count > 1 && !opts.all) {
|
|
546
|
+
const at = [];
|
|
547
|
+
scope.split("\n").forEach((l, i) => {
|
|
548
|
+
if (l.includes(needle.split("\n")[0] ?? needle)) at.push(from + i);
|
|
549
|
+
});
|
|
550
|
+
throw new OpsError(409, `\`find\` occurs ${count} times${where} (lines ${at.slice(0, 12).join(", ")}${at.length > 12 ? ", …" : ""}): pass fromLine/toLine around the one you mean, include more of the text around it, or pass all to change every one`);
|
|
551
|
+
}
|
|
552
|
+
const changed = opts.all ? scope.split(needle).join(replacement) : scope.replace(needle, () => replacement);
|
|
553
|
+
const next = [
|
|
554
|
+
...lines.slice(0, from - 1),
|
|
555
|
+
...changed.split("\n"),
|
|
556
|
+
...lines.slice(to)
|
|
557
|
+
].join("\n");
|
|
558
|
+
await fs.writeFile(entry, next, "utf8");
|
|
559
|
+
return {
|
|
560
|
+
id: docId,
|
|
561
|
+
replaced: opts.all ? count : 1,
|
|
562
|
+
hash: hashSource(next),
|
|
563
|
+
...unescaped ? { unescaped: true } : {}
|
|
564
|
+
};
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
//#endregion
|
|
568
|
+
//#region src/ops/translations.ts
|
|
569
|
+
const run = promisify(execFile);
|
|
570
|
+
const DIFF_LIMIT = 2e5;
|
|
571
|
+
async function git(cwd, args) {
|
|
572
|
+
const { stdout } = await run("git", args, {
|
|
573
|
+
cwd,
|
|
574
|
+
maxBuffer: 33554432
|
|
575
|
+
});
|
|
576
|
+
return stdout;
|
|
577
|
+
}
|
|
578
|
+
async function gitOk(cwd, args) {
|
|
579
|
+
try {
|
|
580
|
+
await git(cwd, args);
|
|
581
|
+
return true;
|
|
582
|
+
} catch {
|
|
583
|
+
return false;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
function folderOf(ctx, docId) {
|
|
587
|
+
return path.relative(ctx.userCwd, path.join(ctx.docsRoot, docId)).split(path.sep).join("/");
|
|
588
|
+
}
|
|
589
|
+
async function readSource(ctx, docId) {
|
|
590
|
+
const entry = resolveEntry(ctx, docId);
|
|
591
|
+
return entry ? fs.readFile(entry, "utf8") : null;
|
|
592
|
+
}
|
|
593
|
+
async function statusOf(ctx, id, source, opts) {
|
|
594
|
+
const of = readMetaField(source, "translationOf");
|
|
595
|
+
if (!of) return null;
|
|
596
|
+
const translatedAt = readMetaField(source, "translatedAt");
|
|
597
|
+
const base = {
|
|
598
|
+
id,
|
|
599
|
+
lang: readMetaField(source, "lang"),
|
|
600
|
+
of,
|
|
601
|
+
state: "current",
|
|
602
|
+
...translatedAt ? { translatedAt } : {},
|
|
603
|
+
changedFiles: [],
|
|
604
|
+
commits: []
|
|
605
|
+
};
|
|
606
|
+
const original = await readSource(ctx, of);
|
|
607
|
+
if (original === null) return {
|
|
608
|
+
...base,
|
|
609
|
+
state: "missing-source"
|
|
610
|
+
};
|
|
611
|
+
base.sourceLang = readMetaField(original, "lang");
|
|
612
|
+
if (!opts.repo) return {
|
|
613
|
+
...base,
|
|
614
|
+
state: "no-git"
|
|
615
|
+
};
|
|
616
|
+
if (!translatedAt) return {
|
|
617
|
+
...base,
|
|
618
|
+
state: "unsynced"
|
|
619
|
+
};
|
|
620
|
+
if (!await gitOk(ctx.userCwd, [
|
|
621
|
+
"cat-file",
|
|
622
|
+
"-e",
|
|
623
|
+
`${translatedAt}^{commit}`
|
|
624
|
+
])) return {
|
|
625
|
+
...base,
|
|
626
|
+
state: "unknown-revision"
|
|
627
|
+
};
|
|
628
|
+
const folder = folderOf(ctx, of);
|
|
629
|
+
const tracked = (await git(ctx.userCwd, [
|
|
630
|
+
"diff",
|
|
631
|
+
"--name-only",
|
|
632
|
+
translatedAt,
|
|
633
|
+
"--",
|
|
634
|
+
folder
|
|
635
|
+
])).split("\n").filter(Boolean);
|
|
636
|
+
const untracked = (await git(ctx.userCwd, [
|
|
637
|
+
"ls-files",
|
|
638
|
+
"--others",
|
|
639
|
+
"--exclude-standard",
|
|
640
|
+
"--",
|
|
641
|
+
folder
|
|
642
|
+
])).split("\n").filter(Boolean);
|
|
643
|
+
const changedFiles = [.../* @__PURE__ */ new Set([...tracked, ...untracked])].sort();
|
|
644
|
+
const commits = (await git(ctx.userCwd, [
|
|
645
|
+
"log",
|
|
646
|
+
"--format=%h%x09%s",
|
|
647
|
+
`${translatedAt}..HEAD`,
|
|
648
|
+
"--",
|
|
649
|
+
folder
|
|
650
|
+
])).split("\n").filter(Boolean).map((line) => {
|
|
651
|
+
const [short, ...subject] = line.split(" ");
|
|
652
|
+
return {
|
|
653
|
+
short,
|
|
654
|
+
subject: subject.join(" ")
|
|
655
|
+
};
|
|
656
|
+
});
|
|
657
|
+
const status = {
|
|
658
|
+
...base,
|
|
659
|
+
state: changedFiles.length > 0 ? "stale" : "current",
|
|
660
|
+
changedFiles,
|
|
661
|
+
commits
|
|
662
|
+
};
|
|
663
|
+
if (opts.diff && changedFiles.length > 0) {
|
|
664
|
+
let diff = await git(ctx.userCwd, [
|
|
665
|
+
"diff",
|
|
666
|
+
translatedAt,
|
|
667
|
+
"--",
|
|
668
|
+
folder
|
|
669
|
+
]);
|
|
670
|
+
for (const file of untracked) {
|
|
671
|
+
const text = await fs.readFile(path.join(ctx.userCwd, file), "utf8").catch(() => "");
|
|
672
|
+
diff += `--- /dev/null\n+++ b/${file}\n${text.split("\n").map((l) => `+${l}`).join("\n")}\n`;
|
|
673
|
+
}
|
|
674
|
+
status.diff = diff.length > DIFF_LIMIT ? diff.slice(0, DIFF_LIMIT) : diff;
|
|
675
|
+
if (diff.length > DIFF_LIMIT) status.diffTruncated = true;
|
|
676
|
+
}
|
|
677
|
+
return status;
|
|
678
|
+
}
|
|
679
|
+
/**
|
|
680
|
+
* Every document that declares `meta.translationOf`, and whether its source
|
|
681
|
+
* moved on since the version it was translated from (`meta.translatedAt`, a
|
|
682
|
+
* commit). Pass `docId` for one translation, and `diff` for what changed.
|
|
683
|
+
*/
|
|
684
|
+
async function translationStatus(ctx, opts = {}) {
|
|
685
|
+
const repo = await gitOk(ctx.userCwd, ["rev-parse", "--is-inside-work-tree"]) === true;
|
|
686
|
+
const ids = opts.docId ? [opts.docId] : await listDocIds(ctx);
|
|
687
|
+
const out = [];
|
|
688
|
+
for (const id of ids) {
|
|
689
|
+
const source = await readSource(ctx, id);
|
|
690
|
+
if (source === null) {
|
|
691
|
+
if (opts.docId) throw new OpsError(404, `document not found: ${id}`);
|
|
692
|
+
continue;
|
|
693
|
+
}
|
|
694
|
+
const status = await statusOf(ctx, id, source, {
|
|
695
|
+
diff: opts.diff,
|
|
696
|
+
repo
|
|
697
|
+
});
|
|
698
|
+
if (status) out.push(status);
|
|
699
|
+
else if (opts.docId) throw new OpsError(422, `${id} has no meta.translationOf`);
|
|
700
|
+
}
|
|
701
|
+
return out;
|
|
702
|
+
}
|
|
703
|
+
/**
|
|
704
|
+
* Records that a translation now matches its source as of `revision` — by
|
|
705
|
+
* default the last commit that touched the source. Refuses while the source
|
|
706
|
+
* has uncommitted changes, which no commit could name.
|
|
707
|
+
*/
|
|
708
|
+
async function markTranslated(ctx, docId, revision) {
|
|
709
|
+
const entry = resolveEntry(ctx, docId);
|
|
710
|
+
if (!entry) throw new OpsError(404, `document not found: ${docId}`);
|
|
711
|
+
if (!await gitOk(ctx.userCwd, ["rev-parse", "--is-inside-work-tree"])) throw new OpsError(412, "the workspace is not a git repository; translations are traced by commit");
|
|
712
|
+
const source = await fs.readFile(entry, "utf8");
|
|
713
|
+
const of = readMetaField(source, "translationOf");
|
|
714
|
+
if (!of) throw new OpsError(422, `${docId} has no meta.translationOf`);
|
|
715
|
+
if (!resolveEntry(ctx, of)) throw new OpsError(404, `source document not found: ${of}`);
|
|
716
|
+
const folder = folderOf(ctx, of);
|
|
717
|
+
let commit;
|
|
718
|
+
if (revision) try {
|
|
719
|
+
commit = (await git(ctx.userCwd, [
|
|
720
|
+
"rev-parse",
|
|
721
|
+
"--verify",
|
|
722
|
+
`${revision}^{commit}`
|
|
723
|
+
])).trim();
|
|
724
|
+
} catch {
|
|
725
|
+
throw new OpsError(400, `not a commit: ${revision}`);
|
|
726
|
+
}
|
|
727
|
+
else {
|
|
728
|
+
if ((await git(ctx.userCwd, [
|
|
729
|
+
"status",
|
|
730
|
+
"--porcelain",
|
|
731
|
+
"--",
|
|
732
|
+
folder
|
|
733
|
+
])).trim()) throw new OpsError(409, `${of} has uncommitted changes; commit them first so the translation can name the version it matches`);
|
|
734
|
+
commit = (await git(ctx.userCwd, [
|
|
735
|
+
"log",
|
|
736
|
+
"-1",
|
|
737
|
+
"--format=%H",
|
|
738
|
+
"--",
|
|
739
|
+
folder
|
|
740
|
+
])).trim();
|
|
741
|
+
if (!commit) throw new OpsError(409, `${of} has never been committed`);
|
|
742
|
+
}
|
|
743
|
+
await withFileLock(entry, async () => {
|
|
744
|
+
const current = await fs.readFile(entry, "utf8");
|
|
745
|
+
const next = setMetaString(current, "translatedAt", commit);
|
|
746
|
+
if (next === null) throw new OpsError(422, `${docId} has no \`export const meta\` object`);
|
|
747
|
+
if (next !== current) await fs.writeFile(entry, next, "utf8");
|
|
748
|
+
});
|
|
749
|
+
return {
|
|
750
|
+
id: docId,
|
|
751
|
+
of,
|
|
752
|
+
translatedAt: commit
|
|
753
|
+
};
|
|
754
|
+
}
|
|
755
|
+
//#endregion
|
|
756
|
+
export { outlineSource as a, diffDocument as c, outlineDocument as i, translationStatus as n, readDocumentLines as o, editDocument as r, searchDocument as s, markTranslated as t };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { B as listDocIds, S as closeRenderSession, w as proofreadDocument } from "./session-DmbXf3u7.js";
|
|
2
|
+
import "./ops-BgbM5Irm.js";
|
|
3
|
+
import { t as cliContext } from "./context-CFPnmNCR.js";
|
|
4
|
+
import chalk from "chalk";
|
|
5
|
+
//#region src/cli/proofread.ts
|
|
6
|
+
function line(finding) {
|
|
7
|
+
const where = `p.${finding.page}`;
|
|
8
|
+
const detail = [chalk.dim(`${finding.rule}`), finding.loc && chalk.dim(`@ ${finding.loc}`)].filter(Boolean).join(" ");
|
|
9
|
+
return ` ${chalk.yellow("!")} ${chalk.dim(where.padEnd(5))} ${finding.message}\n ${chalk.dim(finding.snippet)}\n ${detail}`;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Reads the text every sheet prints and reports consistency slips. It is
|
|
13
|
+
* advice, so it exits zero even with findings; `check` is the gate.
|
|
14
|
+
*/
|
|
15
|
+
async function proofreadDocs(docIds, opts = {}) {
|
|
16
|
+
const ctx = await cliContext();
|
|
17
|
+
const targets = docIds.length > 0 ? docIds : await listDocIds(ctx);
|
|
18
|
+
if (targets.length === 0) throw new Error("No documents found under docs/.");
|
|
19
|
+
const reports = [];
|
|
20
|
+
try {
|
|
21
|
+
for (const docId of targets) {
|
|
22
|
+
const report = await proofreadDocument(ctx, docId);
|
|
23
|
+
reports.push(report);
|
|
24
|
+
if (opts.json) continue;
|
|
25
|
+
const summary = report.warnings === 0 ? chalk.green("clean") : chalk.yellow(`${report.warnings} warning(s)${report.omitted > 0 ? `, ${report.omitted} more not shown` : ""}`);
|
|
26
|
+
process.stdout.write(`${chalk.bold(docId)} ${chalk.dim(`${report.pageCount} pages · ${report.lang}`)} — ${summary}\n`);
|
|
27
|
+
for (const finding of report.findings) process.stdout.write(`${line(finding)}\n`);
|
|
28
|
+
}
|
|
29
|
+
} finally {
|
|
30
|
+
await closeRenderSession();
|
|
31
|
+
}
|
|
32
|
+
if (opts.json) process.stdout.write(`${JSON.stringify(reports, null, 2)}\n`);
|
|
33
|
+
}
|
|
34
|
+
//#endregion
|
|
35
|
+
export { proofreadDocs };
|