Commit 68398dc9 by PLN (Algolia)

blog: l'énumération descend dans les groupes d'année (4 chiffres) — slug…

blog: l'énumération descend dans les groupes d'année (4 chiffres) — slug toujours basename, scaffold yarn post dans l'année courante, commentaire Sveltia mis au niveau de l'arbre
parent 0e996709
......@@ -6,10 +6,14 @@ import { draftsVisible } from './content';
import { resolveHydraSketch } from './hydra-sketch';
/**
* Blog posts — the bilingual folder model: `content/blog/<slug>/` holds
* Blog posts — the bilingual folder model: a post is a folder holding
* `meta.md` (shared metadata: canonical title, date, description, tags,
* draft, share) plus one `<lang>.md` per language (localized title, optional
* localized description, body). The slug IS the folder name.
* localized description, body). The slug IS the folder basename; physically
* posts live under a year group, `content/blog/<YYYY>/<slug>/` — the year is
* shelf organization only, no URL carries it. The ideas pad
* (`content/blog/ideas/`, flat seed .md files, no meta.md) stays at the root,
* filtered out like any non-post folder.
*
* A post missing one language file is a valid post in the other; accessors
* report that via `untranslated` so the UI can show the honest banner.
......@@ -18,30 +22,80 @@ import { resolveHydraSketch } from './hydra-sketch';
* Drafts follow the same prod/dev rule as every collection (draftsVisible).
*/
const BLOG_ROOT = path.join(process.cwd(), 'content', 'blog');
let blogRoot = path.join(process.cwd(), 'content', 'blog');
/**
* Test seam — point the whole module at another root. Returns the restore
* function so a test's afterEach can put the real tree back.
*/
export function useBlogRoot(root) {
const previous = blogRoot;
blogRoot = root;
return () => {
blogRoot = previous;
};
}
/** Language of `/blog/<slug>` — the canonical URL stays default-language. */
export const DEFAULT_LANG = 'fr';
/** All supported languages, in canonical order. */
export const BLOG_LANGS = ['fr', 'en'];
/** A 4-digit directory under the blog root is a year group, not a post. */
const YEAR_GROUP = /^\d{4}$/;
// A folder with neither meta.md nor a language file is not a post —
// notes, scratch, seeds-in-progress. It mints no slug and no route.
function isPostDir(dir) {
return (
fs.existsSync(path.join(dir, 'meta.md')) ||
BLOG_LANGS.some((lang) => fs.existsSync(path.join(dir, `${lang}.md`)))
);
}
/**
* Enumerate post folders under `root` as `{ slug, dir }` pairs. Depth-1
* listing; a year group is the one directory we descend into (its children
* are the candidate posts). Any other directory stays a leaf: a post when
* isPostDir, else skipped — never descended into. The slug is the basename,
* so a post moves between years without touching a single URL; slugs are
* assumed unique across years (one folder per post, as before).
*/
export function listPostDirs(root) {
const posts = [];
for (const name of fs.readdirSync(root)) {
const dir = path.join(root, name);
if (!fs.statSync(dir).isDirectory()) continue;
if (YEAR_GROUP.test(name)) {
for (const child of fs.readdirSync(dir)) {
const childDir = path.join(dir, child);
if (fs.statSync(childDir).isDirectory() && isPostDir(childDir)) {
posts.push({ slug: child, dir: childDir });
}
}
} else if (isPostDir(dir)) {
posts.push({ slug: name, dir });
}
}
return posts;
}
function postDirs() {
return listPostDirs(blogRoot);
}
function blogSlugs() {
return fs
.readdirSync(BLOG_ROOT)
.filter((name) => fs.statSync(path.join(BLOG_ROOT, name)).isDirectory())
// A folder with neither meta.md nor a language file is not a post —
// notes, scratch, seeds-in-progress. It mints no slug and no route.
.filter((name) => {
const dir = path.join(BLOG_ROOT, name);
return (
fs.existsSync(path.join(dir, 'meta.md')) ||
BLOG_LANGS.some((lang) => fs.existsSync(path.join(dir, `${lang}.md`)))
);
});
return postDirs().map((post) => post.slug);
}
/** Folder of a post — under the year tree the slug alone doesn't locate it. */
function postDir(slug) {
const hit = postDirs().find((post) => post.slug === slug);
return hit ? hit.dir : path.join(blogRoot, slug);
}
function langFile(slug, lang) {
return path.join(BLOG_ROOT, slug, `${lang}.md`);
return path.join(postDir(slug), `${lang}.md`);
}
function hasLang(slug, lang) {
......@@ -64,7 +118,7 @@ function readFrontmatter(file) {
* listing down. Hand-authored posts always carry meta.md.
*/
function readMeta(slug) {
const metaFile = path.join(BLOG_ROOT, slug, 'meta.md');
const metaFile = path.join(postDir(slug), 'meta.md');
if (fs.existsSync(metaFile)) return readFrontmatter(metaFile);
for (const lang of BLOG_LANGS) {
const file = langFile(slug, lang);
......@@ -138,7 +192,7 @@ function readingMinutes(html) {
* `description` stays plain text — the UI renders it, not the markdown chain.
*/
export async function getBlogPost(slug, lang = DEFAULT_LANG) {
if (!fs.existsSync(path.join(BLOG_ROOT, slug))) {
if (!isPostDir(postDir(slug))) {
throw new Error(`No blog post found for blog/${slug}`);
}
const languages = getBlogLanguages(slug);
......
......@@ -22,20 +22,28 @@ backend:
media_folder: public/images/blog
public_folder: /images/blog
# Blog content model: each post is a folder `content/blog/<slug>/` holding
# meta.md (shared metadata) + fr.md + en.md (one file per language, localized
# title + body). Sveltia/Decap i18n cannot express that layout: their i18n
# structures are single_file (all locales in <slug>.<ext>), multiple_files
# (<slug>.<locale>.<ext>) and multiple_folders (<locale>/<slug>.<ext>) — none
# produces <slug>/<locale>.md, and none has a notion of a shared,
# non-localized meta file. So instead of the i18n machinery, the folder is
# exposed as three monolingual entry collections sharing the same `folder`,
# each pinned to its file by the `path` template (which also keeps unrelated
# files out of each collection's list).
# Blog content model: posts physically live under a year group — each post
# is a folder `content/blog/<YYYY>/<slug>/` holding meta.md (shared
# metadata) + fr.md + en.md (one file per language, localized title + body).
# The slug is the folder BASENAME: no URL carries the year, it is shelf
# organization only. Sveltia/Decap i18n cannot express that layout: their
# i18n structures are single_file (all locales in <slug>.<ext>),
# multiple_files (<slug>.<locale>.<ext>) and multiple_folders
# (<locale>/<slug>.<ext>) — none produces <slug>/<locale>.md, and none has a
# notion of a shared, non-localized meta file. So instead of the i18n
# machinery, the folder is exposed as three monolingual entry collections
# sharing the same `folder`, each pinned to its file by the `path` template
# (which also keeps unrelated files out of each collection's list). With the
# year tree, a listed entry's slug is the path under content/blog minus the
# extension — e.g. `2026/devname` — and the `{{slug}}` template still
# resolves to the same file; `{{dirname}}` in the summaries shows the same
# year-prefixed path.
#
# Workflow: create a post under "Blog" (meta.md), then add its bodies under
# "Blog FR" / "Blog EN" — when creating there, type the post's slug (the
# folder name) so the files land in the same folder.
# Workflow: create a post under "Blog" (meta.md) typing `<year>/<slug>` as
# the slug (e.g. `2026/my-post` — `yarn post <slug>` scaffolds into the
# current year), then add its bodies under "Blog FR" / "Blog EN" — when
# creating there, type the same year-prefixed slug so the files land in the
# same folder.
collections:
- name: blog
label: Blog
......@@ -66,8 +74,9 @@ collections:
- { label: Description, name: description, widget: string, required: false }
- { label: Per-language slug, name: slug, widget: string, required: false, pattern: ['^[a-z0-9]+(-[a-z0-9]+)*$', 'Kebab-case: lowercase letters, digits, hyphens'], hint: 'per-language slug; makes /<slug> + /<slug-en> work directly' }
- { label: Body, name: body, widget: markdown }
# `{{slug}}` on creation comes from the entry slug editor — type the post's
# folder name there; opening an existing file always resolves it correctly.
# `{{slug}}` on creation comes from the entry slug editor — type the
# post's year-prefixed folder path there (`<year>/<slug>`); opening an
# existing file always resolves it correctly.
- name: blog-en
label: Blog EN
......
#!/usr/bin/env node
// Scaffold a blog post folder. Usage: node scripts/new-post.mjs <slug> (or `yarn post <slug>`).
//
// Bilingual folder model: content/blog/<slug>/meta.md (shared metadata) +
// fr.md (the body starts in the site's default language; add en.md by hand or
// via the CMS when the translation is written).
// Bilingual folder model: posts live under a year group,
// content/blog/<YYYY>/<slug>/meta.md (shared metadata) + fr.md (the body
// starts in the site's default language; add en.md by hand or via the CMS
// when the translation is written). The slug is the folder basename — the
// year never appears in a URL.
import fs from 'node:fs';
import path from 'node:path';
......@@ -14,7 +16,7 @@ if (!slug || /[^a-z0-9-]/i.test(slug)) {
}
const date = new Date().toISOString().slice(0, 10);
const dir = path.join(process.cwd(), 'content/blog', slug);
const dir = path.join(process.cwd(), 'content/blog', String(new Date().getFullYear()), slug);
if (fs.existsSync(dir)) {
console.error(`Already exists: ${dir}`);
......
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