From ac76ba32f6589335d422e90ab5a6b0c9b3c9fb2a Mon Sep 17 00:00:00 2001 From: kbe Date: Tue, 19 Aug 2025 16:06:28 +0200 Subject: [PATCH] Display categories --- hugo.toml | 4 +-- layouts/author/list.html | 4 +-- layouts/categories/list.html | 34 ++++++++++++++++++++ layouts/pages/list.html | 56 +++++++++++++++++++++++++++++++++ scripts/fetch-wordpress.js | 53 ++++++++++++++++++++++++++++++- scripts/generate-content.js | 61 ++++++++++++++++++++++++++---------- 6 files changed, 191 insertions(+), 21 deletions(-) create mode 100644 layouts/categories/list.html create mode 100644 layouts/pages/list.html diff --git a/hugo.toml b/hugo.toml index a519689..498abd9 100644 --- a/hugo.toml +++ b/hugo.toml @@ -14,8 +14,8 @@ ignoreLogs = ["warning-goldmark-raw-html"] languageName = 'French' weight = 1 -[taxonomies] - category = "categories" +# [taxonomies] + # category = "categories" [markup.goldmark.renderer] unsafe = true diff --git a/layouts/author/list.html b/layouts/author/list.html index b1361b6..a350c33 100644 --- a/layouts/author/list.html +++ b/layouts/author/list.html @@ -5,7 +5,7 @@
-

Articles by {{ $authorName }}

+

{{ .Title }}

Tous les articles écrits par {{ $authorName }}

@@ -81,4 +81,4 @@ -{{ end }} \ No newline at end of file +{{ end }} diff --git a/layouts/categories/list.html b/layouts/categories/list.html new file mode 100644 index 0000000..1ef5ad2 --- /dev/null +++ b/layouts/categories/list.html @@ -0,0 +1,34 @@ +{{ define "main" }} +{{ $defaultCategory := "General" }} +{{ if .Site.Params.defaultCategory }}{{ $defaultCategory = .Site.Params.defaultCategory }}{{ end }} + +
+
+

{{ .Title }}

+

Cette page répertorie toutes les catégories de notre site, offrant une vue d'ensemble structurée de notre contenu.

+
+
+ + +
+
+
+
+ {{ range $.Site.Data.wordpress.categories }} +
+
+

{{ .name }}

+

{{ .description | safeHTML }}

+ +
+
+ {{ if not .IsLast }}
{{ end }} + {{ end }} +
+
+
+
+ +{{ end }} diff --git a/layouts/pages/list.html b/layouts/pages/list.html new file mode 100644 index 0000000..f39c1f8 --- /dev/null +++ b/layouts/pages/list.html @@ -0,0 +1,56 @@ +{{ define "main" }} +{{ $authorName := .Params.author }} +{{ $authorSlug := .Params.author_slug }} +{{ $authorPosts := where .Site.RegularPages "Params.author" $authorName }} + +
+
+

{{ .Title }}

+

Retrouvez toutes les pages utiles du site ici.

+
+
+ + +
+
+
+
+ {{ $paginationLimit := 10 }} + {{ if .Site.Params.paginationLimit }}{{ $paginationLimit = .Site.Params.paginationLimit }}{{ end }} + {{ $paginator := .Paginate $authorPosts $paginationLimit }} + + {{ if gt (len $authorPosts) 0 }} + {{ range $paginator.Pages }} + +
+
+

{{ .Title }}

+ {{ if .Params.excerpt }} +

{{ .Params.excerpt }}

+ {{ else if .Summary }} +

{{ .Summary }}

+ {{ else }} +

{{ truncate 200 .Content }}

+ {{ end }} + +
+
+ + {{ end }} + + + {{ partial "pagination.html" (dict "Paginator" .Paginator "Page" .) }} + {{ else }} +
+

Aucun article trouvé

+

Aucun article n'a été trouvé pour cet auteur.

+
+ {{ end }} +
+
+
+
+ +{{ end }} diff --git a/scripts/fetch-wordpress.js b/scripts/fetch-wordpress.js index 13dfd93..c10e796 100644 --- a/scripts/fetch-wordpress.js +++ b/scripts/fetch-wordpress.js @@ -68,16 +68,67 @@ async function generateData() { modified: page.modified })); + // Create author-post mapping + const authorPosts = {}; + posts.forEach(post => { + if (post.status === 'publish') { + const author = post._embedded?.author?.[0]; + if (author) { + const authorSlug = author.slug; + if (!authorPosts[authorSlug]) { + authorPosts[authorSlug] = { + id: author.id, + name: author.name, + slug: author.slug, + description: author.description || '', + avatar: author.avatar_urls || {}, + link: author.link, + posts: [] + }; + } + authorPosts[authorSlug].posts.push({ + id: post.id, + title: post.title.rendered, + slug: post.slug, + date: post.date, + excerpt: post.excerpt.rendered, + featured_image: post._embedded?.['wp:featuredmedia']?.[0]?.source_url || '', + categories: (post._embedded?.['wp:term']?.[0] || []).map(cat => ({ + id: cat.id, + name: cat.name, + slug: cat.slug + })), + tags: (post._embedded?.['wp:term']?.[1] || []).map(tag => ({ + id: tag.id, + name: tag.name, + slug: tag.slug + })) + }); + } + } + }); + // Save data as JSON files fs.writeFileSync(path.join(OUTPUT_DIR, 'posts.json'), JSON.stringify(posts, null, 2)); fs.writeFileSync(path.join(OUTPUT_DIR, 'pages.json'), JSON.stringify(publishedPages, null, 2)); + // Extract category descriptions + const categoriesWithDescriptions = categories.map(cat => ({ + id: cat.id, + name: cat.name, + slug: cat.slug, + description: cat.description || '', + count: cat.count || 0 + })); + + // Also save to wordpress directory fs.writeFileSync(path.join(OUTPUT_DIR, 'categories.json'), JSON.stringify(categories, null, 2)); fs.writeFileSync(path.join(OUTPUT_DIR, 'tags.json'), JSON.stringify(tags, null, 2)); fs.writeFileSync(path.join(OUTPUT_DIR, 'authors.json'), JSON.stringify(authors, null, 2)); + fs.writeFileSync(path.join(OUTPUT_DIR, 'author-posts.json'), JSON.stringify(authorPosts, null, 2)); fs.writeFileSync(path.join(OUTPUT_DIR, 'navigation.json'), JSON.stringify(navigationData, null, 2)); console.log(`✅ Fetched ${posts.length} posts, ${publishedPages.length} pages, ${categories.length} categories, ${tags.length} tags, ${authors.length} authors`); console.log(`✅ Generated navigation data with ${navigationData.length} items`); } -generateData().catch(console.error); \ No newline at end of file +generateData().catch(console.error); diff --git a/scripts/generate-content.js b/scripts/generate-content.js index 2951655..1e5767b 100644 --- a/scripts/generate-content.js +++ b/scripts/generate-content.js @@ -161,22 +161,47 @@ function generateAuthorDirectories(posts) { fs.mkdirSync(AUTHORS_DIR, { recursive: true }); } - // Group posts by author + // Group posts by author using proper author data const postsByAuthor = {}; posts.forEach(post => { - const authorName = post._embedded?.author?.[0]?.name || 'Unknown'; - const authorSlug = authorName.toLowerCase().replace(/\s+/g, '-').replace(/[^\w-]/g, ''); - - if (!postsByAuthor[authorSlug]) { - postsByAuthor[authorSlug] = { - name: authorName, - slug: authorSlug, - posts: [] - }; + const author = post._embedded?.author?.[0]; + if (author) { + const authorSlug = author.slug; + const authorName = author.name; + + if (!postsByAuthor[authorSlug]) { + postsByAuthor[authorSlug] = { + id: author.id, + name: authorName, + slug: authorSlug, + description: author.description || '', + avatar: author.avatar_urls || {}, + link: author.link, + posts: [] + }; + } + + postsByAuthor[authorSlug].posts.push(post); + } else { + // Handle unknown author + const unknownSlug = 'unknown'; + const unknownName = 'Unknown'; + + if (!postsByAuthor[unknownSlug]) { + postsByAuthor[unknownSlug] = { + id: 0, + name: unknownName, + slug: unknownSlug, + description: '', + avatar: {}, + link: '', + posts: [] + }; + } + + postsByAuthor[unknownSlug].posts.push(post); } - - postsByAuthor[authorSlug].posts.push(post); }); // Create author directories and index pages @@ -187,13 +212,17 @@ function generateAuthorDirectories(posts) { fs.mkdirSync(authorDir, { recursive: true }); } - // Generate author index page + // Generate author index page with proper metadata const frontmatter = { - title: `Lise des articles de ${author.name}`, + title: `Articles de ${author.name}`, type: 'authors', layout: 'list', author: author.name, - author_slug: author.slug + author_slug: author.slug, + description: author.description, + avatar: author.avatar, + link: author.link, + post_count: author.posts.length }; const content = `--- @@ -209,4 +238,4 @@ ${Object.entries(frontmatter) }); } -generateContent(); \ No newline at end of file +generateContent();