editlet 0.0.0-stage → 0.1.0-alpha.1
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 +140 -2
- package/dist/admin/config.d.ts +22 -0
- package/dist/admin/config.js +75 -0
- package/dist/admin/dist/assets/RichEditor-BlaCRrEX.css +1 -0
- package/dist/admin/dist/assets/RichEditor-IZKdt6MK.js +42 -0
- package/dist/admin/dist/assets/blank-line-CbGzlqBI.js +1 -0
- package/dist/admin/dist/assets/channel-CljeMIwA.js +1 -0
- package/dist/admin/dist/assets/db-CCMyPgQa.js +1 -0
- package/dist/admin/dist/assets/index-CWoJh4H8.js +257 -0
- package/dist/admin/dist/assets/index-DkRMnD_M.css +1 -0
- package/dist/admin/dist/assets/mdast-util-from-markdown-C79q1_d8.js +3 -0
- package/dist/admin/dist/assets/mdast-util-gfm-cM-_xFJE.js +19 -0
- package/dist/admin/dist/assets/micromark-extension-gfm-Wz6-oszS.js +1 -0
- package/dist/admin/dist/assets/micromark-util-classify-character-BgXaGAja.js +1 -0
- package/dist/admin/dist/assets/micromark-util-html-tag-name-B61T18yC.js +1 -0
- package/dist/admin/dist/assets/rolldown-runtime-XKyaHtl9.js +1 -0
- package/dist/admin/dist/index.html +16 -0
- package/dist/admin/dist/preview.js +1 -0
- package/dist/admin/dist/window.js +1 -0
- package/dist/astro/content.d.ts +21 -0
- package/dist/astro/content.js +235 -0
- package/dist/astro/devServer.js +89 -0
- package/dist/astro/index.d.ts +15 -0
- package/dist/astro/index.js +98 -0
- package/dist/astro/nodeBridge.js +36 -0
- package/dist/astro/shared.js +10 -0
- package/dist/astro/stub/index.html +71 -0
- package/dist/auth/client.d.ts +24 -0
- package/dist/auth/client.js +120 -0
- package/dist/auth/handler.d.ts +16 -0
- package/dist/auth/handler.js +281 -0
- package/dist/auth/protocol.d.ts +25 -0
- package/dist/auth/protocol.js +3 -0
- package/dist/core/assets.d.ts +73 -0
- package/dist/core/assets.js +405 -0
- package/dist/core/devApi.d.ts +28 -0
- package/dist/core/devApi.js +3 -0
- package/dist/core/entryFile.d.ts +13 -0
- package/dist/core/entryFile.js +81 -0
- package/dist/core/fields.js +147 -0
- package/dist/core/generate.d.ts +3 -0
- package/dist/core/generate.js +70 -0
- package/dist/core/gitStyle.d.ts +92 -0
- package/dist/core/gitStyle.js +206 -0
- package/dist/core/index.d.ts +16 -0
- package/dist/core/index.js +16 -0
- package/dist/core/issues.d.ts +10 -0
- package/dist/core/issues.js +10 -0
- package/dist/core/paths.d.ts +31 -0
- package/dist/core/paths.js +114 -0
- package/dist/core/project.js +56 -0
- package/dist/core/references.d.ts +68 -0
- package/dist/core/references.js +103 -0
- package/dist/core/rename.d.ts +60 -0
- package/dist/core/rename.js +133 -0
- package/dist/core/schema.d.ts +117 -0
- package/dist/core/schema.js +299 -0
- package/dist/core/sha.d.ts +2 -0
- package/dist/core/sha.js +13 -0
- package/dist/core/source.d.ts +48 -0
- package/dist/core/source.js +57 -0
- package/dist/core/validate.d.ts +22 -0
- package/dist/core/validate.js +148 -0
- package/dist/core/walk.d.ts +23 -0
- package/dist/core/walk.js +94 -0
- package/dist/core/writes.d.ts +37 -0
- package/dist/core/writes.js +166 -0
- package/dist/github/base64.js +16 -0
- package/dist/github/cache.d.ts +34 -0
- package/dist/github/cache.js +126 -0
- package/dist/github/client.d.ts +38 -0
- package/dist/github/client.js +228 -0
- package/dist/github/codeowners.d.ts +23 -0
- package/dist/github/codeowners.js +96 -0
- package/dist/github/commitReader.d.ts +25 -0
- package/dist/github/commitReader.js +38 -0
- package/dist/github/deploy.d.ts +15 -0
- package/dist/github/deploy.js +33 -0
- package/dist/github/index.d.ts +9 -0
- package/dist/github/index.js +8 -0
- package/dist/github/repo.d.ts +95 -0
- package/dist/github/repo.js +375 -0
- package/dist/github/source.d.ts +115 -0
- package/dist/github/source.js +558 -0
- package/dist/loader/index.d.ts +27 -0
- package/dist/loader/index.js +151 -0
- package/dist/loader/protocol.d.ts +95 -0
- package/dist/loader/protocol.js +120 -0
- package/dist/loader/window/geometry.js +9 -0
- package/dist/next/EditletLoader.d.ts +5 -0
- package/dist/next/EditletLoader.js +33 -0
- package/dist/next/assetUrl.d.ts +9 -0
- package/dist/next/assetUrl.js +13 -0
- package/dist/next/buildGuard.js +26 -0
- package/dist/next/config.d.ts +18 -0
- package/dist/next/config.js +255 -0
- package/dist/next/devServer.js +70 -0
- package/dist/next/editletField.d.ts +7 -0
- package/dist/next/editletField.js +7 -0
- package/dist/next/index.d.ts +8 -0
- package/dist/next/index.js +6 -0
- package/dist/next/preview.d.ts +18 -0
- package/dist/next/preview.js +100 -0
- package/dist/next/previewCookie.d.ts +5 -0
- package/dist/next/previewCookie.js +72 -0
- package/dist/next/previewEnv.js +25 -0
- package/dist/next/previewRef.d.ts +8 -0
- package/dist/next/previewRef.js +17 -0
- package/dist/next/publicDir.js +65 -0
- package/dist/next/reader.d.ts +49 -0
- package/dist/next/reader.js +161 -0
- package/dist/next/stub/index.html +56 -0
- package/dist/node/devHandler.d.ts +6 -0
- package/dist/node/devHandler.js +430 -0
- package/dist/node/fs.js +37 -0
- package/dist/node/index.d.ts +3 -0
- package/dist/node/index.js +3 -0
- package/dist/node/project.d.ts +12 -0
- package/dist/node/project.js +43 -0
- package/package.json +187 -4
- package/skills/README.md +84 -0
- package/skills/editlet/SKILL.md +119 -0
- package/skills/editlet/references/content-format.md +191 -0
- package/skills/editlet/references/preview.md +75 -0
- package/skills/editlet/references/publishing.md +137 -0
- package/skills/editlet/references/schema-changes.md +127 -0
- package/skills/editlet/scripts/guard.mjs +1061 -0
- package/skills/editlet/scripts/link.mjs +193 -0
- package/skills/editlet/scripts/refs.mjs +1480 -0
- package/skills/editlet/scripts/schema.mjs +1340 -0
- package/skills/editlet/scripts/style.mjs +657 -0
- package/skills/editlet/scripts/types.mjs +525 -0
- package/skills/editlet/scripts/validate.mjs +724 -0
- package/skills/editlet-migrate/SKILL.md +92 -0
- package/skills/editlet-migrate/references/mapping.md +73 -0
- package/skills/editlet-migrate/references/sources.md +70 -0
- package/skills/editlet-setup/SKILL.md +108 -0
- package/skills/editlet-setup/references/astro.md +83 -0
- package/skills/editlet-setup/references/doctor.md +89 -0
- package/skills/editlet-setup/references/next.md +140 -0
- package/skills/editlet-setup/references/preview.md +54 -0
- package/skills/editlet-setup/references/production.md +153 -0
- package/skills/editlet-setup/references/roles.md +238 -0
- package/skills/editlet-setup/references/skills.md +99 -0
- package/skills/editlet-setup/scripts/doctor.mjs +1378 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { BLOCK_KEY } from "./schema.js";
|
|
2
|
+
import { buildDataSchema } from "./validate.js";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
// core/generate.ts
|
|
5
|
+
/**
|
|
6
|
+
* JSON Schema and TypeScript declarations generated from the content schema.
|
|
7
|
+
*
|
|
8
|
+
* The generated `.d.ts` describes the `normalizeData` output of a valid entry: optional fields are absent rather than
|
|
9
|
+
* `null` or `''`. Adapters and the admin read entry data through `normalizeData` before handing it out as these
|
|
10
|
+
* types. `toJsonSchema` describes the raw frontmatter that validation accepts (where `null` / `''` are allowed).
|
|
11
|
+
*/
|
|
12
|
+
function toJsonSchema(collection) {
|
|
13
|
+
return z.toJSONSchema(buildDataSchema(collection), { target: "draft-2020-12" });
|
|
14
|
+
}
|
|
15
|
+
const pascal = (key) => key.charAt(0).toUpperCase() + key.slice(1);
|
|
16
|
+
function array(type) {
|
|
17
|
+
return type.includes(" | ") ? `(${type})[]` : `${type}[]`;
|
|
18
|
+
}
|
|
19
|
+
function fieldType(field) {
|
|
20
|
+
switch (field.type) {
|
|
21
|
+
case "string": return field.list ? "string[]" : "string";
|
|
22
|
+
case "text":
|
|
23
|
+
case "url":
|
|
24
|
+
case "color":
|
|
25
|
+
case "datetime":
|
|
26
|
+
case "markdown": return "string";
|
|
27
|
+
case "boolean": return "boolean";
|
|
28
|
+
case "number": return "number";
|
|
29
|
+
case "json": return "unknown";
|
|
30
|
+
case "select": {
|
|
31
|
+
const union = field.options.map((o) => `'${o.value.replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`).join(" | ");
|
|
32
|
+
return field.multiple ? array(union) : union;
|
|
33
|
+
}
|
|
34
|
+
case "image": return "{ src: string; alt: string }";
|
|
35
|
+
case "reference": {
|
|
36
|
+
const union = field.collections.map((c) => `\`${c}/\${string}\``).join(" | ");
|
|
37
|
+
return field.multiple ? array(union) : union;
|
|
38
|
+
}
|
|
39
|
+
case "object": {
|
|
40
|
+
const type = objectType(field.fields);
|
|
41
|
+
return field.list ? `${type}[]` : type;
|
|
42
|
+
}
|
|
43
|
+
case "blocks": return array(Object.entries(field.blocks).map(([kind, block]) => objectType(block.fields, `${BLOCK_KEY}: '${quote(kind)}'`)).join(" | "));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const quote = (v) => v.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
|
|
47
|
+
/** Inline type of nested fields: `{ title: string; image?: … }`; a nested `markdown` is a string. */
|
|
48
|
+
function objectType(fields, first) {
|
|
49
|
+
const parts = first === void 0 ? [] : [first];
|
|
50
|
+
for (const [key, field] of Object.entries(fields)) parts.push(`${key}${field.required ? "" : "?"}: ${fieldType(field)}`);
|
|
51
|
+
return `{ ${parts.join("; ")} }`;
|
|
52
|
+
}
|
|
53
|
+
function generateTypes(schema) {
|
|
54
|
+
const lines = ["// Generated by editlet from the content schema. Do not edit."];
|
|
55
|
+
for (const [key, collection] of Object.entries(schema.collections)) {
|
|
56
|
+
const name = pascal(key);
|
|
57
|
+
lines.push("", `export interface ${name}Data {`);
|
|
58
|
+
for (const [fieldKey, field] of Object.entries(collection.fields)) {
|
|
59
|
+
if (field.type === "markdown") continue;
|
|
60
|
+
lines.push(` ${fieldKey}${field.required ? "" : "?"}: ${fieldType(field)};`);
|
|
61
|
+
}
|
|
62
|
+
lines.push(" draft?: boolean;", "}");
|
|
63
|
+
lines.push("", `export interface ${name}Entry {`, ` collection: '${key}';`, " slug: string;", ` data: ${name}Data;`, " body: string;", "}");
|
|
64
|
+
}
|
|
65
|
+
lines.push("", "export interface EntryMap {");
|
|
66
|
+
for (const key of Object.keys(schema.collections)) lines.push(` ${key}: ${pascal(key)}Entry;`);
|
|
67
|
+
lines.push("}");
|
|
68
|
+
return `${lines.join("\n")}\n`;
|
|
69
|
+
}
|
|
70
|
+
export { generateTypes, toJsonSchema };
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { GitConfig, Schema } from "./schema.js";
|
|
2
|
+
/** How branches, commits and pull requests are named: `git` of the schema with every default filled in. */
|
|
3
|
+
export interface GitStyle {
|
|
4
|
+
branchPrefix: string;
|
|
5
|
+
commitMessage: string;
|
|
6
|
+
pullRequest: {
|
|
7
|
+
title: string;
|
|
8
|
+
labels: string[];
|
|
9
|
+
draft: boolean;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
export declare const DEFAULT_BRANCH_PREFIX = "editlet/";
|
|
13
|
+
export declare const DEFAULT_COMMIT_MESSAGE = "{Action} {target}";
|
|
14
|
+
export declare const DEFAULT_PULL_REQUEST_TITLE = "Update {branch}";
|
|
15
|
+
export declare const DEFAULT_LABELS: readonly string[];
|
|
16
|
+
/** Git trailer key that tells who made a commit: `Editlet-Source: admin` or `Editlet-Source: agent`. */
|
|
17
|
+
export declare const SOURCE_TRAILER = "Editlet-Source";
|
|
18
|
+
/** Editlet owns only the text between these markers in a pull request body. */
|
|
19
|
+
export declare const PULL_REQUEST_BODY_START = "<!-- editlet:start -->";
|
|
20
|
+
export declare const PULL_REQUEST_BODY_END = "<!-- editlet:end -->";
|
|
21
|
+
/** The style of a schema's `git` section; `null` (no or broken schema) gives the defaults. */
|
|
22
|
+
export declare function resolveGitStyle(git?: GitConfig | null): GitStyle;
|
|
23
|
+
/** `<prefix><name>` where the whole name is valid ref segments and `<name>` is not empty. */
|
|
24
|
+
export declare function isEditletBranchName(name: string, prefix?: string): boolean;
|
|
25
|
+
export type CommitAction = 'create' | 'update' | 'delete' | 'upload' | 'rename';
|
|
26
|
+
/** What a commit is about; fills the `commitMessage` placeholders. */
|
|
27
|
+
export interface CommitSubject {
|
|
28
|
+
action: CommitAction;
|
|
29
|
+
collection: string;
|
|
30
|
+
slug: string;
|
|
31
|
+
target: string;
|
|
32
|
+
}
|
|
33
|
+
/** One entry. */
|
|
34
|
+
export declare function entrySubject(action: CommitAction, collection: string, slug: string): CommitSubject;
|
|
35
|
+
/** An entry's new slug (its references and images move in the same commit): `Rename post/old → post/new`. */
|
|
36
|
+
export declare function renameSubject(collection: string, from: string, to: string): CommitSubject;
|
|
37
|
+
/** Several entries in one commit (for example alt text in every entry that uses an image). */
|
|
38
|
+
export declare function entriesSubject(action: CommitAction, entries: {
|
|
39
|
+
collection: string;
|
|
40
|
+
slug: string;
|
|
41
|
+
}[]): CommitSubject;
|
|
42
|
+
/**
|
|
43
|
+
* Files that are not entries (images). The collection is the folder under `contentDir` when it is a collection key,
|
|
44
|
+
* else `media`; one file is named, several are counted.
|
|
45
|
+
*/
|
|
46
|
+
export declare function filesSubject(schema: Schema, action: CommitAction, paths: string[]): CommitSubject;
|
|
47
|
+
/** The schema file, alone or with the entries it rewrites. */
|
|
48
|
+
export declare function schemaSubject(entries?: number): CommitSubject;
|
|
49
|
+
type FileStatus = 'added' | 'modified' | 'removed';
|
|
50
|
+
/**
|
|
51
|
+
* The subject of a commit from its changed files (for example `git diff --cached --name-status`): the schema with the
|
|
52
|
+
* entries it rewrites, else the entries, else the other files (images). One action when every file agrees, else
|
|
53
|
+
* `update`; new images are an `upload`.
|
|
54
|
+
*/
|
|
55
|
+
export declare function changesSubject(schema: Schema, schemaPath: string, files: {
|
|
56
|
+
path: string;
|
|
57
|
+
status: FileStatus;
|
|
58
|
+
}[]): CommitSubject;
|
|
59
|
+
export declare function formatCommitMessage(style: GitStyle, subject: CommitSubject): string;
|
|
60
|
+
/** The message plus a blank line and `Editlet-Source: <source>`. */
|
|
61
|
+
export declare function withTrailer(message: string, source: 'admin' | 'agent'): string;
|
|
62
|
+
/** The branch name without the prefix (the `?editlet=branch:<name>` value). */
|
|
63
|
+
export declare function branchShortName(style: GitStyle, branch: string): string;
|
|
64
|
+
export declare function formatPullRequestTitle(style: GitStyle, values: {
|
|
65
|
+
branch: string;
|
|
66
|
+
login: string;
|
|
67
|
+
}): string;
|
|
68
|
+
/** One line of the pull request body: an entry, or another CMS file by path. */
|
|
69
|
+
export interface PullRequestChange {
|
|
70
|
+
/** The collection label (`Post`), or `null` for a file that is not an entry. */
|
|
71
|
+
label: string | null;
|
|
72
|
+
/** Slug for an entry, path for another file. */
|
|
73
|
+
name: string;
|
|
74
|
+
status: 'created' | 'updated' | 'deleted';
|
|
75
|
+
}
|
|
76
|
+
/** The changed CMS files of a branch as body lines: entries with their collection label first, then other files. */
|
|
77
|
+
export declare function describeChanges(schema: Schema | null, files: {
|
|
78
|
+
path: string;
|
|
79
|
+
status: 'added' | 'modified' | 'removed';
|
|
80
|
+
}[]): PullRequestChange[];
|
|
81
|
+
/** The Editlet part of a pull request body, between the markers. */
|
|
82
|
+
export declare function pullRequestBlock(input: {
|
|
83
|
+
login: string;
|
|
84
|
+
changes: PullRequestChange[];
|
|
85
|
+
reviewUrl: string | null;
|
|
86
|
+
}): string;
|
|
87
|
+
/**
|
|
88
|
+
* `current` with its Editlet part replaced by `block`. An empty body becomes `block`. `null` when the body has no
|
|
89
|
+
* complete pair of markers (someone removed them: the body is theirs now).
|
|
90
|
+
*/
|
|
91
|
+
export declare function mergePullRequestBody(current: string | null, block: string): string | null;
|
|
92
|
+
export {};
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { parseEntryPath } from "./paths.js";
|
|
2
|
+
import { isRefPath } from "./schema.js";
|
|
3
|
+
// core/gitStyle.ts
|
|
4
|
+
const DEFAULT_BRANCH_PREFIX = "editlet/";
|
|
5
|
+
const DEFAULT_COMMIT_MESSAGE = "{Action} {target}";
|
|
6
|
+
const DEFAULT_PULL_REQUEST_TITLE = "Update {branch}";
|
|
7
|
+
const DEFAULT_LABELS = ["editlet"];
|
|
8
|
+
/** Git trailer key that tells who made a commit: `Editlet-Source: admin` or `Editlet-Source: agent`. */
|
|
9
|
+
const SOURCE_TRAILER = "Editlet-Source";
|
|
10
|
+
/** Editlet owns only the text between these markers in a pull request body. */
|
|
11
|
+
const PULL_REQUEST_BODY_START = "<!-- editlet:start -->";
|
|
12
|
+
const PULL_REQUEST_BODY_END = "<!-- editlet:end -->";
|
|
13
|
+
/** The style of a schema's `git` section; `null` (no or broken schema) gives the defaults. */
|
|
14
|
+
function resolveGitStyle(git) {
|
|
15
|
+
return {
|
|
16
|
+
branchPrefix: git?.branchPrefix ?? "editlet/",
|
|
17
|
+
commitMessage: git?.commitMessage ?? "{Action} {target}",
|
|
18
|
+
pullRequest: {
|
|
19
|
+
title: git?.pullRequest?.title ?? "Update {branch}",
|
|
20
|
+
labels: [...git?.pullRequest?.labels ?? DEFAULT_LABELS],
|
|
21
|
+
draft: git?.pullRequest?.draft ?? false
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/** `<prefix><name>` where the whole name is valid ref segments and `<name>` is not empty. */
|
|
26
|
+
function isEditletBranchName(name, prefix = DEFAULT_BRANCH_PREFIX) {
|
|
27
|
+
return name.length > prefix.length && name.startsWith(prefix) && isRefPath(name);
|
|
28
|
+
}
|
|
29
|
+
/** One entry. */
|
|
30
|
+
function entrySubject(action, collection, slug) {
|
|
31
|
+
return {
|
|
32
|
+
action,
|
|
33
|
+
collection,
|
|
34
|
+
slug,
|
|
35
|
+
target: `${collection}/${slug}`
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/** An entry's new slug (its references and images move in the same commit): `Rename post/old → post/new`. */
|
|
39
|
+
function renameSubject(collection, from, to) {
|
|
40
|
+
return {
|
|
41
|
+
action: "rename",
|
|
42
|
+
collection,
|
|
43
|
+
slug: to,
|
|
44
|
+
target: `${collection}/${from} → ${collection}/${to}`
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/** Several entries in one commit (for example alt text in every entry that uses an image). */
|
|
48
|
+
function entriesSubject(action, entries) {
|
|
49
|
+
const [first, ...rest] = entries;
|
|
50
|
+
if (!first) return {
|
|
51
|
+
action,
|
|
52
|
+
collection: "content",
|
|
53
|
+
slug: "",
|
|
54
|
+
target: "content"
|
|
55
|
+
};
|
|
56
|
+
if (rest.length === 0) return entrySubject(action, first.collection, first.slug);
|
|
57
|
+
const collection = rest.every((e) => e.collection === first.collection) ? first.collection : "content";
|
|
58
|
+
const more = ` +${rest.length}`;
|
|
59
|
+
return {
|
|
60
|
+
action,
|
|
61
|
+
collection,
|
|
62
|
+
slug: `${first.slug}${more}`,
|
|
63
|
+
target: `${first.collection}/${first.slug}${more}`
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Files that are not entries (images). The collection is the folder under `contentDir` when it is a collection key,
|
|
68
|
+
* else `media`; one file is named, several are counted.
|
|
69
|
+
*/
|
|
70
|
+
function filesSubject(schema, action, paths) {
|
|
71
|
+
const collectionOf = (path) => {
|
|
72
|
+
const rel = path.startsWith(`${schema.contentDir}/`) ? path.slice(schema.contentDir.length + 1) : "";
|
|
73
|
+
const top = rel.split("/")[0] ?? "";
|
|
74
|
+
return rel.includes("/") && Object.hasOwn(schema.collections, top) ? top : "media";
|
|
75
|
+
};
|
|
76
|
+
const collections = new Set(paths.map(collectionOf));
|
|
77
|
+
const collection = collections.size === 1 ? [...collections][0] : "media";
|
|
78
|
+
if (paths.length === 1) {
|
|
79
|
+
const name = paths[0].split("/").pop();
|
|
80
|
+
return {
|
|
81
|
+
action,
|
|
82
|
+
collection,
|
|
83
|
+
slug: name,
|
|
84
|
+
target: `${collection}/${name}`
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
const count = `${paths.length} images`;
|
|
88
|
+
return {
|
|
89
|
+
action,
|
|
90
|
+
collection,
|
|
91
|
+
slug: count,
|
|
92
|
+
target: count
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/** The schema file, alone or with the entries it rewrites. */
|
|
96
|
+
function schemaSubject(entries = 0) {
|
|
97
|
+
return {
|
|
98
|
+
action: "update",
|
|
99
|
+
collection: "schema",
|
|
100
|
+
slug: `schema.json${entries > 0 ? ` +${entries}` : ""}`,
|
|
101
|
+
target: entries > 0 ? `schema +${entries} entries` : "schema"
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
const ACTION_OF = {
|
|
105
|
+
added: "create",
|
|
106
|
+
modified: "update",
|
|
107
|
+
removed: "delete"
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* The subject of a commit from its changed files (for example `git diff --cached --name-status`): the schema with the
|
|
111
|
+
* entries it rewrites, else the entries, else the other files (images). One action when every file agrees, else
|
|
112
|
+
* `update`; new images are an `upload`.
|
|
113
|
+
*/
|
|
114
|
+
function changesSubject(schema, schemaPath, files) {
|
|
115
|
+
const entries = files.flatMap((f) => {
|
|
116
|
+
const ref = parseEntryPath(schema, f.path);
|
|
117
|
+
return ref ? [{
|
|
118
|
+
...ref,
|
|
119
|
+
status: f.status
|
|
120
|
+
}] : [];
|
|
121
|
+
});
|
|
122
|
+
if (files.some((f) => f.path === schemaPath)) return schemaSubject(entries.length);
|
|
123
|
+
const one = (statuses) => statuses.every((s) => s === statuses[0]) ? statuses[0] ?? null : null;
|
|
124
|
+
if (entries.length > 0) {
|
|
125
|
+
const status = one(entries.map((e) => e.status));
|
|
126
|
+
return entriesSubject(status ? ACTION_OF[status] : "update", entries);
|
|
127
|
+
}
|
|
128
|
+
const status = one(files.map((f) => f.status));
|
|
129
|
+
return filesSubject(schema, status === "added" ? "upload" : status ? ACTION_OF[status] : "update", files.map((f) => f.path));
|
|
130
|
+
}
|
|
131
|
+
function fill(template, values) {
|
|
132
|
+
return template.replace(/\{([A-Za-z]+)\}/g, (whole, key) => Object.hasOwn(values, key) ? values[key] : whole).replace(/\s+/g, " ").trim();
|
|
133
|
+
}
|
|
134
|
+
function formatCommitMessage(style, subject) {
|
|
135
|
+
const Action = subject.action.charAt(0).toUpperCase() + subject.action.slice(1);
|
|
136
|
+
return fill(style.commitMessage, {
|
|
137
|
+
...subject,
|
|
138
|
+
Action
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
/** The message plus a blank line and `Editlet-Source: <source>`. */
|
|
142
|
+
function withTrailer(message, source) {
|
|
143
|
+
return `${message}\n\n${SOURCE_TRAILER}: ${source}`;
|
|
144
|
+
}
|
|
145
|
+
/** The branch name without the prefix (the `?editlet=branch:<name>` value). */
|
|
146
|
+
function branchShortName(style, branch) {
|
|
147
|
+
return branch.startsWith(style.branchPrefix) ? branch.slice(style.branchPrefix.length) : branch;
|
|
148
|
+
}
|
|
149
|
+
function formatPullRequestTitle(style, values) {
|
|
150
|
+
return fill(style.pullRequest.title, {
|
|
151
|
+
...values,
|
|
152
|
+
name: branchShortName(style, values.branch)
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
const CHANGE_STATUS = {
|
|
156
|
+
added: "created",
|
|
157
|
+
modified: "updated",
|
|
158
|
+
removed: "deleted"
|
|
159
|
+
};
|
|
160
|
+
/** The changed CMS files of a branch as body lines: entries with their collection label first, then other files. */
|
|
161
|
+
function describeChanges(schema, files) {
|
|
162
|
+
const entries = [];
|
|
163
|
+
const others = [];
|
|
164
|
+
for (const file of files) {
|
|
165
|
+
const status = CHANGE_STATUS[file.status];
|
|
166
|
+
const ref = schema ? parseEntryPath(schema, file.path) : null;
|
|
167
|
+
if (ref) entries.push({
|
|
168
|
+
label: schema.collections[ref.collection].label,
|
|
169
|
+
name: ref.slug,
|
|
170
|
+
status
|
|
171
|
+
});
|
|
172
|
+
else others.push({
|
|
173
|
+
label: null,
|
|
174
|
+
name: file.path,
|
|
175
|
+
status
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
return [...entries, ...others];
|
|
179
|
+
}
|
|
180
|
+
const MAX_BODY_LINES = 30;
|
|
181
|
+
/** The Editlet part of a pull request body, between the markers. */
|
|
182
|
+
function pullRequestBlock(input) {
|
|
183
|
+
const lines = input.changes.slice(0, MAX_BODY_LINES).map((c) => `- ${c.label === null ? `\`${c.name}\`` : `${c.label} · ${c.name}`} — ${c.status}`);
|
|
184
|
+
if (input.changes.length > MAX_BODY_LINES) lines.push(`- …and ${input.changes.length - MAX_BODY_LINES} more`);
|
|
185
|
+
return [
|
|
186
|
+
PULL_REQUEST_BODY_START,
|
|
187
|
+
`Opened in Editlet by @${input.login}.`,
|
|
188
|
+
"",
|
|
189
|
+
"**Changes**",
|
|
190
|
+
...lines.length > 0 ? lines : ["- none yet"],
|
|
191
|
+
...input.reviewUrl ? ["", `Review on the site: ${input.reviewUrl}`] : [],
|
|
192
|
+
PULL_REQUEST_BODY_END
|
|
193
|
+
].join("\n");
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* `current` with its Editlet part replaced by `block`. An empty body becomes `block`. `null` when the body has no
|
|
197
|
+
* complete pair of markers (someone removed them: the body is theirs now).
|
|
198
|
+
*/
|
|
199
|
+
function mergePullRequestBody(current, block) {
|
|
200
|
+
if (current === null || current.trim() === "") return block;
|
|
201
|
+
const start = current.indexOf(PULL_REQUEST_BODY_START);
|
|
202
|
+
const end = current.indexOf(PULL_REQUEST_BODY_END, start);
|
|
203
|
+
if (start < 0 || end < 0) return null;
|
|
204
|
+
return current.slice(0, start) + block + current.slice(end + 20);
|
|
205
|
+
}
|
|
206
|
+
export { DEFAULT_BRANCH_PREFIX, DEFAULT_COMMIT_MESSAGE, DEFAULT_LABELS, DEFAULT_PULL_REQUEST_TITLE, PULL_REQUEST_BODY_END, PULL_REQUEST_BODY_START, SOURCE_TRAILER, branchShortName, changesSubject, describeChanges, entriesSubject, entrySubject, filesSubject, formatCommitMessage, formatPullRequestTitle, isEditletBranchName, mergePullRequestBody, pullRequestBlock, renameSubject, resolveGitStyle, schemaSubject, withTrailer };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { EditletError, Issue } from "./issues.js";
|
|
2
|
+
import { BLOCK_KEY, BlockDef, COMMIT_PLACEHOLDERS, Collection, Field, FieldType, GitConfig, Schema, TITLE_PLACEHOLDERS, isRefPath, loadSchema, markdownFieldKey, parseSchemaText } from "./schema.js";
|
|
3
|
+
import { EntryFile, decodeUtf8, parseEntryFile, serializeEntryFile } from "./entryFile.js";
|
|
4
|
+
import { IMAGE_EXTENSIONS, SLUG_RE, entryFilePath, entryPath, isCanonicalPath, isHiddenContentPath, isImagePath, isValidSlug, parseEntryPath, parseRefKey, refKey, relativeAssetSrc, resolveAssetPath, slugify } from "./paths.js";
|
|
5
|
+
import { Entry, ValidateContext, buildDataSchema, initialData, normalizeData, validateEntry } from "./validate.js";
|
|
6
|
+
import { generateTypes, toJsonSchema } from "./generate.js";
|
|
7
|
+
import { ContentSource, FileActor, FileChange, FileEvent, FileHistory, FileRef, createMemorySource } from "./source.js";
|
|
8
|
+
import { gitBlobSha } from "./sha.js";
|
|
9
|
+
import { ChangeContext, isWritablePath, validateChanges, validateCommit } from "./writes.js";
|
|
10
|
+
import { DEV_API_BASE, DevCommitChange, DevCommitRequest, DevErrorResponse, DevFilesResponse, DevGitResponse } from "./devApi.js";
|
|
11
|
+
import { BodyImage, BodyImageEdit, ImageUse, ImageUseEdit, TextRange, applyImageEdits, editBodyImages, findBodyImages, findImageRefs } from "./assets.js";
|
|
12
|
+
import { CommitAction, CommitSubject, DEFAULT_BRANCH_PREFIX, DEFAULT_COMMIT_MESSAGE, DEFAULT_LABELS, DEFAULT_PULL_REQUEST_TITLE, GitStyle, PULL_REQUEST_BODY_END, PULL_REQUEST_BODY_START, PullRequestChange, SOURCE_TRAILER, branchShortName, changesSubject, describeChanges, entriesSubject, entrySubject, filesSubject, formatCommitMessage, formatPullRequestTitle, isEditletBranchName, mergePullRequestBody, pullRequestBlock, renameSubject, resolveGitStyle, schemaSubject, withTrailer } from "./gitStyle.js";
|
|
13
|
+
import { MAX_REFERENCE_DEPTH, ReferenceField, Referrer, Resolved, ResolvedData, canReference, checkReferenceDepth, createResolver, entryReferences, findReferrers, referenceFields, replaceReference } from "./references.js";
|
|
14
|
+
import { FieldPath, allFields, blockFields, mapFields, visitFields } from "./walk.js";
|
|
15
|
+
import { EntryRewrite, PlanEntry, RemovePlan, RenamePlan, planRemove, planRename } from "./rename.js";
|
|
16
|
+
export { BLOCK_KEY, BlockDef, BodyImage, BodyImageEdit, COMMIT_PLACEHOLDERS, ChangeContext, Collection, CommitAction, CommitSubject, ContentSource, DEFAULT_BRANCH_PREFIX, DEFAULT_COMMIT_MESSAGE, DEFAULT_LABELS, DEFAULT_PULL_REQUEST_TITLE, DEV_API_BASE, DevCommitChange, DevCommitRequest, DevErrorResponse, DevFilesResponse, DevGitResponse, EditletError, Entry, EntryFile, EntryRewrite, Field, FieldPath, FieldType, FileActor, FileChange, FileEvent, FileHistory, FileRef, GitConfig, GitStyle, IMAGE_EXTENSIONS, ImageUse, ImageUseEdit, Issue, MAX_REFERENCE_DEPTH, PULL_REQUEST_BODY_END, PULL_REQUEST_BODY_START, PlanEntry, PullRequestChange, ReferenceField, Referrer, RemovePlan, RenamePlan, Resolved, ResolvedData, SLUG_RE, SOURCE_TRAILER, Schema, TITLE_PLACEHOLDERS, TextRange, ValidateContext, allFields, applyImageEdits, blockFields, branchShortName, buildDataSchema, canReference, changesSubject, checkReferenceDepth, createMemorySource, createResolver, decodeUtf8, describeChanges, editBodyImages, entriesSubject, entryFilePath, entryPath, entryReferences, entrySubject, filesSubject, findBodyImages, findImageRefs, findReferrers, formatCommitMessage, formatPullRequestTitle, generateTypes, gitBlobSha, initialData, isCanonicalPath, isEditletBranchName, isHiddenContentPath, isImagePath, isRefPath, isValidSlug, isWritablePath, loadSchema, mapFields, markdownFieldKey, mergePullRequestBody, normalizeData, parseEntryFile, parseEntryPath, parseRefKey, parseSchemaText, planRemove, planRename, pullRequestBlock, refKey, referenceFields, relativeAssetSrc, renameSubject, replaceReference, resolveAssetPath, resolveGitStyle, schemaSubject, serializeEntryFile, slugify, toJsonSchema, validateChanges, validateCommit, validateEntry, visitFields, withTrailer };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { EditletError } from "./issues.js";
|
|
2
|
+
import { BLOCK_KEY, COMMIT_PLACEHOLDERS, TITLE_PLACEHOLDERS, isRefPath, loadSchema, markdownFieldKey, parseSchemaText } from "./schema.js";
|
|
3
|
+
import { decodeUtf8, parseEntryFile, serializeEntryFile } from "./entryFile.js";
|
|
4
|
+
import { IMAGE_EXTENSIONS, SLUG_RE, entryFilePath, entryPath, isCanonicalPath, isHiddenContentPath, isImagePath, isValidSlug, parseEntryPath, parseRefKey, refKey, relativeAssetSrc, resolveAssetPath, slugify } from "./paths.js";
|
|
5
|
+
import { buildDataSchema, initialData, normalizeData, validateEntry } from "./validate.js";
|
|
6
|
+
import { generateTypes, toJsonSchema } from "./generate.js";
|
|
7
|
+
import { createMemorySource } from "./source.js";
|
|
8
|
+
import { gitBlobSha } from "./sha.js";
|
|
9
|
+
import { isWritablePath, validateChanges, validateCommit } from "./writes.js";
|
|
10
|
+
import { DEV_API_BASE } from "./devApi.js";
|
|
11
|
+
import { applyImageEdits, editBodyImages, findBodyImages, findImageRefs } from "./assets.js";
|
|
12
|
+
import { DEFAULT_BRANCH_PREFIX, DEFAULT_COMMIT_MESSAGE, DEFAULT_LABELS, DEFAULT_PULL_REQUEST_TITLE, PULL_REQUEST_BODY_END, PULL_REQUEST_BODY_START, SOURCE_TRAILER, branchShortName, changesSubject, describeChanges, entriesSubject, entrySubject, filesSubject, formatCommitMessage, formatPullRequestTitle, isEditletBranchName, mergePullRequestBody, pullRequestBlock, renameSubject, resolveGitStyle, schemaSubject, withTrailer } from "./gitStyle.js";
|
|
13
|
+
import { MAX_REFERENCE_DEPTH, canReference, checkReferenceDepth, createResolver, entryReferences, findReferrers, referenceFields, replaceReference } from "./references.js";
|
|
14
|
+
import { allFields, blockFields, mapFields, visitFields } from "./walk.js";
|
|
15
|
+
import { planRemove, planRename } from "./rename.js";
|
|
16
|
+
export { BLOCK_KEY, COMMIT_PLACEHOLDERS, DEFAULT_BRANCH_PREFIX, DEFAULT_COMMIT_MESSAGE, DEFAULT_LABELS, DEFAULT_PULL_REQUEST_TITLE, DEV_API_BASE, EditletError, IMAGE_EXTENSIONS, MAX_REFERENCE_DEPTH, PULL_REQUEST_BODY_END, PULL_REQUEST_BODY_START, SLUG_RE, SOURCE_TRAILER, TITLE_PLACEHOLDERS, allFields, applyImageEdits, blockFields, branchShortName, buildDataSchema, canReference, changesSubject, checkReferenceDepth, createMemorySource, createResolver, decodeUtf8, describeChanges, editBodyImages, entriesSubject, entryFilePath, entryPath, entryReferences, entrySubject, filesSubject, findBodyImages, findImageRefs, findReferrers, formatCommitMessage, formatPullRequestTitle, generateTypes, gitBlobSha, initialData, isCanonicalPath, isEditletBranchName, isHiddenContentPath, isImagePath, isRefPath, isValidSlug, isWritablePath, loadSchema, mapFields, markdownFieldKey, mergePullRequestBody, normalizeData, parseEntryFile, parseEntryPath, parseRefKey, parseSchemaText, planRemove, planRename, pullRequestBlock, refKey, referenceFields, relativeAssetSrc, renameSubject, replaceReference, resolveAssetPath, resolveGitStyle, schemaSubject, serializeEntryFile, slugify, toJsonSchema, validateChanges, validateCommit, validateEntry, visitFields, withTrailer };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface Issue {
|
|
2
|
+
file?: string;
|
|
3
|
+
/** Dot path inside the file ('title', 'tags.2'); '' means the whole file. */
|
|
4
|
+
path: string;
|
|
5
|
+
message: string;
|
|
6
|
+
}
|
|
7
|
+
export declare class EditletError extends Error {
|
|
8
|
+
issues: Issue[];
|
|
9
|
+
constructor(issues: Issue[], message?: string);
|
|
10
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// core/issues.ts
|
|
2
|
+
var EditletError = class extends Error {
|
|
3
|
+
issues;
|
|
4
|
+
constructor(issues, message) {
|
|
5
|
+
super(message ?? issues.map((i) => i.path ? `${i.path}: ${i.message}` : i.message).join("; "));
|
|
6
|
+
this.name = "EditletError";
|
|
7
|
+
this.issues = issues;
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
export { EditletError };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Schema } from "./schema.js";
|
|
2
|
+
export declare const SLUG_RE: RegExp;
|
|
3
|
+
export declare function isValidSlug(slug: string): boolean;
|
|
4
|
+
export declare function slugify(text: string): string;
|
|
5
|
+
/**
|
|
6
|
+
* `<contentDir>/<collection>/<slug>.md` (`index.md` for a singleton) without any validation; never throws. Use
|
|
7
|
+
* `entryPath` when the collection and slug come from outside.
|
|
8
|
+
*/
|
|
9
|
+
export declare function entryFilePath(schema: Schema, collection: string, slug: string): string;
|
|
10
|
+
export declare function entryPath(schema: Schema, collection: string, slug?: string): string;
|
|
11
|
+
/**
|
|
12
|
+
* A relative posix path: no empty, `.` or `..` segments (so no leading or trailing slash), no backslash, NUL or
|
|
13
|
+
* `:` (rules out drive letters and URL schemes).
|
|
14
|
+
*/
|
|
15
|
+
export declare function isCanonicalPath(path: string): boolean;
|
|
16
|
+
/** True when a segment below `contentDir` starts with `.`: dot files and dot directories are hidden. */
|
|
17
|
+
export declare function isHiddenContentPath(schema: Schema, path: string): boolean;
|
|
18
|
+
export declare function parseEntryPath(schema: Schema, path: string): {
|
|
19
|
+
collection: string;
|
|
20
|
+
slug: string;
|
|
21
|
+
} | null;
|
|
22
|
+
export declare function resolveAssetPath(schema: Schema, entryFile: string, src: string): string | null;
|
|
23
|
+
/** Inverse of `resolveAssetPath`: the `src` for `assetPath` relative to the entry file's folder. */
|
|
24
|
+
export declare function relativeAssetSrc(entryFile: string, assetPath: string): string;
|
|
25
|
+
export declare function refKey(collection: string, slug: string): string;
|
|
26
|
+
export declare function parseRefKey(ref: string): {
|
|
27
|
+
collection: string;
|
|
28
|
+
slug: string;
|
|
29
|
+
} | null;
|
|
30
|
+
export declare const IMAGE_EXTENSIONS: readonly string[];
|
|
31
|
+
export declare function isImagePath(path: string): boolean;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { EditletError } from "./issues.js";
|
|
2
|
+
// core/paths.ts
|
|
3
|
+
const SLUG_RE = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
4
|
+
function isValidSlug(slug) {
|
|
5
|
+
return SLUG_RE.test(slug);
|
|
6
|
+
}
|
|
7
|
+
function slugify(text) {
|
|
8
|
+
return text.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
|
|
9
|
+
}
|
|
10
|
+
function fail(message) {
|
|
11
|
+
throw new EditletError([{
|
|
12
|
+
path: "",
|
|
13
|
+
message
|
|
14
|
+
}]);
|
|
15
|
+
}
|
|
16
|
+
function hasCollection(schema, collection) {
|
|
17
|
+
return Object.hasOwn(schema.collections, collection);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* `<contentDir>/<collection>/<slug>.md` (`index.md` for a singleton) without any validation; never throws. Use
|
|
21
|
+
* `entryPath` when the collection and slug come from outside.
|
|
22
|
+
*/
|
|
23
|
+
function entryFilePath(schema, collection, slug) {
|
|
24
|
+
const singleton = hasCollection(schema, collection) && schema.collections[collection].singleton;
|
|
25
|
+
return `${schema.contentDir}/${collection}/${singleton ? "index" : slug}.md`;
|
|
26
|
+
}
|
|
27
|
+
function entryPath(schema, collection, slug) {
|
|
28
|
+
if (!hasCollection(schema, collection)) return fail(`Unknown collection "${collection}"`);
|
|
29
|
+
if (!schema.collections[collection].singleton && (slug === void 0 || !isValidSlug(slug))) return fail(`Invalid slug "${slug ?? ""}" for collection "${collection}"`);
|
|
30
|
+
return entryFilePath(schema, collection, slug ?? "index");
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* A relative posix path: no empty, `.` or `..` segments (so no leading or trailing slash), no backslash, NUL or
|
|
34
|
+
* `:` (rules out drive letters and URL schemes).
|
|
35
|
+
*/
|
|
36
|
+
function isCanonicalPath(path) {
|
|
37
|
+
if (path === "" || /[\\\0:]/.test(path)) return false;
|
|
38
|
+
return path.split("/").every((seg) => seg !== "" && seg !== "." && seg !== "..");
|
|
39
|
+
}
|
|
40
|
+
/** True when a segment below `contentDir` starts with `.`: dot files and dot directories are hidden. */
|
|
41
|
+
function isHiddenContentPath(schema, path) {
|
|
42
|
+
const prefix = `${schema.contentDir}/`;
|
|
43
|
+
return (path.startsWith(prefix) ? path.slice(prefix.length) : path).split("/").some((seg) => seg.startsWith("."));
|
|
44
|
+
}
|
|
45
|
+
function parseEntryPath(schema, path) {
|
|
46
|
+
const prefix = `${schema.contentDir}/`;
|
|
47
|
+
if (!path.startsWith(prefix) || !isCanonicalPath(path)) return null;
|
|
48
|
+
const parts = path.slice(prefix.length).split("/");
|
|
49
|
+
if (parts.length !== 2) return null;
|
|
50
|
+
const [collection, file] = parts;
|
|
51
|
+
if (!hasCollection(schema, collection) || !file.endsWith(".md")) return null;
|
|
52
|
+
const name = file.slice(0, -3);
|
|
53
|
+
if (name === "") return null;
|
|
54
|
+
if (schema.collections[collection].singleton) return file === "index.md" ? {
|
|
55
|
+
collection,
|
|
56
|
+
slug: "index"
|
|
57
|
+
} : null;
|
|
58
|
+
return {
|
|
59
|
+
collection,
|
|
60
|
+
slug: name
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function resolveAssetPath(schema, entryFile, src) {
|
|
64
|
+
if (src === "" || src.startsWith("/") || src.includes("\\") || /^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(src)) return null;
|
|
65
|
+
const srcSegs = src.split("/");
|
|
66
|
+
const last = srcSegs[srcSegs.length - 1];
|
|
67
|
+
if (last === "" || last === "." || last === "..") return null;
|
|
68
|
+
const out = entryFile.split("/").slice(0, -1);
|
|
69
|
+
for (const seg of srcSegs) {
|
|
70
|
+
if (seg === "" || seg === ".") continue;
|
|
71
|
+
if (seg === "..") {
|
|
72
|
+
if (out.length === 0) return null;
|
|
73
|
+
out.pop();
|
|
74
|
+
} else out.push(seg);
|
|
75
|
+
}
|
|
76
|
+
const result = out.join("/");
|
|
77
|
+
return result.startsWith(`${schema.contentDir}/`) ? result : null;
|
|
78
|
+
}
|
|
79
|
+
/** Inverse of `resolveAssetPath`: the `src` for `assetPath` relative to the entry file's folder. */
|
|
80
|
+
function relativeAssetSrc(entryFile, assetPath) {
|
|
81
|
+
const from = entryFile.split("/").slice(0, -1);
|
|
82
|
+
const to = assetPath.split("/");
|
|
83
|
+
let common = 0;
|
|
84
|
+
while (common < from.length && common < to.length - 1 && from[common] === to[common]) common++;
|
|
85
|
+
return "../".repeat(from.length - common) + to.slice(common).join("/");
|
|
86
|
+
}
|
|
87
|
+
function refKey(collection, slug) {
|
|
88
|
+
return `${collection}/${slug}`;
|
|
89
|
+
}
|
|
90
|
+
function parseRefKey(ref) {
|
|
91
|
+
const parts = ref.split("/");
|
|
92
|
+
if (parts.length !== 2) return null;
|
|
93
|
+
const [collection, slug] = parts;
|
|
94
|
+
if (collection === "" || !isValidSlug(slug)) return null;
|
|
95
|
+
return {
|
|
96
|
+
collection,
|
|
97
|
+
slug
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
const IMAGE_EXTENSIONS = [
|
|
101
|
+
"webp",
|
|
102
|
+
"png",
|
|
103
|
+
"jpg",
|
|
104
|
+
"jpeg",
|
|
105
|
+
"avif",
|
|
106
|
+
"gif",
|
|
107
|
+
"svg"
|
|
108
|
+
];
|
|
109
|
+
function isImagePath(path) {
|
|
110
|
+
const name = path.slice(path.lastIndexOf("/") + 1);
|
|
111
|
+
const dot = name.lastIndexOf(".");
|
|
112
|
+
return dot > 0 && IMAGE_EXTENSIONS.includes(name.slice(dot + 1).toLowerCase());
|
|
113
|
+
}
|
|
114
|
+
export { IMAGE_EXTENSIONS, SLUG_RE, entryFilePath, entryPath, isCanonicalPath, isHiddenContentPath, isImagePath, isValidSlug, parseEntryPath, parseRefKey, refKey, relativeAssetSrc, resolveAssetPath, slugify };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { decodeUtf8, parseEntryFile } from "./entryFile.js";
|
|
2
|
+
import { EditletError } from "./issues.js";
|
|
3
|
+
import { entryFilePath, isHiddenContentPath, isImagePath, parseEntryPath } from "./paths.js";
|
|
4
|
+
// core/project.ts
|
|
5
|
+
const byFile = (a, b) => (a.file ?? "").localeCompare(b.file ?? "");
|
|
6
|
+
/** Schema file, hidden paths and images are never entries nor reported. */
|
|
7
|
+
function isSkippedPath(schema, schemaPath, file) {
|
|
8
|
+
return file === schemaPath || isHiddenContentPath(schema, file) || isImagePath(file);
|
|
9
|
+
}
|
|
10
|
+
/** Collection/slug when `file` is an entry file (`.md` under the schema's layout), else null. */
|
|
11
|
+
function entryCandidate(schema, file) {
|
|
12
|
+
return file.endsWith(".md") ? parseEntryPath(schema, file) : null;
|
|
13
|
+
}
|
|
14
|
+
/** Pure "files → entries" step shared by the node reader and the preview reader. */
|
|
15
|
+
function entriesFromFiles(schema, schemaPath, files) {
|
|
16
|
+
const entries = [];
|
|
17
|
+
const issues = [];
|
|
18
|
+
for (const [file, content] of files) {
|
|
19
|
+
if (isSkippedPath(schema, schemaPath, file)) continue;
|
|
20
|
+
const parsed = entryCandidate(schema, file);
|
|
21
|
+
if (!parsed) {
|
|
22
|
+
issues.push({
|
|
23
|
+
file,
|
|
24
|
+
path: "",
|
|
25
|
+
message: "Not an entry file"
|
|
26
|
+
});
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
const decoded = decodeUtf8(content, file);
|
|
30
|
+
if ("issue" in decoded) {
|
|
31
|
+
issues.push(decoded.issue);
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
const { data, body } = parseEntryFile(decoded.text);
|
|
36
|
+
entries.push({
|
|
37
|
+
collection: parsed.collection,
|
|
38
|
+
slug: parsed.slug,
|
|
39
|
+
data,
|
|
40
|
+
body
|
|
41
|
+
});
|
|
42
|
+
} catch (error) {
|
|
43
|
+
if (!(error instanceof EditletError)) throw error;
|
|
44
|
+
issues.push(...error.issues.map((issue) => ({
|
|
45
|
+
...issue,
|
|
46
|
+
file
|
|
47
|
+
})));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const fileOf = (e) => entryFilePath(schema, e.collection, e.slug);
|
|
51
|
+
return {
|
|
52
|
+
entries: entries.toSorted((a, b) => fileOf(a).localeCompare(fileOf(b))),
|
|
53
|
+
issues: issues.toSorted(byFile)
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
export { byFile, entriesFromFiles, entryCandidate, isSkippedPath };
|