@lownoise-studio/rendershield 0.3.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +73 -32
- package/CONTRIBUTING.md +41 -0
- package/README.md +227 -144
- package/SECURITY.md +25 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +39 -26
- package/dist/cli.js.map +1 -1
- package/dist/cliArgs.d.ts +8 -0
- package/dist/cliArgs.d.ts.map +1 -0
- package/dist/cliArgs.js +56 -0
- package/dist/cliArgs.js.map +1 -0
- package/dist/commands/build.d.ts +3 -0
- package/dist/commands/build.d.ts.map +1 -0
- package/dist/commands/build.js +12 -11
- package/dist/commands/build.js.map +1 -1
- package/dist/commands/init.d.ts +3 -0
- package/dist/commands/init.d.ts.map +1 -0
- package/dist/commands/init.js +8 -7
- package/dist/commands/init.js.map +1 -1
- package/dist/commands/verify.d.ts +33 -0
- package/dist/commands/verify.d.ts.map +1 -0
- package/dist/commands/verify.js +202 -117
- package/dist/commands/verify.js.map +1 -1
- package/dist/configPath.d.ts +7 -0
- package/dist/configPath.d.ts.map +1 -0
- package/dist/configPath.js +8 -0
- package/dist/configPath.js.map +1 -0
- package/dist/core/generateRobots.d.ts +3 -0
- package/dist/core/generateRobots.d.ts.map +1 -0
- package/dist/core/generateSitemap.d.ts +3 -0
- package/dist/core/generateSitemap.d.ts.map +1 -0
- package/dist/core/generateWorker.d.ts +3 -0
- package/dist/core/generateWorker.d.ts.map +1 -0
- package/dist/core/generateWorker.js +75 -75
- package/dist/core/generateWorker.js.map +1 -1
- package/dist/core/listOutputRoutes.d.ts +4 -0
- package/dist/core/listOutputRoutes.d.ts.map +1 -0
- package/dist/core/listOutputRoutes.js +40 -0
- package/dist/core/listOutputRoutes.js.map +1 -0
- package/dist/core/loadConfig.d.ts +5 -0
- package/dist/core/loadConfig.d.ts.map +1 -0
- package/dist/core/loadConfig.js +108 -58
- package/dist/core/loadConfig.js.map +1 -1
- package/dist/core/loadMarkdown.d.ts +3 -0
- package/dist/core/loadMarkdown.d.ts.map +1 -0
- package/dist/core/loadMarkdown.js +4 -3
- package/dist/core/loadMarkdown.js.map +1 -1
- package/dist/core/renderHtml.d.ts +3 -0
- package/dist/core/renderHtml.d.ts.map +1 -0
- package/dist/core/renderHtml.js +25 -10
- package/dist/core/renderHtml.js.map +1 -1
- package/dist/core/validateOutput.d.ts +28 -0
- package/dist/core/validateOutput.d.ts.map +1 -0
- package/dist/core/validateOutput.js +7 -1
- package/dist/core/validateOutput.js.map +1 -1
- package/dist/errors.d.ts +14 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +24 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +53 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -1
- package/dist/types.js.map +1 -1
- package/docs/CONFIG.md +70 -0
- package/docs/deploy-cloudflare.md +40 -14
- package/package.json +25 -2
- package/rendershield.config.schema.json +100 -0
- package/src/cli.ts +96 -75
- package/src/cliArgs.ts +71 -0
- package/src/commands/build.ts +200 -185
- package/src/commands/init.ts +8 -8
- package/src/commands/verify.ts +451 -236
- package/src/configPath.ts +17 -0
- package/src/core/generateWorker.ts +97 -97
- package/src/core/listOutputRoutes.ts +51 -0
- package/src/core/loadConfig.ts +282 -173
- package/src/core/loadMarkdown.ts +9 -3
- package/src/core/renderHtml.ts +36 -12
- package/src/core/validateOutput.ts +335 -328
- package/src/errors.ts +48 -0
- package/src/index.ts +43 -0
- package/src/types.ts +5 -2
package/src/core/loadConfig.ts
CHANGED
|
@@ -1,173 +1,282 @@
|
|
|
1
|
-
import fs from "fs-extra";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import { RenderShieldConfig } from "../types.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
function
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
`worker.
|
|
82
|
-
);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
if (!
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
const
|
|
151
|
-
const
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
parsed
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
}
|
|
1
|
+
import fs from "fs-extra";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { RenderShieldConfig, SCHEMA_TYPES, type SchemaType } from "../types.js";
|
|
4
|
+
import { renderShieldError } from "../errors.js";
|
|
5
|
+
import {
|
|
6
|
+
resolveConfigFile,
|
|
7
|
+
DEFAULT_CONFIG_NAME,
|
|
8
|
+
type CommandOptions,
|
|
9
|
+
} from "../configPath.js";
|
|
10
|
+
|
|
11
|
+
const DEFAULT_SITEMAP_PATH = "/sitemap.xml";
|
|
12
|
+
const DEFAULT_ROBOTS_PATH = "/robots.txt";
|
|
13
|
+
|
|
14
|
+
type BoolFlag = { enabled: boolean };
|
|
15
|
+
|
|
16
|
+
/** Shape of config as read from JSON (before normalization). Used for validation only. */
|
|
17
|
+
interface ParsedInput {
|
|
18
|
+
version?: unknown;
|
|
19
|
+
site?: { canonicalBase?: unknown; siteName?: unknown; defaultOgImage?: unknown; authorName?: unknown };
|
|
20
|
+
content?: { markdown?: { baseDir?: unknown; collections?: unknown[] } };
|
|
21
|
+
output?: { outDir?: unknown };
|
|
22
|
+
sitemap?: Record<string, unknown>;
|
|
23
|
+
robots?: Record<string, unknown>;
|
|
24
|
+
worker?: Record<string, unknown>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function isObject(v: unknown): v is Record<string, unknown> {
|
|
28
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function coerceBoolFlag(
|
|
32
|
+
parsed: Record<string, unknown>,
|
|
33
|
+
key: "sitemap" | "robots" | "worker",
|
|
34
|
+
defaultEnabled: boolean
|
|
35
|
+
): BoolFlag {
|
|
36
|
+
if (parsed?.[key] == null) return { enabled: defaultEnabled };
|
|
37
|
+
|
|
38
|
+
const v = parsed[key];
|
|
39
|
+
if (!isObject(v)) {
|
|
40
|
+
throw renderShieldError(
|
|
41
|
+
"CONFIG_INVALID",
|
|
42
|
+
`${key} must be an object like { "enabled": true }`
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
const enabled = (v as Record<string, unknown>).enabled;
|
|
46
|
+
if (typeof enabled !== "boolean") {
|
|
47
|
+
throw renderShieldError(
|
|
48
|
+
"CONFIG_INVALID",
|
|
49
|
+
`${key}.enabled must be a boolean`
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
return { enabled };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function readSpaOrigin(worker: Record<string, unknown>): string | undefined {
|
|
56
|
+
const spa = worker.spaOrigin ?? worker.lovableOrigin;
|
|
57
|
+
return typeof spa === "string" && spa.trim() !== "" ? spa.trim() : undefined;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Worker config policy: strict mode when enabled.
|
|
62
|
+
* spaOrigin (or deprecated lovableOrigin) must be http(s).
|
|
63
|
+
*/
|
|
64
|
+
function validateWorkerWhenEnabled(parsed: Record<string, unknown>): void {
|
|
65
|
+
const worker = parsed.worker as Record<string, unknown> | undefined;
|
|
66
|
+
const spaOrigin = worker ? readSpaOrigin(worker) : undefined;
|
|
67
|
+
|
|
68
|
+
if (!spaOrigin) {
|
|
69
|
+
throw renderShieldError(
|
|
70
|
+
"CONFIG_INVALID",
|
|
71
|
+
`worker.spaOrigin is required when worker.enabled is true. Example: "https://app.example.com" (lovableOrigin is accepted as a deprecated alias).`
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let url: URL;
|
|
76
|
+
try {
|
|
77
|
+
url = new URL(spaOrigin);
|
|
78
|
+
} catch {
|
|
79
|
+
throw renderShieldError(
|
|
80
|
+
"CONFIG_INVALID",
|
|
81
|
+
`worker.spaOrigin must be a valid URL. Got: "${spaOrigin}"`
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
const scheme = url.protocol.toLowerCase();
|
|
85
|
+
if (scheme !== "http:" && scheme !== "https:") {
|
|
86
|
+
throw renderShieldError(
|
|
87
|
+
"CONFIG_INVALID",
|
|
88
|
+
`worker.spaOrigin must use http or https. Got: "${url.protocol}"`
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
if (
|
|
92
|
+
!Array.isArray(worker?.rewriteRouteBases) ||
|
|
93
|
+
worker.rewriteRouteBases.length === 0
|
|
94
|
+
) {
|
|
95
|
+
throw renderShieldError(
|
|
96
|
+
"CONFIG_INVALID",
|
|
97
|
+
`worker.rewriteRouteBases must be a non-empty array when worker.enabled is true. Example: ["/blog/"]`
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
if (
|
|
101
|
+
!Array.isArray(worker?.botUserAgentPatterns) ||
|
|
102
|
+
worker.botUserAgentPatterns.length === 0
|
|
103
|
+
) {
|
|
104
|
+
throw renderShieldError(
|
|
105
|
+
"CONFIG_INVALID",
|
|
106
|
+
`worker.botUserAgentPatterns must be a non-empty array when worker.enabled is true. Example: ["googlebot", "bingbot"]`
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
for (let i = 0; i < worker.botUserAgentPatterns.length; i++) {
|
|
110
|
+
const p = worker.botUserAgentPatterns[i];
|
|
111
|
+
if (typeof p !== "string" || p.trim() === "") {
|
|
112
|
+
throw renderShieldError(
|
|
113
|
+
"CONFIG_INVALID",
|
|
114
|
+
`worker.botUserAgentPatterns[${i}] must be a non-empty string (substring match). Empty or invalid entry would overmatch.`
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function normalizeWorker(
|
|
121
|
+
parsed: Record<string, unknown>,
|
|
122
|
+
enabled: boolean
|
|
123
|
+
): void {
|
|
124
|
+
if (!enabled) {
|
|
125
|
+
parsed.worker = {
|
|
126
|
+
enabled: false,
|
|
127
|
+
spaOrigin: "",
|
|
128
|
+
rewriteRouteBases: [],
|
|
129
|
+
botUserAgentPatterns: [],
|
|
130
|
+
debugHeaders: false,
|
|
131
|
+
};
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
validateWorkerWhenEnabled(parsed);
|
|
136
|
+
const worker = parsed.worker as Record<string, unknown>;
|
|
137
|
+
parsed.worker = {
|
|
138
|
+
enabled: true,
|
|
139
|
+
spaOrigin: readSpaOrigin(worker) as string,
|
|
140
|
+
rewriteRouteBases: worker.rewriteRouteBases,
|
|
141
|
+
botUserAgentPatterns: worker.botUserAgentPatterns,
|
|
142
|
+
debugHeaders: worker.debugHeaders === true,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export async function loadConfig(
|
|
147
|
+
cwd = process.cwd(),
|
|
148
|
+
options?: CommandOptions
|
|
149
|
+
): Promise<RenderShieldConfig> {
|
|
150
|
+
const configFile = resolveConfigFile(cwd, options?.configPath);
|
|
151
|
+
const configName = path.basename(configFile);
|
|
152
|
+
const exists = await fs.pathExists(configFile);
|
|
153
|
+
if (!exists) {
|
|
154
|
+
throw renderShieldError(
|
|
155
|
+
"CONFIG_MISSING",
|
|
156
|
+
`Missing ${configName}. Run: rendershield init`
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const raw = await fs.readFile(configFile, "utf8");
|
|
161
|
+
let parsed: ParsedInput & Record<string, unknown>;
|
|
162
|
+
try {
|
|
163
|
+
parsed = JSON.parse(raw) as ParsedInput & Record<string, unknown>;
|
|
164
|
+
} catch {
|
|
165
|
+
throw renderShieldError("CONFIG_INVALID", `${configName} is not valid JSON`);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (parsed?.version !== 1) {
|
|
169
|
+
throw renderShieldError("CONFIG_INVALID", "Config version must be 1");
|
|
170
|
+
}
|
|
171
|
+
if (!parsed?.site?.canonicalBase) {
|
|
172
|
+
throw renderShieldError("CONFIG_INVALID", "site.canonicalBase is required");
|
|
173
|
+
}
|
|
174
|
+
if (!parsed?.site?.siteName) {
|
|
175
|
+
throw renderShieldError("CONFIG_INVALID", "site.siteName is required");
|
|
176
|
+
}
|
|
177
|
+
if (!parsed?.site?.defaultOgImage) {
|
|
178
|
+
throw renderShieldError("CONFIG_INVALID", "site.defaultOgImage is required");
|
|
179
|
+
}
|
|
180
|
+
if (!parsed?.site?.authorName) {
|
|
181
|
+
throw renderShieldError("CONFIG_INVALID", "site.authorName is required");
|
|
182
|
+
}
|
|
183
|
+
if (!parsed?.content?.markdown?.baseDir) {
|
|
184
|
+
throw renderShieldError("CONFIG_INVALID", "content.markdown.baseDir is required");
|
|
185
|
+
}
|
|
186
|
+
if (
|
|
187
|
+
!Array.isArray(parsed?.content?.markdown?.collections) ||
|
|
188
|
+
parsed.content.markdown.collections.length === 0
|
|
189
|
+
) {
|
|
190
|
+
throw renderShieldError(
|
|
191
|
+
"CONFIG_INVALID",
|
|
192
|
+
"content.markdown.collections must be a non-empty array"
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
if (!parsed?.output?.outDir) {
|
|
196
|
+
throw renderShieldError("CONFIG_INVALID", "output.outDir is required");
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
parsed.content.markdown.collections = normalizeCollections(
|
|
200
|
+
parsed.content.markdown.collections
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
const sitemapFlag = coerceBoolFlag(parsed, "sitemap", true);
|
|
204
|
+
const sitemapObj = parsed.sitemap as Record<string, unknown> | undefined;
|
|
205
|
+
const sitemapPathVal = sitemapObj?.path;
|
|
206
|
+
const sitemapPath =
|
|
207
|
+
typeof sitemapPathVal === "string" && sitemapPathVal.trim().startsWith("/")
|
|
208
|
+
? sitemapPathVal.trim()
|
|
209
|
+
: DEFAULT_SITEMAP_PATH;
|
|
210
|
+
parsed.sitemap = { enabled: sitemapFlag.enabled, path: sitemapPath };
|
|
211
|
+
|
|
212
|
+
const robotsFlag = coerceBoolFlag(parsed, "robots", true);
|
|
213
|
+
const robotsObj = parsed.robots as Record<string, unknown> | undefined;
|
|
214
|
+
const robotsPathVal = robotsObj?.path;
|
|
215
|
+
const robotsPath =
|
|
216
|
+
typeof robotsPathVal === "string" && robotsPathVal.trim().startsWith("/")
|
|
217
|
+
? robotsPathVal.trim()
|
|
218
|
+
: DEFAULT_ROBOTS_PATH;
|
|
219
|
+
parsed.robots = { enabled: robotsFlag.enabled, path: robotsPath };
|
|
220
|
+
|
|
221
|
+
const workerFlag = coerceBoolFlag(parsed, "worker", true);
|
|
222
|
+
normalizeWorker(parsed, workerFlag.enabled);
|
|
223
|
+
|
|
224
|
+
return parsed as RenderShieldConfig;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export { DEFAULT_CONFIG_NAME };
|
|
228
|
+
|
|
229
|
+
function normalizeCollections(collections: unknown[]): RenderShieldConfig["content"]["markdown"]["collections"] {
|
|
230
|
+
return collections.map((raw, index) => {
|
|
231
|
+
if (!isObject(raw)) {
|
|
232
|
+
throw renderShieldError(
|
|
233
|
+
"CONFIG_INVALID",
|
|
234
|
+
`content.markdown.collections[${index}] must be an object`
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const name = raw.name;
|
|
239
|
+
const pattern = raw.pattern;
|
|
240
|
+
const routeBase = raw.routeBase;
|
|
241
|
+
const schemaTypeRaw = raw.schemaType;
|
|
242
|
+
|
|
243
|
+
if (typeof name !== "string" || name.trim() === "") {
|
|
244
|
+
throw renderShieldError(
|
|
245
|
+
"CONFIG_INVALID",
|
|
246
|
+
`content.markdown.collections[${index}].name must be a non-empty string`
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
if (typeof pattern !== "string" || pattern.trim() === "") {
|
|
250
|
+
throw renderShieldError(
|
|
251
|
+
"CONFIG_INVALID",
|
|
252
|
+
`content.markdown.collections[${index}].pattern must be a non-empty string`
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
if (typeof routeBase !== "string" || routeBase.trim() === "") {
|
|
256
|
+
throw renderShieldError(
|
|
257
|
+
"CONFIG_INVALID",
|
|
258
|
+
`content.markdown.collections[${index}].routeBase must be a non-empty string`
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const schemaType: SchemaType =
|
|
263
|
+
schemaTypeRaw === undefined ? "Article" : parseSchemaType(schemaTypeRaw, index);
|
|
264
|
+
|
|
265
|
+
return {
|
|
266
|
+
name: name.trim(),
|
|
267
|
+
pattern: pattern.trim(),
|
|
268
|
+
routeBase: routeBase.trim(),
|
|
269
|
+
schemaType,
|
|
270
|
+
};
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function parseSchemaType(value: unknown, index: number): SchemaType {
|
|
275
|
+
if (typeof value !== "string" || !SCHEMA_TYPES.includes(value as SchemaType)) {
|
|
276
|
+
throw renderShieldError(
|
|
277
|
+
"CONFIG_INVALID",
|
|
278
|
+
`content.markdown.collections[${index}].schemaType must be one of: ${SCHEMA_TYPES.join(", ")}`
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
return value as SchemaType;
|
|
282
|
+
}
|
package/src/core/loadMarkdown.ts
CHANGED
|
@@ -4,6 +4,7 @@ import fs from "fs-extra";
|
|
|
4
4
|
import matter from "gray-matter";
|
|
5
5
|
import MarkdownIt from "markdown-it";
|
|
6
6
|
import { MarkdownDoc, RenderShieldConfig } from "../types.js";
|
|
7
|
+
import { renderShieldError } from "../errors.js";
|
|
7
8
|
|
|
8
9
|
const md = new MarkdownIt({ html: false, linkify: true, typographer: true });
|
|
9
10
|
|
|
@@ -11,7 +12,8 @@ const REQUIRED_FIELDS = "title, excerpt, datePublished, coverImage, slug";
|
|
|
11
12
|
|
|
12
13
|
function requireString(value: unknown, field: string, file: string): string {
|
|
13
14
|
if (typeof value !== "string" || value.trim().length === 0) {
|
|
14
|
-
throw
|
|
15
|
+
throw renderShieldError(
|
|
16
|
+
"CONTENT_INVALID",
|
|
15
17
|
`Missing required frontmatter field "${field}" in ${file}. Required fields: ${REQUIRED_FIELDS}`
|
|
16
18
|
);
|
|
17
19
|
}
|
|
@@ -25,7 +27,10 @@ function normalizeDate(value: unknown, file: string): string {
|
|
|
25
27
|
if (value instanceof Date) {
|
|
26
28
|
return value.toISOString().slice(0, 10);
|
|
27
29
|
}
|
|
28
|
-
throw
|
|
30
|
+
throw renderShieldError(
|
|
31
|
+
"CONTENT_INVALID",
|
|
32
|
+
`Invalid datePublished in ${file}. Use format YYYY-MM-DD.`
|
|
33
|
+
);
|
|
29
34
|
}
|
|
30
35
|
|
|
31
36
|
export async function loadAllMarkdownDocs(cfg: RenderShieldConfig, cwd = process.cwd()): Promise<MarkdownDoc[]> {
|
|
@@ -47,7 +52,8 @@ export async function loadAllMarkdownDocs(cfg: RenderShieldConfig, cwd = process
|
|
|
47
52
|
|
|
48
53
|
const datePublishedRaw = parsed.data?.datePublished;
|
|
49
54
|
if (datePublishedRaw === undefined || datePublishedRaw === null) {
|
|
50
|
-
throw
|
|
55
|
+
throw renderShieldError(
|
|
56
|
+
"CONTENT_INVALID",
|
|
51
57
|
`Missing required frontmatter field "datePublished" in ${abs}. Required fields: ${REQUIRED_FIELDS}`
|
|
52
58
|
);
|
|
53
59
|
}
|
package/src/core/renderHtml.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { MarkdownDoc, RenderShieldConfig } from "../types.js";
|
|
1
|
+
import { MarkdownDoc, RenderShieldConfig, SchemaType } from "../types.js";
|
|
2
2
|
|
|
3
3
|
function escapeHtml(s: string): string {
|
|
4
4
|
return s
|
|
@@ -15,6 +15,37 @@ function joinUrl(base: string, pathname: string): string {
|
|
|
15
15
|
return b + p;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
+
function resolveSchemaType(cfg: RenderShieldConfig, doc: MarkdownDoc): SchemaType {
|
|
19
|
+
const collection = cfg.content.markdown.collections.find((c) => c.name === doc.collection);
|
|
20
|
+
return collection?.schemaType ?? "Article";
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function buildJsonLd(
|
|
24
|
+
schemaType: SchemaType,
|
|
25
|
+
cfg: RenderShieldConfig,
|
|
26
|
+
doc: MarkdownDoc,
|
|
27
|
+
canonicalUrl: string,
|
|
28
|
+
ogImageUrl: string
|
|
29
|
+
): Record<string, unknown> {
|
|
30
|
+
const base = {
|
|
31
|
+
"@context": "https://schema.org",
|
|
32
|
+
"@type": schemaType,
|
|
33
|
+
mainEntityOfPage: canonicalUrl,
|
|
34
|
+
image: ogImageUrl,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
if (schemaType === "WebPage") {
|
|
38
|
+
return { ...base, name: doc.title };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
...base,
|
|
43
|
+
headline: doc.title,
|
|
44
|
+
author: { "@type": "Person", name: cfg.site.authorName },
|
|
45
|
+
datePublished: doc.datePublished,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
18
49
|
export function renderPageHtml(cfg: RenderShieldConfig, doc: MarkdownDoc): string {
|
|
19
50
|
const canonicalUrl = joinUrl(cfg.site.canonicalBase, doc.routePath);
|
|
20
51
|
const ogImageUrl = doc.coverImage.startsWith("http")
|
|
@@ -23,16 +54,9 @@ export function renderPageHtml(cfg: RenderShieldConfig, doc: MarkdownDoc): strin
|
|
|
23
54
|
|
|
24
55
|
const title = `${doc.title} - ${cfg.site.siteName}`;
|
|
25
56
|
const description = doc.excerpt;
|
|
26
|
-
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
"@type": "Article",
|
|
30
|
-
headline: doc.title,
|
|
31
|
-
author: { "@type": "Person", name: cfg.site.authorName },
|
|
32
|
-
datePublished: doc.datePublished,
|
|
33
|
-
image: ogImageUrl,
|
|
34
|
-
mainEntityOfPage: canonicalUrl,
|
|
35
|
-
};
|
|
57
|
+
const schemaType = resolveSchemaType(cfg, doc);
|
|
58
|
+
const ogType = schemaType === "WebPage" ? "website" : "article";
|
|
59
|
+
const jsonLd = buildJsonLd(schemaType, cfg, doc, canonicalUrl, ogImageUrl);
|
|
36
60
|
|
|
37
61
|
// NOTE: doc.htmlContent is already HTML from markdown-it.
|
|
38
62
|
// We trust it as generated output, not user-injected raw HTML (markdown-it html:false).
|
|
@@ -46,7 +70,7 @@ export function renderPageHtml(cfg: RenderShieldConfig, doc: MarkdownDoc): strin
|
|
|
46
70
|
<meta name="description" content="${escapeHtml(description)}">
|
|
47
71
|
<link rel="canonical" href="${escapeHtml(canonicalUrl)}">
|
|
48
72
|
|
|
49
|
-
<meta property="og:type" content="
|
|
73
|
+
<meta property="og:type" content="${ogType}">
|
|
50
74
|
<meta property="og:title" content="${escapeHtml(doc.title)}">
|
|
51
75
|
<meta property="og:description" content="${escapeHtml(description)}">
|
|
52
76
|
<meta property="og:image" content="${escapeHtml(ogImageUrl)}">
|