Skip to content

Commit 69f39be

Browse files
authored
Merge pull request #13 from eSolia/fix/crosspost-slug-collision
fix(scripts): resolve slug collision in cross-post manifest
2 parents f01392a + 3c8d125 commit 69f39be

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

scripts/cross-post-devto.mts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,11 +480,24 @@ function loadFromFile(filePath: string): ArticleInput {
480480

481481
const raw = readFileSync(absPath, "utf-8");
482482
const { frontmatter, body } = parseFrontmatter(raw);
483-
const slug = basename(absPath, ".md");
483+
const slug = deriveSlug(absPath);
484484

485485
return { slug, frontmatter, body };
486486
}
487487

488+
// Derive a manifest key from the file path. When the markdown file follows the
489+
// convention {article-slug}/devto.md, basename() alone gives "devto", which
490+
// collides across articles and causes silent overwrites in --update mode.
491+
// Fall back to the parent directory name for generic filenames; otherwise use
492+
// the filename as before (backwards-compatible for standalone .md files).
493+
function deriveSlug(absPath: string): string {
494+
const base = basename(absPath, ".md");
495+
if (base === "devto" || base === "qiita") {
496+
return basename(dirname(absPath));
497+
}
498+
return base;
499+
}
500+
488501
function loadFromDir(dirPath: string): ArticleInput[] {
489502
const absDir = resolve(dirPath);
490503
if (!existsSync(absDir)) {

scripts/cross-post-qiita.mts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,24 @@ function loadFromFile(filePath: string): ArticleInput {
295295

296296
const raw = readFileSync(absPath, "utf-8");
297297
const { frontmatter, body } = parseFrontmatter(raw);
298-
const slug = basename(absPath, ".md");
298+
const slug = deriveSlug(absPath);
299299

300300
return { slug, frontmatter, body };
301301
}
302302

303+
// Derive a manifest key from the file path. When the markdown file follows the
304+
// convention {article-slug}/qiita.md, basename() alone gives "qiita", which
305+
// collides across articles and causes silent overwrites in --update mode.
306+
// Fall back to the parent directory name for generic filenames; otherwise use
307+
// the filename as before (backwards-compatible for standalone .md files).
308+
function deriveSlug(absPath: string): string {
309+
const base = basename(absPath, ".md");
310+
if (base === "devto" || base === "qiita") {
311+
return basename(dirname(absPath));
312+
}
313+
return base;
314+
}
315+
303316
function loadFromDir(dirPath: string): ArticleInput[] {
304317
const absDir = resolve(dirPath);
305318
if (!existsSync(absDir)) {

0 commit comments

Comments
 (0)