Use shared auto index for blog
This commit is contained in:
parent
de4469d08a
commit
c84a9d180f
3 changed files with 19 additions and 161 deletions
|
|
@ -1,73 +0,0 @@
|
|||
import { QuartzPluginData } from "../plugins/vfile"
|
||||
import { Date } from "./Date"
|
||||
import { QuartzComponent, QuartzComponentConstructor, QuartzComponentProps } from "./types"
|
||||
import { resolveRelative } from "../util/path"
|
||||
import { collectGameCultBlogPosts, getGameCultAuthorLabel } from "./gamecult"
|
||||
|
||||
function getBlogCardDescription(page: QuartzPluginData) {
|
||||
const frontmatterDescription = page.frontmatter?.description
|
||||
if (typeof frontmatterDescription === "string" && frontmatterDescription.trim().length > 0) {
|
||||
return frontmatterDescription.trim()
|
||||
}
|
||||
|
||||
const pageDescription = page.description
|
||||
if (typeof pageDescription === "string" && pageDescription.trim().length > 0) {
|
||||
return pageDescription.trim()
|
||||
}
|
||||
|
||||
const socialDeck = page.frontmatter?.socialDeck
|
||||
if (typeof socialDeck === "string" && socialDeck.trim().length > 0) {
|
||||
return socialDeck.trim()
|
||||
}
|
||||
|
||||
return "This post exists, which is already more than many ideas manage."
|
||||
}
|
||||
|
||||
export default (() => {
|
||||
const GameCultBlogFolder: QuartzComponent = ({ cfg, fileData, allFiles }: QuartzComponentProps) => {
|
||||
const posts = collectGameCultBlogPosts(allFiles)
|
||||
const intro = fileData.description
|
||||
|
||||
return (
|
||||
<div class="popover-hint gamecult-blog-index">
|
||||
{intro && (
|
||||
<section class="gamecult-blog-index-intro">
|
||||
<p>{intro}</p>
|
||||
</section>
|
||||
)}
|
||||
<div class="gamecult-blog-card-list">
|
||||
{posts.map((post) => {
|
||||
const title = post.frontmatter?.title ?? post.slug ?? "Untitled post"
|
||||
const href = resolveRelative(fileData.slug!, post.slug!)
|
||||
const author = getGameCultAuthorLabel(post)
|
||||
const date = post.dates?.published ?? post.dates?.created ?? post.dates?.modified
|
||||
|
||||
return (
|
||||
<article class="gamecult-blog-card">
|
||||
<a href={href} class="gamecult-blog-card-link">
|
||||
<div class="gamecult-blog-card-head">
|
||||
<h2>{title}</h2>
|
||||
<p class="gamecult-blog-card-meta">
|
||||
<span class="gamecult-blog-card-author">{author}</span>
|
||||
{date && (
|
||||
<>
|
||||
<span class="gamecult-blog-card-meta-separator">/</span>
|
||||
<span class="gamecult-blog-card-date">
|
||||
<Date date={date} locale={cfg.locale} />
|
||||
</span>
|
||||
</>
|
||||
)}
|
||||
</p>
|
||||
</div>
|
||||
<p class="gamecult-blog-card-description">{getBlogCardDescription(post)}</p>
|
||||
</a>
|
||||
</article>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return GameCultBlogFolder
|
||||
}) satisfies QuartzComponentConstructor
|
||||
|
|
@ -13,7 +13,7 @@ import {
|
|||
} from "../util/path"
|
||||
import { QuartzComponent, QuartzComponentConstructor, QuartzComponentProps } from "./types"
|
||||
import FolderContent from "./pages/FolderContent"
|
||||
import GameCultBlogFolder from "./GameCultBlogFolder"
|
||||
import AutoIndexFolder from "./AutoIndexFolder"
|
||||
import { resolveGameCultReferenceSlug, resolveGameCultSourceFile, stripTopTagline } from "./gamecult"
|
||||
|
||||
interface Options {
|
||||
|
|
@ -170,7 +170,16 @@ function getCompositeSections(
|
|||
export default ((opts?: Partial<Options>) => {
|
||||
const options: Options = { ...defaultOptions, ...opts }
|
||||
const DefaultFolderContent = FolderContent()
|
||||
const BlogFolderContent = GameCultBlogFolder()
|
||||
const BlogFolderContent = AutoIndexFolder({
|
||||
rootSlug: "Blog",
|
||||
classPrefix: "gamecult-blog",
|
||||
hideFrontmatterKey: "hideFromBlogIndex",
|
||||
defaultAuthor: "GameCult",
|
||||
emptyDescription: "This post exists, which is already more than many ideas manage.",
|
||||
showDescriptionIntro: true,
|
||||
sidebarTagline: "Recent notes, fiction, experiments, and other escaped materials.",
|
||||
sidebarSummary: (count) => `${count} public posts, newest trouble first.`,
|
||||
})
|
||||
|
||||
const GameCultCompositeContent: QuartzComponent = (props: QuartzComponentProps) => {
|
||||
const { fileData, tree, allFiles } = props
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import { toString } from "hast-util-to-string"
|
|||
import { QuartzPluginData } from "../plugins/vfile"
|
||||
import { FullSlug, simplifySlug, splitAnchor, stripSlashes } from "../util/path"
|
||||
import { clone } from "../util/clone"
|
||||
import { buildAutoIndexSidebarData } from "./AutoIndexFolder"
|
||||
|
||||
export type GameCultSidebarLink = {
|
||||
label: string
|
||||
|
|
@ -29,10 +30,6 @@ export type GameCultPageContext = {
|
|||
sidebar?: GameCultSidebarData
|
||||
}
|
||||
|
||||
type GameCultBlogPost = QuartzPluginData & {
|
||||
slug: FullSlug
|
||||
}
|
||||
|
||||
type FrontmatterSidebarLink = {
|
||||
label?: unknown
|
||||
slug?: unknown
|
||||
|
|
@ -410,87 +407,6 @@ function extractFrontmatterOverviewGroups(file: QuartzPluginData): GameCultSideb
|
|||
return groups.length > 0 ? groups : undefined
|
||||
}
|
||||
|
||||
function blogDateValue(file: QuartzPluginData) {
|
||||
return file.dates?.published ?? file.dates?.created ?? file.dates?.modified
|
||||
}
|
||||
|
||||
export function getGameCultAuthorLabel(file: QuartzPluginData) {
|
||||
const author = file.frontmatter?.author
|
||||
if (typeof author === "string" && author.trim().length > 0) {
|
||||
return author.trim()
|
||||
}
|
||||
|
||||
const authors = file.frontmatter?.authors
|
||||
if (Array.isArray(authors)) {
|
||||
const names = authors
|
||||
.filter((entry): entry is string => typeof entry === "string" && entry.trim().length > 0)
|
||||
.map((entry) => entry.trim())
|
||||
|
||||
if (names.length > 0) {
|
||||
return names.join(", ")
|
||||
}
|
||||
}
|
||||
|
||||
return "GameCult"
|
||||
}
|
||||
|
||||
export function collectGameCultBlogPosts(allFiles: QuartzPluginData[]): GameCultBlogPost[] {
|
||||
return allFiles
|
||||
.filter(
|
||||
(file): file is GameCultBlogPost =>
|
||||
typeof file.slug === "string" &&
|
||||
file.slug.startsWith("Blog/") &&
|
||||
file.slug !== "Blog/index" &&
|
||||
file.frontmatter?.hideFromBlogIndex !== true &&
|
||||
!!file.frontmatter?.title,
|
||||
)
|
||||
.sort((a, b) => {
|
||||
const aDate = blogDateValue(a)
|
||||
const bDate = blogDateValue(b)
|
||||
|
||||
if (aDate && bDate) {
|
||||
return bDate.getTime() - aDate.getTime()
|
||||
}
|
||||
|
||||
if (aDate && !bDate) {
|
||||
return -1
|
||||
}
|
||||
|
||||
if (!aDate && bDate) {
|
||||
return 1
|
||||
}
|
||||
|
||||
return (a.frontmatter?.title ?? "").localeCompare(b.frontmatter?.title ?? "")
|
||||
})
|
||||
}
|
||||
|
||||
function buildBlogSidebarData(file: QuartzPluginData, allFiles: QuartzPluginData[]): GameCultSidebarData {
|
||||
const posts = collectGameCultBlogPosts(allFiles)
|
||||
const groupsByYear = new Map<string, GameCultSidebarLink[]>()
|
||||
|
||||
for (const post of posts) {
|
||||
const date = blogDateValue(post)
|
||||
const year = date ? date.getUTCFullYear().toString() : "Undated"
|
||||
const existing = groupsByYear.get(year) ?? []
|
||||
existing.push({
|
||||
label: post.frontmatter?.title ?? post.slug,
|
||||
slug: post.slug,
|
||||
})
|
||||
groupsByYear.set(year, existing)
|
||||
}
|
||||
|
||||
return {
|
||||
title: file.frontmatter?.title ?? "Blog",
|
||||
slug: file.slug as FullSlug,
|
||||
tagline: "Recent notes, fiction, experiments, and other escaped materials.",
|
||||
summary: `${posts.length} public posts, newest trouble first.`,
|
||||
groups: [...groupsByYear.entries()].map(([title, links]) => ({
|
||||
title,
|
||||
links,
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
function overviewCandidates(currentSlug: FullSlug, includeCurrent = true) {
|
||||
if (currentSlug === "index") {
|
||||
return includeCurrent ? (["index"] as FullSlug[]) : []
|
||||
|
|
@ -575,7 +491,13 @@ export function buildGameCultPageContext(
|
|||
const overviewNote = findSidebarOverviewNote(currentFile.slug, allFiles)
|
||||
const sidebar = overviewNote
|
||||
? overviewNote.slug === ("Blog/index" as FullSlug)
|
||||
? buildBlogSidebarData(overviewNote, allFiles)
|
||||
? buildAutoIndexSidebarData(overviewNote, allFiles, {
|
||||
rootSlug: "Blog",
|
||||
hideFrontmatterKey: "hideFromBlogIndex",
|
||||
defaultAuthor: "GameCult",
|
||||
sidebarTagline: "Recent notes, fiction, experiments, and other escaped materials.",
|
||||
sidebarSummary: (count) => `${count} public posts, newest trouble first.`,
|
||||
})
|
||||
: extractOverviewData(overviewNote)
|
||||
: undefined
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue