@dooc-dev/dooc 0.0.3 → 0.0.5
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/cli.js +98 -56
- package/dist/compiler/asset-references.d.ts +4 -0
- package/dist/compiler/asset-references.js +117 -0
- package/dist/compiler/bundle-urls.d.ts +1 -1
- package/dist/compiler/bundle-urls.js +15 -5
- package/dist/compiler/check.js +8 -5
- package/dist/compiler/code-languages.js +7 -2
- package/dist/compiler/compile.d.ts +5 -0
- package/dist/compiler/compile.js +105 -10
- package/dist/compiler/config.js +2 -1
- package/dist/compiler/export.d.ts +4 -0
- package/dist/compiler/export.js +140 -0
- package/dist/compiler/figures.js +4 -0
- package/dist/compiler/files.d.ts +1 -0
- package/dist/compiler/files.js +96 -38
- package/dist/compiler/fonts.js +4 -1
- package/dist/compiler/icons.d.ts +1 -1
- package/dist/compiler/icons.js +11 -2
- package/dist/compiler/ignored-files.d.ts +3 -0
- package/dist/compiler/ignored-files.js +68 -0
- package/dist/compiler/locales.js +13 -8
- package/dist/compiler/manifest.d.ts +2 -0
- package/dist/compiler/manifest.js +9 -2
- package/dist/compiler/ops.d.ts +6 -0
- package/dist/compiler/ops.js +98 -7
- package/dist/compiler/subformats.d.ts +24 -0
- package/dist/compiler/subformats.js +299 -0
- package/dist/compiler/textfile.js +3 -0
- package/dist/compiler/theme-variants.js +5 -2
- package/dist/core/content-metadata.d.ts +15 -6
- package/dist/core/content-metadata.js +278 -17
- package/dist/core/content-types.d.ts +3 -1
- package/dist/core/figure-frame.js +19 -1
- package/dist/core/files.d.ts +2 -1
- package/dist/core/files.js +6 -8
- package/dist/core/flow-base-css.generated.js +1 -1
- package/dist/core/html.js +3 -3
- package/dist/core/icons.d.ts +3 -0
- package/dist/core/icons.js +3 -0
- package/dist/core/json-tools.d.ts +1 -0
- package/dist/core/json-tools.js +12 -0
- package/dist/core/routing.d.ts +5 -2
- package/dist/core/routing.js +17 -5
- package/dist/core/subformat-record.d.ts +3 -0
- package/dist/core/subformat-record.js +131 -0
- package/dist/core/subformats.d.ts +43 -0
- package/dist/core/subformats.js +81 -0
- package/dist/core/toc-ui.js +25 -23
- package/dist/edit.global.js +322 -95
- package/dist/mcp.js +50 -9
- package/dist/plugins/index.d.ts +1 -0
- package/dist/plugins/local-storage.js +3 -0
- package/dist/plugins/oauth.d.ts +1 -1
- package/dist/plugins/oauth.js +8 -2
- package/dist/plugins/runtime.d.ts +6 -0
- package/dist/plugins/runtime.js +166 -34
- package/dist/plugins/transport.d.ts +22 -0
- package/dist/plugins/transport.js +1 -0
- package/dist/plugins/types.d.ts +3 -1
- package/dist/plugins.global.js +128 -0
- package/dist/subformats.global.js +127 -0
- package/dist/ui.global.js +91 -38
- package/dist/viewer/build-plugins.d.ts +7 -0
- package/dist/viewer/build-plugins.js +70 -0
- package/dist/viewer/build.d.ts +9 -0
- package/dist/viewer/build.js +32 -5
- package/dist/viewer/editor-tools.d.ts +7 -7
- package/dist/viewer/editor-tools.js +36 -5
- package/dist/viewer/font-picker.js +5 -1
- package/dist/viewer/git-diff.d.ts +3 -0
- package/dist/viewer/git-diff.js +47 -0
- package/dist/viewer/hmr.js +4 -1
- package/dist/viewer/i18n.js +7 -5
- package/dist/viewer/language-picker.d.ts +2 -2
- package/dist/viewer/language-picker.js +26 -9
- package/dist/viewer/lazy-graph.d.ts +2 -0
- package/dist/viewer/lazy-graph.js +30 -0
- package/dist/viewer/locales/eng.json +74 -10
- package/dist/viewer/locales/jpn.json +75 -11
- package/dist/viewer/locales/zho.json +75 -11
- package/dist/viewer/nav-focus.js +8 -1
- package/dist/viewer/nav-update.d.ts +2 -0
- package/dist/viewer/nav-update.js +117 -0
- package/dist/viewer/plugin-style.d.ts +1 -0
- package/dist/viewer/plugin-style.js +82 -0
- package/dist/viewer/plugins.js +46 -3
- package/dist/viewer/resize.d.ts +0 -2
- package/dist/viewer/resize.js +2 -98
- package/dist/viewer/resource-library.d.ts +10 -8
- package/dist/viewer/resource-library.js +179 -30
- package/dist/viewer/search.d.ts +4 -2
- package/dist/viewer/search.js +252 -53
- package/dist/viewer/serve.d.ts +10 -5
- package/dist/viewer/serve.js +882 -287
- package/dist/viewer/site-icon.js +13 -6
- package/dist/viewer/site-title.d.ts +13 -0
- package/dist/viewer/site-title.js +50 -0
- package/dist/viewer/textfile.d.ts +3 -0
- package/dist/viewer/textfile.js +19 -2
- package/dist/viewer/theme-picker.d.ts +2 -0
- package/dist/viewer/theme-picker.js +24 -8
- package/dist/viewer/workspace.d.ts +2 -0
- package/dist/viewer/workspace.js +46 -1
- package/package.json +24 -9
- package/dist/compiler/bundle.d.ts +0 -2
- package/dist/compiler/bundle.js +0 -100
package/dist/compiler/fonts.js
CHANGED
|
@@ -2,6 +2,7 @@ import { createHash } from "node:crypto";
|
|
|
2
2
|
import { lstatSync, mkdirSync, readdirSync, readFileSync, unlinkSync, writeFileSync, } from "node:fs";
|
|
3
3
|
import { basename, extname } from "node:path";
|
|
4
4
|
import { localFile } from "../core/files.js";
|
|
5
|
+
import { ignoredFiles } from "./ignored-files.js";
|
|
5
6
|
import { textfilePath } from "./textfile.js";
|
|
6
7
|
export const PROJECT_FONTS_DIR = ".dooc/fonts";
|
|
7
8
|
const fontName = (name) => {
|
|
@@ -13,10 +14,12 @@ export const listProjectFonts = (root) => {
|
|
|
13
14
|
const directory = textfilePath(root, PROJECT_FONTS_DIR);
|
|
14
15
|
if (!lstatSync(directory, { throwIfNoEntry: false }))
|
|
15
16
|
return [];
|
|
16
|
-
|
|
17
|
+
const names = readdirSync(directory, { withFileTypes: true })
|
|
17
18
|
.filter((entry) => entry.isFile() && /\.(ttf|otf|woff2?)$/i.test(entry.name))
|
|
18
19
|
.map((entry) => fontName(entry.name))
|
|
19
20
|
.sort();
|
|
21
|
+
const ignored = ignoredFiles(root, names.map((name) => `${PROJECT_FONTS_DIR}/${name}`));
|
|
22
|
+
return names.filter((name) => !ignored.has(`${PROJECT_FONTS_DIR}/${name}`));
|
|
20
23
|
};
|
|
21
24
|
export const addProjectFont = (root, file) => {
|
|
22
25
|
const name = fontName(basename(file));
|
package/dist/compiler/icons.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ export declare const ICONS_DIR = "icons";
|
|
|
8
8
|
*/
|
|
9
9
|
export declare const squareSvg: (svg: string) => string | undefined;
|
|
10
10
|
/** Read icons/ (docId → svg markup). An icon with no doc is an error (missed rename / delete). */
|
|
11
|
-
export declare const loadIcons: (root: string, docIds: ReadonlySet<string>, siteIconFile?: string, layout?: FileLayout) => {
|
|
11
|
+
export declare const loadIcons: (root: string, docIds: ReadonlySet<string>, siteIconFile?: string, layout?: FileLayout, siteIcons?: readonly string[]) => {
|
|
12
12
|
icons: Map<string, string>;
|
|
13
13
|
errors: string[];
|
|
14
14
|
};
|
package/dist/compiler/icons.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import { docIconFile } from "../core/files.js";
|
|
6
|
+
import { assertNotIgnored, ignoredFiles } from "./ignored-files.js";
|
|
6
7
|
import { textfilePath } from "./textfile.js";
|
|
7
8
|
export const ICONS_DIR = "icons";
|
|
8
9
|
const ROOT_SVG_RE = /<svg\b[^>]*>/i;
|
|
@@ -44,11 +45,15 @@ export const squareSvg = (svg) => {
|
|
|
44
45
|
return svg.replace(root, next);
|
|
45
46
|
};
|
|
46
47
|
/** Read icons/ (docId → svg markup). An icon with no doc is an error (missed rename / delete). */
|
|
47
|
-
export const loadIcons = (root, docIds, siteIconFile, layout) => {
|
|
48
|
+
export const loadIcons = (root, docIds, siteIconFile, layout, siteIcons = []) => {
|
|
48
49
|
const dir = join(root, ICONS_DIR);
|
|
49
50
|
const icons = new Map();
|
|
50
51
|
const errors = [];
|
|
51
|
-
|
|
52
|
+
const files = existsSync(dir) ? readdirSync(dir) : [];
|
|
53
|
+
const ignored = ignoredFiles(root, files.map((file) => `${ICONS_DIR}/${file}`));
|
|
54
|
+
for (const f of files) {
|
|
55
|
+
if (ignored.has(`${ICONS_DIR}/${f}`))
|
|
56
|
+
continue;
|
|
52
57
|
if (f.startsWith(".")) {
|
|
53
58
|
continue;
|
|
54
59
|
}
|
|
@@ -56,6 +61,9 @@ export const loadIcons = (root, docIds, siteIconFile, layout) => {
|
|
|
56
61
|
if (!docIds.has(id)) {
|
|
57
62
|
if (siteIconFile?.replace(/^\.\//, "") === `${ICONS_DIR}/${f}`)
|
|
58
63
|
continue;
|
|
64
|
+
if (siteIcons.includes(`${ICONS_DIR}/${f}`) ||
|
|
65
|
+
Object.values(layout?.icons ?? {}).includes(`${ICONS_DIR}/${f}`))
|
|
66
|
+
continue;
|
|
59
67
|
errors.push(`${ICONS_DIR}/${f}: no matching doc (content/${id || f}.md)`);
|
|
60
68
|
continue;
|
|
61
69
|
}
|
|
@@ -70,6 +78,7 @@ export const loadIcons = (root, docIds, siteIconFile, layout) => {
|
|
|
70
78
|
if (layout?.icons?.[id]) {
|
|
71
79
|
try {
|
|
72
80
|
const file = textfilePath(root, docIconFile(id, layout));
|
|
81
|
+
assertNotIgnored(root, docIconFile(id, layout));
|
|
73
82
|
if (!existsSync(file))
|
|
74
83
|
continue;
|
|
75
84
|
const svg = readFileSync(file, "utf8");
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { existsSync, readFileSync, realpathSync, statSync } from "node:fs";
|
|
3
|
+
import { dirname, join, posix, relative, resolve, sep } from "node:path";
|
|
4
|
+
import ignore, {} from "ignore";
|
|
5
|
+
/** Use Git's tracked-file and exclude rules; standalone folders use their .gitignore files. */
|
|
6
|
+
export const ignoredFiles = (root, files) => {
|
|
7
|
+
if (!files.length)
|
|
8
|
+
return new Set();
|
|
9
|
+
const hasGit = (directory) => existsSync(join(directory, ".git", "HEAD")) ||
|
|
10
|
+
statSync(join(directory, ".git"), { throwIfNoEntry: false })?.isFile();
|
|
11
|
+
let repository = resolve(root);
|
|
12
|
+
while (!hasGit(repository) && dirname(repository) !== repository)
|
|
13
|
+
repository = dirname(repository);
|
|
14
|
+
const inRepository = hasGit(repository);
|
|
15
|
+
if (inRepository) {
|
|
16
|
+
const result = spawnSync("git", ["-C", root, "check-ignore", "--stdin", "-z"], {
|
|
17
|
+
input: `${files.join("\0")}\0`,
|
|
18
|
+
encoding: "utf8",
|
|
19
|
+
timeout: 2_000,
|
|
20
|
+
maxBuffer: 5_000_000,
|
|
21
|
+
windowsHide: true,
|
|
22
|
+
});
|
|
23
|
+
if (result.status === 0 || result.status === 1)
|
|
24
|
+
return new Set(result.stdout.split("\0").filter(Boolean));
|
|
25
|
+
if (result.error?.code !== "ENOENT")
|
|
26
|
+
throw new Error(`Cannot check ignored files: ${result.error?.message ?? result.stderr.trim()}`);
|
|
27
|
+
}
|
|
28
|
+
const ignoreRoot = inRepository ? repository : root;
|
|
29
|
+
const prefix = relative(ignoreRoot, root).split(sep).join("/");
|
|
30
|
+
const rules = new Map();
|
|
31
|
+
const readRules = (directory) => {
|
|
32
|
+
let matcher = rules.get(directory);
|
|
33
|
+
if (!matcher) {
|
|
34
|
+
const path = join(ignoreRoot, directory, ".gitignore");
|
|
35
|
+
matcher = ignore().add(existsSync(path) ? readFileSync(path, "utf8") : "");
|
|
36
|
+
rules.set(directory, matcher);
|
|
37
|
+
}
|
|
38
|
+
return matcher;
|
|
39
|
+
};
|
|
40
|
+
return new Set(files.filter((file) => {
|
|
41
|
+
const normalized = posix.join(prefix, file);
|
|
42
|
+
const parts = normalized.replace(/\/$/, "").split("/");
|
|
43
|
+
const matchers = [];
|
|
44
|
+
for (let i = 0; i < parts.length; i++) {
|
|
45
|
+
const directory = parts.slice(0, i).join("/");
|
|
46
|
+
matchers.push(readRules(directory));
|
|
47
|
+
let ignored = false;
|
|
48
|
+
for (const [base, matcher] of matchers.entries()) {
|
|
49
|
+
const path = parts.slice(base, i + 1).join("/") +
|
|
50
|
+
(i < parts.length - 1 || file.endsWith("/") ? "/" : "");
|
|
51
|
+
const result = matcher.test(path);
|
|
52
|
+
if (result.ignored || result.unignored)
|
|
53
|
+
ignored = result.ignored;
|
|
54
|
+
}
|
|
55
|
+
if (ignored)
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
return false;
|
|
59
|
+
}));
|
|
60
|
+
};
|
|
61
|
+
export const assertNotIgnored = (root, file) => {
|
|
62
|
+
const path = join(root, file);
|
|
63
|
+
const resolved = existsSync(path)
|
|
64
|
+
? relative(realpathSync(root), realpathSync(path)).split(sep).join("/")
|
|
65
|
+
: file;
|
|
66
|
+
if (ignoredFiles(root, [...new Set([file, resolved])]).size)
|
|
67
|
+
throw new Error(`Referenced file is ignored by Git ignore rules: ${file}`);
|
|
68
|
+
};
|
package/dist/compiler/locales.js
CHANGED
|
@@ -5,6 +5,7 @@ import { isDocId } from "../core/content-metadata.js";
|
|
|
5
5
|
import { localFile } from "../core/files.js";
|
|
6
6
|
import { contentLanguageCode, DEFAULT_LANG } from "../core/language.js";
|
|
7
7
|
import { parseDoc } from "./dialect.js";
|
|
8
|
+
import { ignoredFiles } from "./ignored-files.js";
|
|
8
9
|
import { isContentLanguage } from "./language-codes.js";
|
|
9
10
|
import { docFile, readManifest } from "./manifest.js";
|
|
10
11
|
import { assembleDoc } from "./serialize.js";
|
|
@@ -32,14 +33,18 @@ export const siteContentLanguages = (root, manifest = readManifest(root)) => man
|
|
|
32
33
|
...translationLanguages(root),
|
|
33
34
|
]),
|
|
34
35
|
];
|
|
35
|
-
export const translationFiles = (root) =>
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
36
|
+
export const translationFiles = (root) => {
|
|
37
|
+
const files = translationLanguages(root).flatMap((language) => {
|
|
38
|
+
const directory = `locales/${language}`;
|
|
39
|
+
return readdirSync(textfilePath(root, directory), { recursive: true, withFileTypes: true })
|
|
40
|
+
.filter((entry) => entry.isFile() && entry.name.endsWith(".md"))
|
|
41
|
+
.map((entry) => relative(root, join(entry.parentPath, entry.name)).replaceAll("\\", "/"))
|
|
42
|
+
.filter(localFile)
|
|
43
|
+
.sort();
|
|
44
|
+
});
|
|
45
|
+
const ignored = ignoredFiles(root, files);
|
|
46
|
+
return files.filter((file) => !ignored.has(file));
|
|
47
|
+
};
|
|
43
48
|
export const setDocTranslation = (root, sourceLanguage, id, language, added) => {
|
|
44
49
|
if (!isDocId(id))
|
|
45
50
|
throw new Error("Invalid document id");
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { type DocNode, type ParsedManifest } from "../core/content-metadata.js";
|
|
2
|
+
/** CLI paths select a manifest; every project path is relative to its parent. */
|
|
3
|
+
export declare const projectRoot: (input?: string) => string;
|
|
2
4
|
export declare const parseManifestText: (raw: string) => ParsedManifest;
|
|
3
5
|
/** Empty tree if missing (a new docs root with zero docs). */
|
|
4
6
|
export declare const readManifest: (root: string) => ParsedManifest;
|
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
// Reads/writes dooc.yaml (node-dependent). Tree validation and manipulation live in
|
|
2
2
|
// core/content-metadata.ts.
|
|
3
|
-
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
4
|
-
import { join } from "node:path";
|
|
3
|
+
import { existsSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
5
5
|
import { parse } from "yaml";
|
|
6
6
|
import { MANIFEST_FILE, parseManifest, serializeManifest, } from "../core/content-metadata.js";
|
|
7
|
+
/** CLI paths select a manifest; every project path is relative to its parent. */
|
|
8
|
+
export const projectRoot = (input = MANIFEST_FILE) => {
|
|
9
|
+
const file = resolve(input);
|
|
10
|
+
if (basename(file) !== MANIFEST_FILE || !existsSync(file) || !statSync(file).isFile())
|
|
11
|
+
throw new Error(`Specify an existing dooc.yaml file: ${file}`);
|
|
12
|
+
return dirname(file);
|
|
13
|
+
};
|
|
7
14
|
export const parseManifestText = (raw) => {
|
|
8
15
|
let value;
|
|
9
16
|
try {
|
package/dist/compiler/ops.d.ts
CHANGED
|
@@ -19,6 +19,12 @@ export declare const createDocOps: (root: string) => {
|
|
|
19
19
|
addFile: (name: string, title: string, parent?: string) => OpResult<{
|
|
20
20
|
id: string;
|
|
21
21
|
}>;
|
|
22
|
+
addSubformat: (ext: string, title: string, parent?: string) => OpResult<{
|
|
23
|
+
id: string;
|
|
24
|
+
}>;
|
|
25
|
+
renameSubformat: (id: string, title: string) => OpResult<{
|
|
26
|
+
id: string;
|
|
27
|
+
}>;
|
|
22
28
|
move: (id: string, parent?: string, index?: number) => OpResult<{
|
|
23
29
|
id: string;
|
|
24
30
|
}>;
|
package/dist/compiler/ops.js
CHANGED
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
// add / remove figures). Shared by the viewer's edit API, the CLI (doc / figure) and MCP.
|
|
3
3
|
// Failures are returned as { error, status } (status is HTTP-like) with no partial updates.
|
|
4
4
|
import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, unlinkSync, writeFileSync, } from "node:fs";
|
|
5
|
-
import { dirname, join } from "node:path";
|
|
6
|
-
import { flattenTree, insertDoc, isDocId, moveDoc, removeDoc, renameDoc, } from "../core/content-metadata.js";
|
|
7
|
-
import { defaultFilePath, docIconFile, isDocumentFileName, parseFileLayout, } from "../core/files.js";
|
|
5
|
+
import { dirname, join, posix } from "node:path";
|
|
6
|
+
import { flattenTree, insertDoc, isDocId, moveDoc, removeDoc, renameDoc, subformatPageId, } from "../core/content-metadata.js";
|
|
7
|
+
import { defaultFilePath, docIconFile, fileDocumentId, isDocumentFileName, parseFileLayout, } from "../core/files.js";
|
|
8
8
|
import { parseDocLink } from "../core/routing.js";
|
|
9
9
|
import { collectDocLinks, compileSite } from "./compile.js";
|
|
10
10
|
import { figureName, figureUploadName } from "./figures.js";
|
|
11
|
-
import { projectFiles, rewriteFileLinks } from "./files.js";
|
|
11
|
+
import { filesSnapshot, projectFiles, rewriteFileLinks, updateFiles } from "./files.js";
|
|
12
12
|
import { translationFile, translationFiles, translationLanguages } from "./locales.js";
|
|
13
13
|
import { docFile, readManifest, writeManifest } from "./manifest.js";
|
|
14
14
|
import { assembleDoc } from "./serialize.js";
|
|
@@ -27,7 +27,10 @@ export const createDocOps = (root) => {
|
|
|
27
27
|
? { error: manifest.errors.join(" / "), status: 409 }
|
|
28
28
|
: manifest;
|
|
29
29
|
};
|
|
30
|
-
const existingDoc = (id) => typeof id === "string" &&
|
|
30
|
+
const existingDoc = (id) => typeof id === "string" &&
|
|
31
|
+
isDocId(id) &&
|
|
32
|
+
!flattenTree(readManifest(root).tree).some((node) => node.id === id && node.subformat) &&
|
|
33
|
+
existsSync(docPath(id))
|
|
31
34
|
? { id }
|
|
32
35
|
: { error: `doc "${String(id)}" not found`, status: 404 };
|
|
33
36
|
// Include extracted callout sources and unregistered Markdown files in reference checks.
|
|
@@ -56,11 +59,20 @@ export const createDocOps = (root) => {
|
|
|
56
59
|
if (isOpError(loaded)) {
|
|
57
60
|
return loaded;
|
|
58
61
|
}
|
|
62
|
+
if (loaded.format === "nested") {
|
|
63
|
+
const file = loaded.files?.defaultLayout !== false
|
|
64
|
+
? defaultFilePath(filename ?? `${id}.md`)
|
|
65
|
+
: `content/${filename ?? `${id}.md`}`;
|
|
66
|
+
id = fileDocumentId(file);
|
|
67
|
+
loaded.files ??= parseFileLayout({});
|
|
68
|
+
loaded.files.documents[id] = file;
|
|
69
|
+
}
|
|
59
70
|
const tree = insertDoc(loaded.tree, id, parent);
|
|
60
71
|
if (!tree) {
|
|
61
72
|
return { error: `duplicate id "${id}" or missing parent "${parent}"`, status: 409 };
|
|
62
73
|
}
|
|
63
|
-
if (
|
|
74
|
+
if (loaded.format !== "nested" &&
|
|
75
|
+
(filename !== undefined || loaded.files?.defaultLayout !== false)) {
|
|
64
76
|
loaded.files ??= parseFileLayout({});
|
|
65
77
|
loaded.files.documents[id] = loaded.files.defaultLayout
|
|
66
78
|
? defaultFilePath(filename ?? `${id}.md`)
|
|
@@ -92,6 +104,37 @@ export const createDocOps = (root) => {
|
|
|
92
104
|
id = `${stem}-${index}`;
|
|
93
105
|
return add(id, title, parent, name);
|
|
94
106
|
};
|
|
107
|
+
const addSubformat = (ext, title, parent) => {
|
|
108
|
+
const loaded = loadTree();
|
|
109
|
+
if (isOpError(loaded))
|
|
110
|
+
return loaded;
|
|
111
|
+
if (!loaded.subformats?.some((format) => format.ext === ext) || !title.trim())
|
|
112
|
+
return { error: "Choose an existing subformat and enter a title", status: 400 };
|
|
113
|
+
const id = subformatPageId(ext);
|
|
114
|
+
const tree = insertDoc(loaded.tree, id, parent, { subformat: ext, title: title.trim() });
|
|
115
|
+
if (!tree)
|
|
116
|
+
return { error: "Page already exists or parent is missing", status: 409 };
|
|
117
|
+
writeManifest(root, tree, loaded);
|
|
118
|
+
return { id };
|
|
119
|
+
};
|
|
120
|
+
const renameSubformat = (id, title) => {
|
|
121
|
+
const loaded = loadTree();
|
|
122
|
+
if (isOpError(loaded))
|
|
123
|
+
return loaded;
|
|
124
|
+
if (!title.trim())
|
|
125
|
+
return { error: "title is required", status: 400 };
|
|
126
|
+
const visit = (nodes) => nodes.some((node) => {
|
|
127
|
+
if (node.id === id && node.subformat) {
|
|
128
|
+
node.title = title.trim();
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
return visit(node.children);
|
|
132
|
+
});
|
|
133
|
+
if (!visit(loaded.tree))
|
|
134
|
+
return { error: "Subformat page not found", status: 404 };
|
|
135
|
+
writeManifest(root, loaded.tree, loaded);
|
|
136
|
+
return { id };
|
|
137
|
+
};
|
|
95
138
|
/**
|
|
96
139
|
* Reorders and reparents. Rewrites only dooc.yaml. Omitted index = after the last child
|
|
97
140
|
* of parent.
|
|
@@ -125,6 +168,37 @@ export const createDocOps = (root) => {
|
|
|
125
168
|
if (newId === "" || newId === oldId) {
|
|
126
169
|
return { id: oldId, updatedRefs: 0 };
|
|
127
170
|
}
|
|
171
|
+
const manifest = readManifest(root);
|
|
172
|
+
if (manifest.format === "nested") {
|
|
173
|
+
const name = newId.endsWith(".md") ? newId : `${newId}.md`;
|
|
174
|
+
if (!isDocumentFileName(name))
|
|
175
|
+
return { error: "Enter a Markdown file name", status: 400 };
|
|
176
|
+
try {
|
|
177
|
+
const file = docFile(oldId, manifest.files);
|
|
178
|
+
const next = posix.join(posix.dirname(file), name);
|
|
179
|
+
const moves = new Map([[file, next]]);
|
|
180
|
+
const updatedRefs = referenceFiles().filter((path) => {
|
|
181
|
+
const raw = readFileSync(textfilePath(root, path), "utf8");
|
|
182
|
+
return rewriteFileLinks(raw, moves) !== raw;
|
|
183
|
+
}).length;
|
|
184
|
+
updateFiles(root, {
|
|
185
|
+
action: "rename",
|
|
186
|
+
file,
|
|
187
|
+
name,
|
|
188
|
+
revision: filesSnapshot(root).revision,
|
|
189
|
+
});
|
|
190
|
+
return {
|
|
191
|
+
id: fileDocumentId(next),
|
|
192
|
+
updatedRefs,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
catch (error) {
|
|
196
|
+
return {
|
|
197
|
+
error: error instanceof Error ? error.message : String(error),
|
|
198
|
+
status: 409,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
}
|
|
128
202
|
if (!isDocId(newId)) {
|
|
129
203
|
return { error: "id must be a slug (lowercase alphanumerics, hyphens)", status: 400 };
|
|
130
204
|
}
|
|
@@ -204,6 +278,22 @@ export const createDocOps = (root) => {
|
|
|
204
278
|
* referencedBy. parent = the deleted doc's parent.
|
|
205
279
|
*/
|
|
206
280
|
const remove = (id, force = false) => {
|
|
281
|
+
const manifest = loadTree();
|
|
282
|
+
if (isOpError(manifest))
|
|
283
|
+
return manifest;
|
|
284
|
+
const page = flattenTree(manifest.tree).find((node) => node.id === id && node.subformat);
|
|
285
|
+
if (page) {
|
|
286
|
+
const tree = removeDoc(manifest.tree, id);
|
|
287
|
+
if (!tree)
|
|
288
|
+
return { error: "doc has children (move / delete them first)", status: 409 };
|
|
289
|
+
const removedIcon = iconPath(id);
|
|
290
|
+
if (manifest.files?.icons)
|
|
291
|
+
delete manifest.files.icons[id];
|
|
292
|
+
writeManifest(root, tree, manifest);
|
|
293
|
+
if (existsSync(removedIcon))
|
|
294
|
+
unlinkSync(removedIcon);
|
|
295
|
+
return { parent: page.parent, danglingRefs: 0 };
|
|
296
|
+
}
|
|
207
297
|
const target = existingDoc(id);
|
|
208
298
|
if (isOpError(target)) {
|
|
209
299
|
return target;
|
|
@@ -245,7 +335,6 @@ export const createDocOps = (root) => {
|
|
|
245
335
|
delete loaded.files.documents[id];
|
|
246
336
|
if (loaded.files.icons)
|
|
247
337
|
delete loaded.files.icons[id];
|
|
248
|
-
loaded.files.order = loaded.files.order.filter((file) => join(root, file) !== removedPath);
|
|
249
338
|
}
|
|
250
339
|
writeManifest(root, tree, loaded);
|
|
251
340
|
unlinkSync(removedPath);
|
|
@@ -352,6 +441,8 @@ export const createDocOps = (root) => {
|
|
|
352
441
|
figureFiles,
|
|
353
442
|
add,
|
|
354
443
|
addFile,
|
|
444
|
+
addSubformat,
|
|
445
|
+
renameSubformat,
|
|
355
446
|
move,
|
|
356
447
|
rename,
|
|
357
448
|
remove,
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ValidateFunction } from "ajv";
|
|
2
|
+
import { type Subformat, type SubformatCollection, type SubformatField } from "../core/subformats.js";
|
|
3
|
+
export interface LoadedSubformat extends Subformat {
|
|
4
|
+
fields: SubformatField[];
|
|
5
|
+
template: string;
|
|
6
|
+
validate: ValidateFunction;
|
|
7
|
+
}
|
|
8
|
+
export declare const compileSubformatSchema: (content: string) => {
|
|
9
|
+
schema: import("ajv").SchemaObject;
|
|
10
|
+
validate: ValidateFunction<unknown>;
|
|
11
|
+
};
|
|
12
|
+
export declare const loadSubformat: (root: string, config: Subformat) => LoadedSubformat;
|
|
13
|
+
export declare const validateSubformatRecord: (format: LoadedSubformat, data: Record<string, unknown>) => void;
|
|
14
|
+
export declare const validateSubformatFile: (root: string, file: string, content: string) => void;
|
|
15
|
+
export declare const subformatResponse: (root: string, input: Record<string, unknown> | undefined) => {
|
|
16
|
+
status: number;
|
|
17
|
+
body: {
|
|
18
|
+
content?: string;
|
|
19
|
+
data?: Record<string, unknown>;
|
|
20
|
+
collection?: SubformatCollection;
|
|
21
|
+
error?: string;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
export { parseSubformatRecord } from "../core/subformat-record.js";
|