-
-
Notifications
You must be signed in to change notification settings - Fork 18
Add config-based toggling for layout and sections #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,3 +22,6 @@ pnpm-debug.log* | |
|
|
||
| # jetbrains setting folder | ||
| .idea/ | ||
|
|
||
| # Local configuration overrides | ||
| cvfolio.config.local.json | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| "layout": { | ||
| "header": true, | ||
| "footer": true, | ||
| "themeSwitcher": true | ||
| }, | ||
| "sections": { | ||
| "homepage": { | ||
| "author": true, | ||
| "about": true, | ||
| "contact": true, | ||
| "workExperience": true, | ||
| "speaking": true | ||
| }, | ||
| "writing": { | ||
| "author": true, | ||
| "latestPosts": true | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,153 @@ | ||||||||||||||||||||||||||
| import { z } from 'astro/zod'; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| type LayoutConfig = { | ||||||||||||||||||||||||||
| header: boolean; | ||||||||||||||||||||||||||
| footer: boolean; | ||||||||||||||||||||||||||
| themeSwitcher: boolean; | ||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| type HomepageSections = { | ||||||||||||||||||||||||||
| author: boolean; | ||||||||||||||||||||||||||
| about: boolean; | ||||||||||||||||||||||||||
| contact: boolean; | ||||||||||||||||||||||||||
| workExperience: boolean; | ||||||||||||||||||||||||||
| speaking: boolean; | ||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| type WritingSections = { | ||||||||||||||||||||||||||
| author: boolean; | ||||||||||||||||||||||||||
| latestPosts: boolean; | ||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| type SectionsConfig = { | ||||||||||||||||||||||||||
| homepage: HomepageSections; | ||||||||||||||||||||||||||
| writing: WritingSections; | ||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| export type SiteConfig = { | ||||||||||||||||||||||||||
| layout: LayoutConfig; | ||||||||||||||||||||||||||
| sections: SectionsConfig; | ||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const layoutSchema = z | ||||||||||||||||||||||||||
| .object({ | ||||||||||||||||||||||||||
| header: z.boolean().optional(), | ||||||||||||||||||||||||||
| footer: z.boolean().optional(), | ||||||||||||||||||||||||||
| themeSwitcher: z.boolean().optional(), | ||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||
| .optional(); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const homepageSectionsSchema = z | ||||||||||||||||||||||||||
| .object({ | ||||||||||||||||||||||||||
| author: z.boolean().optional(), | ||||||||||||||||||||||||||
| about: z.boolean().optional(), | ||||||||||||||||||||||||||
| contact: z.boolean().optional(), | ||||||||||||||||||||||||||
| workExperience: z.boolean().optional(), | ||||||||||||||||||||||||||
| speaking: z.boolean().optional(), | ||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||
| .optional(); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const writingSectionsSchema = z | ||||||||||||||||||||||||||
| .object({ | ||||||||||||||||||||||||||
| author: z.boolean().optional(), | ||||||||||||||||||||||||||
| latestPosts: z.boolean().optional(), | ||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||
| .optional(); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const sectionsSchema = z | ||||||||||||||||||||||||||
| .object({ | ||||||||||||||||||||||||||
| homepage: homepageSectionsSchema, | ||||||||||||||||||||||||||
| writing: writingSectionsSchema, | ||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||
| .optional(); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const siteConfigSchema = z.object({ | ||||||||||||||||||||||||||
| layout: layoutSchema, | ||||||||||||||||||||||||||
| sections: sectionsSchema, | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| type RawSiteConfig = z.infer<typeof siteConfigSchema>; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| type FlagRecord = Record<string, boolean>; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const DEFAULT_SITE_CONFIG: SiteConfig = { | ||||||||||||||||||||||||||
| layout: { | ||||||||||||||||||||||||||
| header: true, | ||||||||||||||||||||||||||
| footer: true, | ||||||||||||||||||||||||||
| themeSwitcher: true, | ||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||
| sections: { | ||||||||||||||||||||||||||
| homepage: { | ||||||||||||||||||||||||||
| author: true, | ||||||||||||||||||||||||||
| about: true, | ||||||||||||||||||||||||||
| contact: true, | ||||||||||||||||||||||||||
| workExperience: true, | ||||||||||||||||||||||||||
| speaking: true, | ||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||
| writing: { | ||||||||||||||||||||||||||
| author: true, | ||||||||||||||||||||||||||
| latestPosts: true, | ||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| function createDefaultSiteConfig(): SiteConfig { | ||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||
| layout: { ...DEFAULT_SITE_CONFIG.layout }, | ||||||||||||||||||||||||||
| sections: { | ||||||||||||||||||||||||||
| homepage: { ...DEFAULT_SITE_CONFIG.sections.homepage }, | ||||||||||||||||||||||||||
| writing: { ...DEFAULT_SITE_CONFIG.sections.writing }, | ||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| function mergeFlags<T extends FlagRecord>(defaults: T, overrides?: Partial<T>): T { | ||||||||||||||||||||||||||
| return { ...defaults, ...(overrides ?? {}) } as T; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| function formatValidationIssues(error: z.ZodError): string { | ||||||||||||||||||||||||||
| return error.issues | ||||||||||||||||||||||||||
| .map((issue) => { | ||||||||||||||||||||||||||
| const path = issue.path.join('.') || 'root'; | ||||||||||||||||||||||||||
| return ` - ${path}: ${issue.message}`; | ||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||
| .join('\n'); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| function validateSiteConfig(raw: unknown): RawSiteConfig | undefined { | ||||||||||||||||||||||||||
| const result = siteConfigSchema.safeParse(raw); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (result.success) { | ||||||||||||||||||||||||||
| return result.data; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| console.warn('[cvfolio] Invalid cvfolio.config.json detected. Falling back to defaults.'); | ||||||||||||||||||||||||||
| console.warn(formatValidationIssues(result.error)); | ||||||||||||||||||||||||||
| return undefined; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| export async function loadSiteConfig(): Promise<SiteConfig> { | ||||||||||||||||||||||||||
| const defaults = createDefaultSiteConfig(); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||
| const mod = await import('../../cvfolio.config.json?raw'); | ||||||||||||||||||||||||||
| const rawContent = typeof mod.default === 'string' ? mod.default : mod; | ||||||||||||||||||||||||||
| const parsedContent = JSON.parse(rawContent); | ||||||||||||||||||||||||||
| const parsed = validateSiteConfig(parsedContent); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const effective = parsed ?? {}; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const layout = mergeFlags(defaults.layout, effective.layout); | ||||||||||||||||||||||||||
| const sections = { | ||||||||||||||||||||||||||
| homepage: mergeFlags(defaults.sections.homepage, effective.sections?.homepage), | ||||||||||||||||||||||||||
| writing: mergeFlags(defaults.sections.writing, effective.sections?.writing), | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
| const effective = parsed ?? {}; | |
| const layout = mergeFlags(defaults.layout, effective.layout); | |
| const sections = { | |
| homepage: mergeFlags(defaults.sections.homepage, effective.sections?.homepage), | |
| writing: mergeFlags(defaults.sections.writing, effective.sections?.writing), | |
| // Use parsed directly with optional chaining to ensure type safety | |
| const layout = mergeFlags(defaults.layout, parsed?.layout); | |
| const sections = { | |
| homepage: mergeFlags(defaults.sections.homepage, parsed?.sections?.homepage), | |
| writing: mergeFlags(defaults.sections.writing, parsed?.sections?.writing), |
Copilot uses AI. Check for mistakes.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import Author from '@/components/ui/Author.astro'; | |
| import { DEFAULT_CONFIGURATION } from '@/lib/constants'; | ||
| import WorkExperience from '@/components/ui/WorkExperience.astro'; | ||
| import Talk from '@/components/ui/Talk.astro'; | ||
| import { loadSiteConfig } from '@/lib/site-config'; | ||
| import { sortJobsByDate } from '@/lib/utils'; | ||
|
|
||
| const entry = await getEntry('pages', 'homepage'); | ||
|
|
@@ -20,25 +21,35 @@ const links = await getCollection('links'); | |
| const jobs = await getCollection('jobs'); | ||
| const sortedJobs = sortJobsByDate(jobs); | ||
| const talks = await getCollection('talks'); | ||
| const { sections } = await loadSiteConfig(); | ||
|
||
| const homepageSections = sections.homepage; | ||
| --- | ||
|
|
||
| <BaseLayout seo={entry.data.seo}> | ||
| <Container as="section" class="py-6"> | ||
| <Author {...DEFAULT_CONFIGURATION.author} /> | ||
| </Container> | ||
| { | ||
| homepageSections.author && ( | ||
| <Container as="section" class="py-6"> | ||
| <Author {...DEFAULT_CONFIGURATION.author} /> | ||
| </Container> | ||
| ) | ||
| } | ||
|
|
||
| <Container as="section" class="py-6"> | ||
| <div class="flex flex-col gap-6"> | ||
| <div class="flex items-center"> | ||
| <span class="text-headings">About</span> | ||
| </div> | ||
| <div class="prose dark:prose-invert"> | ||
| <Content /> | ||
| </div> | ||
| </div> | ||
| </Container> | ||
| { | ||
| links.length > 0 && ( | ||
| homepageSections.about && ( | ||
| <Container as="section" class="py-6"> | ||
| <div class="flex flex-col gap-6"> | ||
| <div class="flex items-center"> | ||
| <span class="text-headings">About</span> | ||
| </div> | ||
| <div class="prose dark:prose-invert"> | ||
| <Content /> | ||
| </div> | ||
| </div> | ||
| </Container> | ||
| ) | ||
| } | ||
| { | ||
| homepageSections.contact && links.length > 0 && ( | ||
| <Container as="section" class="py-8"> | ||
| <div class="flex flex-col gap-5"> | ||
| <span class="text-headings">Contact</span> | ||
|
|
@@ -66,7 +77,7 @@ const talks = await getCollection('talks'); | |
| ) | ||
| } | ||
| { | ||
| sortedJobs.length > 0 && ( | ||
| homepageSections.workExperience && sortedJobs.length > 0 && ( | ||
| <Container as="section" class="py-6"> | ||
| <div class="flex flex-col gap-5"> | ||
| <span class="text-headings">Work Experience</span> | ||
|
|
@@ -80,7 +91,7 @@ const talks = await getCollection('talks'); | |
| ) | ||
| } | ||
| { | ||
| talks.length > 0 && ( | ||
| homepageSections.speaking && talks.length > 0 && ( | ||
| <Container as="section" class="py-6"> | ||
| <div class="flex flex-col gap-5"> | ||
| <span class="text-headings">Speaking</span> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] pt-24 appears to account for the header; when layout.header is false, this leaves unnecessary top padding. Consider making the padding conditional, e.g., <main class={layout.header ? 'pt-24' : ''}>.
Copilot uses AI. Check for mistakes.