@dooc-dev/dooc 0.0.4 → 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 +41 -3
- 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.js +80 -41
- 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/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/transport.d.ts +22 -0
- package/dist/plugins/transport.js +1 -0
- 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 +72 -10
- package/dist/viewer/locales/jpn.json +73 -11
- package/dist/viewer/locales/zho.json +73 -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/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 +789 -278
- package/dist/viewer/site-icon.js +13 -6
- package/dist/viewer/site-title.js +15 -2
- 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
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
|
|
2
|
+
import { posix } from "node:path";
|
|
3
|
+
import { visit } from "unist-util-visit";
|
|
4
|
+
import { flattenTree, MANIFEST_FILE } from "../core/content-metadata.js";
|
|
5
|
+
import { docFile, docIconFile, fileExtension, localFile } from "../core/files.js";
|
|
6
|
+
import { contentLinks, parseMdast } from "../core/markdown.js";
|
|
7
|
+
import { isRemoteTextfile, parseFileReference } from "../core/textfile.js";
|
|
8
|
+
import { assetReferences } from "./asset-references.js";
|
|
9
|
+
import { CODE_LANGUAGES_DIR } from "./code-languages.js";
|
|
10
|
+
import { CONFIG_FILE } from "./config.js";
|
|
11
|
+
import { collectRefs } from "./dialect.js";
|
|
12
|
+
import { projectFiles } from "./files.js";
|
|
13
|
+
import { PROJECT_FONTS_DIR } from "./fonts.js";
|
|
14
|
+
import { assertNotIgnored, ignoredFiles } from "./ignored-files.js";
|
|
15
|
+
import { siteContentLanguages, translationFile } from "./locales.js";
|
|
16
|
+
import { readManifest } from "./manifest.js";
|
|
17
|
+
import { readTextfile, textfilePath } from "./textfile.js";
|
|
18
|
+
import { themeVariants } from "./theme-variants.js";
|
|
19
|
+
import { zip } from "./zip.js";
|
|
20
|
+
/** Collect source dependencies without executing configuration, plugins or remote requests. */
|
|
21
|
+
export const exportEntries = (root) => {
|
|
22
|
+
const manifest = readManifest(root);
|
|
23
|
+
if (manifest.errors.length)
|
|
24
|
+
throw new Error(manifest.errors.join("\n"));
|
|
25
|
+
const entries = new Map();
|
|
26
|
+
const add = (file) => {
|
|
27
|
+
file = file.replace(/^(\.\/)+/, "");
|
|
28
|
+
if (entries.has(file))
|
|
29
|
+
return;
|
|
30
|
+
const settingsFile = [PROJECT_FONTS_DIR, CODE_LANGUAGES_DIR, ".dooc/editor-tools"].some((dir) => file.startsWith(`${dir}/`) && localFile(file.slice(dir.length + 1)));
|
|
31
|
+
// References are already URL-decoded; # and ? are literal filename characters here.
|
|
32
|
+
if (!localFile(file.replace(/[?#]/g, "_")) && !settingsFile)
|
|
33
|
+
throw new Error(`Invalid export dependency: ${file}`);
|
|
34
|
+
const path = textfilePath(root, file);
|
|
35
|
+
assertNotIgnored(root, file);
|
|
36
|
+
if (!statSync(path).isFile())
|
|
37
|
+
throw new Error(`Expected a regular source file: ${file}`);
|
|
38
|
+
entries.set(file, { name: file, data: readFileSync(path) });
|
|
39
|
+
for (const variant of Object.values(themeVariants(root, file)))
|
|
40
|
+
add(variant);
|
|
41
|
+
};
|
|
42
|
+
const optional = (file) => {
|
|
43
|
+
if (existsSync(textfilePath(root, file)) && !ignoredFiles(root, [file]).has(file))
|
|
44
|
+
add(file);
|
|
45
|
+
};
|
|
46
|
+
add(MANIFEST_FILE);
|
|
47
|
+
optional(CONFIG_FILE);
|
|
48
|
+
for (const { id, subformat } of flattenTree(manifest.tree)) {
|
|
49
|
+
const file = subformat ? undefined : docFile(id, manifest.files);
|
|
50
|
+
if (file)
|
|
51
|
+
add(file);
|
|
52
|
+
if (manifest.files?.icons?.[id])
|
|
53
|
+
add(docIconFile(id, manifest.files));
|
|
54
|
+
else
|
|
55
|
+
optional(docIconFile(id, manifest.files));
|
|
56
|
+
if (!file)
|
|
57
|
+
continue;
|
|
58
|
+
for (const language of siteContentLanguages(root, manifest))
|
|
59
|
+
optional(translationFile(file, language));
|
|
60
|
+
}
|
|
61
|
+
for (const file of new Set([manifest.icon, ...Object.values(manifest.themeIcons ?? {})]))
|
|
62
|
+
if (file)
|
|
63
|
+
add(file);
|
|
64
|
+
const files = projectFiles(root);
|
|
65
|
+
for (const format of manifest.subformats ?? []) {
|
|
66
|
+
add(format.schema);
|
|
67
|
+
for (const file of files)
|
|
68
|
+
if (file.extension === format.ext)
|
|
69
|
+
add(file.path);
|
|
70
|
+
}
|
|
71
|
+
for (const dir of [PROJECT_FONTS_DIR, CODE_LANGUAGES_DIR, ".dooc/editor-tools"]) {
|
|
72
|
+
const path = textfilePath(root, dir);
|
|
73
|
+
if (!existsSync(path))
|
|
74
|
+
continue;
|
|
75
|
+
for (const file of readdirSync(path, { recursive: true, withFileTypes: true }))
|
|
76
|
+
if (file.isFile())
|
|
77
|
+
optional(posix.join(dir, posix.relative(path, file.parentPath), file.name));
|
|
78
|
+
}
|
|
79
|
+
const relative = (source, file) => {
|
|
80
|
+
if (!source || source.startsWith("#") || /^(?:[a-z][\w+.-]*:|\/\/)/i.test(source))
|
|
81
|
+
return;
|
|
82
|
+
if (source.startsWith("/"))
|
|
83
|
+
throw new Error(`Export dependencies must be relative: ${source}`);
|
|
84
|
+
const path = decodeURIComponent(source.split(/[?#]/)[0] ?? "");
|
|
85
|
+
if (!path)
|
|
86
|
+
return;
|
|
87
|
+
const dependency = posix.normalize(posix.join(posix.dirname(file), path));
|
|
88
|
+
add(dependency);
|
|
89
|
+
return dependency;
|
|
90
|
+
};
|
|
91
|
+
const schemas = new Set(manifest.subformats?.map((format) => format.schema));
|
|
92
|
+
for (const file of schemas) {
|
|
93
|
+
const refs = (value) => {
|
|
94
|
+
if (!value || typeof value !== "object")
|
|
95
|
+
return;
|
|
96
|
+
for (const [key, child] of Object.entries(value)) {
|
|
97
|
+
if (key === "$ref" && typeof child === "string") {
|
|
98
|
+
const dependency = relative(child, file);
|
|
99
|
+
if (dependency)
|
|
100
|
+
schemas.add(dependency);
|
|
101
|
+
}
|
|
102
|
+
else
|
|
103
|
+
refs(child);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
refs(JSON.parse(entries.get(file)?.data.toString("utf8") ?? ""));
|
|
107
|
+
}
|
|
108
|
+
// Map iteration visits dependencies appended while walking the graph, once per path.
|
|
109
|
+
for (const entry of entries.values()) {
|
|
110
|
+
const ext = fileExtension(entry.name);
|
|
111
|
+
if (["md", "callouth"].includes(ext)) {
|
|
112
|
+
const tree = parseMdast(entry.data.toString("utf8"));
|
|
113
|
+
const refs = collectRefs(tree);
|
|
114
|
+
for (const file of refs.figures)
|
|
115
|
+
if (!isRemoteTextfile(file))
|
|
116
|
+
add(file);
|
|
117
|
+
for (const file of [...refs.terms, ...refs.textfiles])
|
|
118
|
+
if (!isRemoteTextfile(file)) {
|
|
119
|
+
readTextfile(root, file);
|
|
120
|
+
add(file);
|
|
121
|
+
}
|
|
122
|
+
const reference = (url) => {
|
|
123
|
+
if (/^(?:[a-z][\w+.-]*:|[/#])/i.test(url))
|
|
124
|
+
return;
|
|
125
|
+
add(parseFileReference(url).file.replace(/^(\.\/)+/, ""));
|
|
126
|
+
};
|
|
127
|
+
visit(tree, "image", (node) => reference(node.url));
|
|
128
|
+
for (const link of contentLinks(tree))
|
|
129
|
+
reference(link.url);
|
|
130
|
+
}
|
|
131
|
+
else if (["html", "htm", "svg", "css"].includes(ext))
|
|
132
|
+
assetReferences(entry.data.toString("utf8"), ext, (url, embedded) => {
|
|
133
|
+
if (embedded)
|
|
134
|
+
relative(url, entry.name);
|
|
135
|
+
return url;
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
return [...entries.values()];
|
|
139
|
+
};
|
|
140
|
+
export const exportBundle = (root) => zip(exportEntries(root));
|
package/dist/compiler/figures.js
CHANGED
|
@@ -23,6 +23,7 @@ import { createHash } from "node:crypto";
|
|
|
23
23
|
import { readdirSync, readFileSync } from "node:fs";
|
|
24
24
|
import { dirname } from "node:path";
|
|
25
25
|
import { extractMxfile, isDrawioSvg, normalizeDrawioSvg } from "./drawio.js";
|
|
26
|
+
import { ignoredFiles } from "./ignored-files.js";
|
|
26
27
|
import { textfilePath } from "./textfile.js";
|
|
27
28
|
const EXT_RE = /\.(drawio\.svg|svg|html|png|webp|gif|mp4|webm|jpe?g)$/;
|
|
28
29
|
const FILE_RE = /^[a-z0-9][a-z0-9_-]*\.(drawio\.svg|svg|html|png|webp|gif|mp4|webm|jpe?g)$/;
|
|
@@ -136,8 +137,11 @@ export const loadFigures = (figuresDir, extraFiles = []) => {
|
|
|
136
137
|
// A missing figures/ is valid.
|
|
137
138
|
}
|
|
138
139
|
files = [...new Set([...files.map((file) => `figures/${file}`), ...extraFiles])];
|
|
140
|
+
const ignored = ignoredFiles(dirname(figuresDir), files);
|
|
139
141
|
const keyframeOwner = new Map();
|
|
140
142
|
for (const file of files) {
|
|
143
|
+
if (ignored.has(file))
|
|
144
|
+
continue;
|
|
141
145
|
if (file.startsWith("figures/") &&
|
|
142
146
|
!file.slice(8).includes("/") &&
|
|
143
147
|
!FILE_RE.test(file.slice(8))) {
|
package/dist/compiler/files.js
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import { spawnSync } from "node:child_process";
|
|
2
1
|
import { createHash, randomUUID } from "node:crypto";
|
|
3
2
|
import { existsSync, linkSync, mkdirSync, readdirSync, readFileSync, rmdirSync, rmSync, statSync, unlinkSync, writeFileSync, } from "node:fs";
|
|
4
3
|
import { dirname, join, posix } from "node:path";
|
|
5
4
|
import { visit } from "unist-util-visit";
|
|
6
5
|
import { Document, isSeq, parseDocument } from "yaml";
|
|
7
|
-
import { flattenTree, MANIFEST_FILE } from "../core/content-metadata.js";
|
|
6
|
+
import { flattenTree, MANIFEST_FILE, serializeManifest } from "../core/content-metadata.js";
|
|
8
7
|
import { defaultFilePath, docFile, docIconFile, fileExtension, isDocumentFileName, localFile, MAX_FILE_UPLOAD_BYTES, parseFileLayout, } from "../core/files.js";
|
|
9
8
|
import { codeBlockAt, parseMdast } from "../core/markdown.js";
|
|
10
9
|
import { docPath } from "../core/routing.js";
|
|
11
10
|
import { termFileUrl } from "../core/term-reference.js";
|
|
12
11
|
import { isRemoteTextfile, parseFileReference } from "../core/textfile.js";
|
|
12
|
+
import { assetReferences } from "./asset-references.js";
|
|
13
13
|
import { fileSig } from "./file-cache.js";
|
|
14
|
+
import { ignoredFiles } from "./ignored-files.js";
|
|
14
15
|
import { translationFile, translationFiles, translationLanguages } from "./locales.js";
|
|
15
16
|
import { readManifest } from "./manifest.js";
|
|
16
17
|
import { textfilePath, writeTextfile } from "./textfile.js";
|
|
@@ -18,6 +19,21 @@ import { themeNames, themeVariants } from "./theme-variants.js";
|
|
|
18
19
|
// Project settings and translations have their own layout, outside file restructuring.
|
|
19
20
|
const reserved = new Set([MANIFEST_FILE, "docs.config.mjs", "node_modules", "dist", "locales"]);
|
|
20
21
|
export const managedFile = (file) => localFile(file) && !reserved.has(file.split("/")[0] ?? "");
|
|
22
|
+
const names = new Intl.Collator("en", { numeric: true });
|
|
23
|
+
const compareFilePaths = (a, b) => {
|
|
24
|
+
const left = a.split("/");
|
|
25
|
+
const right = b.split("/");
|
|
26
|
+
for (let i = 0; i < Math.min(left.length, right.length); i++) {
|
|
27
|
+
const first = left[i];
|
|
28
|
+
const second = right[i];
|
|
29
|
+
if (first === second)
|
|
30
|
+
continue;
|
|
31
|
+
return (Number(i < right.length - 1) - Number(i < left.length - 1) ||
|
|
32
|
+
names.compare(first, second) ||
|
|
33
|
+
(first < second ? -1 : 1));
|
|
34
|
+
}
|
|
35
|
+
return left.length - right.length;
|
|
36
|
+
};
|
|
21
37
|
export const completeFilePaths = (root, prefix) => {
|
|
22
38
|
if (typeof prefix !== "string" || prefix.length > 4096)
|
|
23
39
|
throw new Error("Invalid file prefix");
|
|
@@ -28,13 +44,16 @@ export const completeFilePaths = (root, prefix) => {
|
|
|
28
44
|
const path = directory ? textfilePath(root, directory) : root;
|
|
29
45
|
if (!existsSync(path))
|
|
30
46
|
return [];
|
|
31
|
-
|
|
47
|
+
const entries = readdirSync(path, { withFileTypes: true })
|
|
32
48
|
.filter((entry) => !entry.isSymbolicLink() &&
|
|
33
49
|
(entry.isFile() || entry.isDirectory()) &&
|
|
34
50
|
managedFile(directory + entry.name) &&
|
|
35
51
|
(directory + entry.name).toLowerCase().startsWith(prefix.toLowerCase()))
|
|
36
|
-
.map((entry) => directory + entry.name + (entry.isDirectory() ? "/" : ""))
|
|
37
|
-
|
|
52
|
+
.map((entry) => directory + entry.name + (entry.isDirectory() ? "/" : ""));
|
|
53
|
+
const ignored = ignoredFiles(root, entries);
|
|
54
|
+
return entries
|
|
55
|
+
.filter((file) => !ignored.has(file))
|
|
56
|
+
.sort(compareFilePaths)
|
|
38
57
|
.slice(0, 200);
|
|
39
58
|
};
|
|
40
59
|
export const projectFiles = (root) => {
|
|
@@ -55,30 +74,17 @@ export const projectFiles = (root) => {
|
|
|
55
74
|
}
|
|
56
75
|
};
|
|
57
76
|
walk("");
|
|
58
|
-
|
|
77
|
+
const ignored = ignoredFiles(root, result.map((file) => file.path));
|
|
78
|
+
return result
|
|
79
|
+
.filter((file) => !ignored.has(file.path))
|
|
80
|
+
.sort((a, b) => compareFilePaths(a.path, b.path));
|
|
59
81
|
};
|
|
60
82
|
export const filesSnapshot = (root) => {
|
|
61
83
|
const manifest = readManifest(root);
|
|
62
84
|
if (manifest.errors.length)
|
|
63
85
|
throw new Error(manifest.errors.join("\n"));
|
|
64
|
-
|
|
65
|
-
if (files.length) {
|
|
66
|
-
const ignored = spawnSync("git", ["-C", root, "check-ignore", "--stdin", "-z"], {
|
|
67
|
-
input: `${files.map((file) => file.path).join("\0")}\0`,
|
|
68
|
-
encoding: "utf8",
|
|
69
|
-
timeout: 2_000,
|
|
70
|
-
maxBuffer: 5_000_000,
|
|
71
|
-
windowsHide: true,
|
|
72
|
-
});
|
|
73
|
-
if (ignored.status === 0) {
|
|
74
|
-
const paths = new Set(ignored.stdout.split("\0"));
|
|
75
|
-
files = files.filter((file) => !paths.has(file.path));
|
|
76
|
-
}
|
|
77
|
-
}
|
|
86
|
+
const files = projectFiles(root);
|
|
78
87
|
const layout = manifest.files ?? parseFileLayout({});
|
|
79
|
-
const rank = new Map(layout.order.map((path, index) => [path, index]));
|
|
80
|
-
files.sort((a, b) => (rank.get(a.path) ?? Infinity) - (rank.get(b.path) ?? Infinity) ||
|
|
81
|
-
a.path.localeCompare(b.path));
|
|
82
88
|
const hash = createHash("sha256");
|
|
83
89
|
hash.update(existsSync(join(root, MANIFEST_FILE)) ? readFileSync(join(root, MANIFEST_FILE)) : "");
|
|
84
90
|
for (const file of files)
|
|
@@ -92,7 +98,9 @@ export const filesSnapshot = (root) => {
|
|
|
92
98
|
files,
|
|
93
99
|
layout,
|
|
94
100
|
revision: hash.digest("hex"),
|
|
95
|
-
documents: Object.fromEntries(docs
|
|
101
|
+
documents: Object.fromEntries(docs
|
|
102
|
+
.filter((node) => !node.subformat)
|
|
103
|
+
.map(({ id }) => [docFile(id, layout), docPath(docs, id)])),
|
|
96
104
|
};
|
|
97
105
|
};
|
|
98
106
|
export const uploadFile = (root, input) => {
|
|
@@ -236,7 +244,6 @@ export const updateFiles = (root, input) => {
|
|
|
236
244
|
const layout = snapshot.layout;
|
|
237
245
|
const paths = snapshot.files.map((file) => file.path);
|
|
238
246
|
const moves = new Map();
|
|
239
|
-
let order = [...paths];
|
|
240
247
|
if (input.action === "layout") {
|
|
241
248
|
if (typeof input.enabled !== "boolean")
|
|
242
249
|
throw new Error("Invalid layout setting");
|
|
@@ -263,7 +270,7 @@ export const updateFiles = (root, input) => {
|
|
|
263
270
|
throw new Error("File not found");
|
|
264
271
|
if (!localFile(input.name) || input.name.includes("/"))
|
|
265
272
|
throw new Error("Invalid file name");
|
|
266
|
-
if (flattenTree(manifest.tree).some(({ id }) => docFile(id, layout) === input.file) &&
|
|
273
|
+
if (flattenTree(manifest.tree).some(({ id, subformat }) => !subformat && docFile(id, layout) === input.file) &&
|
|
267
274
|
!isDocumentFileName(input.name))
|
|
268
275
|
throw new Error("Enter a file name ending in .md");
|
|
269
276
|
const target = posix.join(posix.dirname(input.file), input.name);
|
|
@@ -291,20 +298,16 @@ export const updateFiles = (root, input) => {
|
|
|
291
298
|
const target = directory
|
|
292
299
|
? `${directory}/${posix.basename(item)}`
|
|
293
300
|
: posix.basename(item);
|
|
301
|
+
if (manifest.defaultSubformatFolder === item ||
|
|
302
|
+
manifest.defaultSubformatFolder?.startsWith(`${item}/`))
|
|
303
|
+
manifest.defaultSubformatFolder =
|
|
304
|
+
target + manifest.defaultSubformatFolder.slice(item.length);
|
|
294
305
|
for (const file of picked.filter((path) => path === item || path.startsWith(`${item}/`))) {
|
|
295
306
|
const next = target + file.slice(item.length);
|
|
296
307
|
if (next !== file)
|
|
297
308
|
moves.set(file, next);
|
|
298
309
|
}
|
|
299
310
|
}
|
|
300
|
-
order = paths.filter((path) => !picked.includes(path));
|
|
301
|
-
const before = input.before;
|
|
302
|
-
const index = before === undefined
|
|
303
|
-
? order.length
|
|
304
|
-
: order.findIndex((path) => path === before || path.startsWith(`${before}/`));
|
|
305
|
-
if (index < 0)
|
|
306
|
-
throw new Error("Invalid drop position");
|
|
307
|
-
order.splice(index, 0, ...picked);
|
|
308
311
|
}
|
|
309
312
|
else
|
|
310
313
|
throw new Error("Unknown file action");
|
|
@@ -329,7 +332,9 @@ export const updateFiles = (root, input) => {
|
|
|
329
332
|
if (existsSync(path))
|
|
330
333
|
throw new Error(`File already exists: ${target}`);
|
|
331
334
|
}
|
|
332
|
-
for (const { id } of flattenTree(manifest.tree)) {
|
|
335
|
+
for (const { id, subformat } of flattenTree(manifest.tree)) {
|
|
336
|
+
if (subformat)
|
|
337
|
+
continue;
|
|
333
338
|
const file = docFile(id, layout);
|
|
334
339
|
if (moves.has(file))
|
|
335
340
|
layout.documents[id] = moves.get(file);
|
|
@@ -339,20 +344,52 @@ export const updateFiles = (root, input) => {
|
|
|
339
344
|
layout.icons[id] = moves.get(icon);
|
|
340
345
|
}
|
|
341
346
|
}
|
|
342
|
-
layout.order = order.map((file) => moves.get(file) ?? file);
|
|
343
347
|
const rawManifest = existsSync(join(root, MANIFEST_FILE))
|
|
344
348
|
? readFileSync(join(root, MANIFEST_FILE), "utf8")
|
|
345
349
|
: null;
|
|
346
350
|
const parsed = parseDocument(rawManifest ?? "docs: []\n");
|
|
347
351
|
const yaml = isSeq(parsed.contents) ? new Document({ docs: parsed.toJS() }) : parsed;
|
|
348
|
-
|
|
352
|
+
manifest.files = layout;
|
|
349
353
|
if (manifest.icon && moves.has(manifest.icon))
|
|
350
|
-
|
|
354
|
+
manifest.icon = moves.get(manifest.icon);
|
|
355
|
+
if (manifest.themeIcons)
|
|
356
|
+
for (const [theme, file] of Object.entries(manifest.themeIcons))
|
|
357
|
+
manifest.themeIcons[theme] = moves.get(file) ?? file;
|
|
358
|
+
for (const subformat of manifest.subformats ?? [])
|
|
359
|
+
subformat.schema = moves.get(subformat.schema) ?? subformat.schema;
|
|
360
|
+
if (manifest.format !== "nested") {
|
|
361
|
+
yaml.set("files", layout);
|
|
362
|
+
if (manifest.icon)
|
|
363
|
+
yaml.set("icon", manifest.icon);
|
|
364
|
+
if (manifest.subformats)
|
|
365
|
+
yaml.set("subformats", manifest.subformats);
|
|
366
|
+
if (manifest.defaultSubformatFolder)
|
|
367
|
+
yaml.set("default_subformat_folder", manifest.defaultSubformatFolder);
|
|
368
|
+
}
|
|
351
369
|
const writes = [];
|
|
352
370
|
const links = new Map(moves);
|
|
353
|
-
for (const file of [...paths, ...translated].filter((file) => /\.(md|callouth)$/.test(file))) {
|
|
371
|
+
for (const file of [...paths, ...translated].filter((file) => /\.(md|callouth|html|htm|svg|css)$/.test(file))) {
|
|
354
372
|
const base = readFileSync(textfilePath(root, file));
|
|
355
|
-
const
|
|
373
|
+
const raw = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(base);
|
|
374
|
+
const next = /\.(md|callouth)$/.test(file)
|
|
375
|
+
? rewriteFileLinks(raw, moves)
|
|
376
|
+
: moves.size
|
|
377
|
+
? assetReferences(raw, fileExtension(file), (url) => {
|
|
378
|
+
if (!url || /^(?:[a-z][\w+.-]*:|[/#])/i.test(url))
|
|
379
|
+
return url;
|
|
380
|
+
const path = decodeURIComponent(url.split(/[?#]/)[0] ?? "");
|
|
381
|
+
if (!path)
|
|
382
|
+
return url;
|
|
383
|
+
const source = posix.normalize(posix.join(posix.dirname(file), path));
|
|
384
|
+
const target = moves.get(source) ?? source;
|
|
385
|
+
const owner = moves.get(file) ?? file;
|
|
386
|
+
if (target === source && posix.dirname(owner) === posix.dirname(file))
|
|
387
|
+
return url;
|
|
388
|
+
const relative = posix.relative(posix.dirname(owner), target);
|
|
389
|
+
return (relative.split("/").map(encodeURIComponent).join("/") +
|
|
390
|
+
url.slice(url.search(/[?#]|$/)));
|
|
391
|
+
})
|
|
392
|
+
: raw;
|
|
356
393
|
const data = Buffer.from(next);
|
|
357
394
|
if (base.equals(data))
|
|
358
395
|
continue;
|
|
@@ -368,7 +405,9 @@ export const updateFiles = (root, input) => {
|
|
|
368
405
|
writes.push({
|
|
369
406
|
file: MANIFEST_FILE,
|
|
370
407
|
base: rawManifest === null ? null : Buffer.from(rawManifest),
|
|
371
|
-
data: Buffer.from(
|
|
408
|
+
data: Buffer.from(manifest.format === "nested"
|
|
409
|
+
? serializeManifest(manifest.tree, manifest)
|
|
410
|
+
: yaml.toString()),
|
|
372
411
|
});
|
|
373
412
|
const originalModes = new Map([...moves.keys()].map((file) => [file, statSync(textfilePath(root, file)).mode]));
|
|
374
413
|
const originals = new Map([...moves.keys()].map((file) => [
|
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");
|
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
|
}>;
|