Commit 6a73379f by PLN (Algolia)

blog-drafts: le roster live passe par les groupes d'année (sinon ses assertions…

blog-drafts: le roster live passe par les groupes d'année (sinon ses assertions ne voient plus aucun vrai post) — readMetaDraft résout le slug à travers les années
parent 9742bd8d
...@@ -45,20 +45,45 @@ async function withProdEnv(fn) { ...@@ -45,20 +45,45 @@ async function withProdEnv(fn) {
// Live roster from disk — the owner edits fixtures freely; the accessors // Live roster from disk — the owner edits fixtures freely; the accessors
// are what this suite pins, not any particular folder name. Post-ness // are what this suite pins, not any particular folder name. Post-ness
// mirrors the loader: a folder with neither meta.md nor a language file // mirrors the loader: a folder with neither meta.md nor a language file
// (seed notes, scratch) is not a post. // (seed notes, scratch) is not a post; a 4-digit year group is descended
// into and the slug stays the folder basename either way.
const BLOG_ROOT = path.join(process.cwd(), "content", "blog"); const BLOG_ROOT = path.join(process.cwd(), "content", "blog");
const isPostDir = (dir) =>
fs.existsSync(path.join(dir, "meta.md")) ||
BLOG_LANGS.some((lang) => fs.existsSync(path.join(dir, `${lang}.md`)));
const ALL_BLOG_SLUGS = fs const ALL_BLOG_SLUGS = fs
.readdirSync(BLOG_ROOT) .readdirSync(BLOG_ROOT)
.filter((f) => !f.startsWith("_") && fs.statSync(path.join(BLOG_ROOT, f)).isDirectory()) .filter((f) => !f.startsWith("_") && fs.statSync(path.join(BLOG_ROOT, f)).isDirectory())
.filter( .flatMap((f) => {
(f) => const dir = path.join(BLOG_ROOT, f);
fs.existsSync(path.join(BLOG_ROOT, f, "meta.md")) || if (/^\d{4}$/.test(f)) {
BLOG_LANGS.some((lang) => fs.existsSync(path.join(BLOG_ROOT, f, `${lang}.md`))) return fs
); .readdirSync(dir)
.filter((c) => !c.startsWith("_") && fs.statSync(path.join(dir, c)).isDirectory())
.filter((c) => isPostDir(path.join(dir, c)));
}
return isPostDir(dir) ? [f] : [];
});
/** Folder of a live slug — under the year tree the slug alone doesn't
locate it: check the root level, then each year group. */
function livePostDir(slug) {
const rootLevel = path.join(BLOG_ROOT, slug);
if (fs.existsSync(rootLevel)) return rootLevel;
for (const name of fs.readdirSync(BLOG_ROOT)) {
if (!/^\d{4}$/.test(name)) continue;
const candidate = path.join(BLOG_ROOT, name, slug);
if (fs.existsSync(candidate)) return candidate;
}
return null;
}
/** Whether a live folder is currently a draft (owner edits freely). */ /** Whether a live folder is currently a draft (owner edits freely). */
function readMetaDraft(slug) { function readMetaDraft(slug) {
const metaPath = path.join(process.cwd(), "content", "blog", slug, "meta.md"); const dir = livePostDir(slug);
if (!dir) return false;
const metaPath = path.join(dir, "meta.md");
if (!fs.existsSync(metaPath)) return false; if (!fs.existsSync(metaPath)) return false;
return matter(fs.readFileSync(metaPath, "utf8")).data.draft === true; return matter(fs.readFileSync(metaPath, "utf8")).data.draft === true;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment