Show the author and publication time on articles
Strapi already records which admin user created an entry, so the byline needs no extra field to fill in: the Article content type sets populateCreatorFields and the API layer exposes only that user's firstname and lastname. The name also feeds the BlogPosting structured data. Also fixes the gap below the article header, which until now came from the cover image's margin and therefore vanished on articles without a cover, leaving the body text against the byline.
This commit is contained in:
@@ -1,6 +1,20 @@
|
||||
import type { Article } from '#shared/types/blog'
|
||||
|
||||
type RawArticle = Omit<Article, 'html' | 'summary'> & { content: string | null }
|
||||
interface AdminUser {
|
||||
firstname: string | null
|
||||
lastname: string | null
|
||||
}
|
||||
|
||||
type RawArticle = Omit<Article, 'html' | 'summary' | 'author'> & {
|
||||
content: string | null
|
||||
createdBy?: AdminUser | null
|
||||
}
|
||||
|
||||
/** Joins the admin user's name parts; returns null when neither is set. */
|
||||
function byline(user: AdminUser | null | undefined): string | null {
|
||||
const name = [user?.firstname, user?.lastname].filter(Boolean).join(' ').trim()
|
||||
return name || null
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event): Promise<Article> => {
|
||||
const slug = getRouterParam(event, 'slug')
|
||||
@@ -19,6 +33,11 @@ export default defineEventHandler(async (event): Promise<Article> => {
|
||||
throw createError({ statusCode: 404, statusMessage: 'Article not found' })
|
||||
}
|
||||
|
||||
const { content, ...rest } = article
|
||||
return { ...rest, html: renderMarkdown(content), summary: summarise(content) }
|
||||
const { content, createdBy, ...rest } = article
|
||||
return {
|
||||
...rest,
|
||||
html: renderMarkdown(content),
|
||||
summary: summarise(content),
|
||||
author: byline(createdBy),
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user