openalmanac 0.2.30 → 0.2.31
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/tools/articles.js +29 -3
- package/package.json +1 -1
package/dist/tools/articles.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
import { readFileSync, writeFileSync, mkdirSync, readdirSync, statSync, existsSync } from "node:fs";
|
|
2
|
+
import { readFileSync, writeFileSync, mkdirSync, readdirSync, statSync, existsSync, unlinkSync } from "node:fs";
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import { stringify as yamlStringify } from "yaml";
|
|
5
5
|
import { request, ARTICLES_DIR, getAuthStatus } from "../auth.js";
|
|
@@ -206,6 +206,9 @@ export function registerArticleTools(server) {
|
|
|
206
206
|
slug: z.string().describe("Article slug (e.g. 'machine-learning')"),
|
|
207
207
|
}),
|
|
208
208
|
async execute({ slug }) {
|
|
209
|
+
if (!SLUG_RE.test(slug)) {
|
|
210
|
+
throw new Error(`Invalid slug "${slug}". Must be kebab-case (e.g. 'machine-learning').`);
|
|
211
|
+
}
|
|
209
212
|
const resp = await request("GET", `/api/articles/${slug}`, {
|
|
210
213
|
params: { format: "md" },
|
|
211
214
|
});
|
|
@@ -213,6 +216,8 @@ export function registerArticleTools(server) {
|
|
|
213
216
|
ensureArticlesDir();
|
|
214
217
|
const filePath = join(ARTICLES_DIR, `${slug}.md`);
|
|
215
218
|
writeFileSync(filePath, markdown, "utf-8");
|
|
219
|
+
const originalPath = join(ARTICLES_DIR, `.${slug}.original.md`);
|
|
220
|
+
writeFileSync(originalPath, markdown, "utf-8");
|
|
216
221
|
const { frontmatter, content } = parseFrontmatter(markdown);
|
|
217
222
|
const title = frontmatter.title || "(untitled)";
|
|
218
223
|
const wordCount = content.trim().split(/\s+/).filter(Boolean).length;
|
|
@@ -260,6 +265,9 @@ export function registerArticleTools(server) {
|
|
|
260
265
|
change_description: z.string().optional().describe("Longer description of what changed and why"),
|
|
261
266
|
}),
|
|
262
267
|
async execute({ slug, change_title, change_description }) {
|
|
268
|
+
if (!SLUG_RE.test(slug)) {
|
|
269
|
+
throw new Error(`Invalid slug "${slug}". Must be kebab-case (e.g. 'machine-learning').`);
|
|
270
|
+
}
|
|
263
271
|
const filePath = join(ARTICLES_DIR, `${slug}.md`);
|
|
264
272
|
let raw;
|
|
265
273
|
try {
|
|
@@ -293,7 +301,25 @@ export function registerArticleTools(server) {
|
|
|
293
301
|
const data = (await resp.json());
|
|
294
302
|
const articleUrl = `https://www.openalmanac.org/article/${slug}?celebrate=true`;
|
|
295
303
|
openBrowser(articleUrl);
|
|
296
|
-
|
|
304
|
+
// Clean up local files after successful publish
|
|
305
|
+
let cleanupWarning = "";
|
|
306
|
+
try {
|
|
307
|
+
unlinkSync(filePath);
|
|
308
|
+
}
|
|
309
|
+
catch (e) {
|
|
310
|
+
if (e.code !== "ENOENT") {
|
|
311
|
+
cleanupWarning = `\nNote: could not remove local draft: ${e.message}`;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
try {
|
|
315
|
+
unlinkSync(join(ARTICLES_DIR, `.${slug}.original.md`));
|
|
316
|
+
}
|
|
317
|
+
catch (e) {
|
|
318
|
+
if (e.code !== "ENOENT") {
|
|
319
|
+
cleanupWarning += `\nNote: could not remove original copy: ${e.message}`;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
return `Pushed successfully.\n\nArticle URL (share this exact link with the user): ${articleUrl}${cleanupWarning}\n\n${JSON.stringify(data, null, 2)}`;
|
|
297
323
|
},
|
|
298
324
|
});
|
|
299
325
|
server.addTool({
|
|
@@ -352,7 +378,7 @@ export function registerArticleTools(server) {
|
|
|
352
378
|
const authLine = auth.loggedIn
|
|
353
379
|
? `Logged in as ${auth.name}.`
|
|
354
380
|
: "Not logged in. Use login to authenticate.";
|
|
355
|
-
const files = readdirSync(ARTICLES_DIR).filter((f) => f.endsWith(".md"));
|
|
381
|
+
const files = readdirSync(ARTICLES_DIR).filter((f) => f.endsWith(".md") && !f.startsWith("."));
|
|
356
382
|
if (files.length === 0) {
|
|
357
383
|
return `${authLine}\n\nLocal articles (0 files in ${ARTICLES_DIR}):\n (none — use download or new to get started)`;
|
|
358
384
|
}
|