%PDF- %PDF-
| Direktori : /home2/rezakur1/www/portfolio/portfolio-rezakurnia/src/utils/ |
| Current File : /home2/rezakur1/www/portfolio/portfolio-rezakurnia/src/utils/articleBody.ts |
import type { ArticleBodyBlock, ArticlePost } from "./articles";
const defaultSectionTitles = [
"Pengenalan",
"Inti pembahasan",
"Langkah praktis",
"Catatan lanjutan",
];
export function resolveArticleBody(post: ArticlePost): ArticleBodyBlock[] {
if (post.body?.length) return post.body;
return post.content.flatMap((text, i) => {
const id = `bagian-${i + 1}`;
const title = defaultSectionTitles[i] ?? `Bagian ${i + 1}`;
return [
{ type: "heading" as const, id, title },
{ type: "paragraph" as const, text },
];
});
}
export function tocFromBody(body: ArticleBodyBlock[]) {
return body.filter(
(b): b is Extract<ArticleBodyBlock, { type: "heading" }> =>
b.type === "heading",
);
}