Skip to content
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

Add basic CMS for Dummy site template #6

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions site-template/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions site-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"test": "npm run test:unit -- --run"
},
"devDependencies": {
"@gonzo-engineering/dummy-ui": "^0.0.2",
"@gonzo-engineering/libs": "0.0.2",
"@gonzo-engineering/dummy-ui": "^0.0.3",
"@gonzo-engineering/libs": "0.0.3",
"@sveltejs/adapter-netlify": "^4.3.6",
"@sveltejs/kit": "^2.9.0",
"@sveltejs/vite-plugin-svelte": "^4.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ description: Going forward enquiries are going to be a lot more… hands on
featuredImage: https://images.pexels.com/photos/6627783/pexels-photo-6627783.jpeg
featuredImageAltText: Someone putting on a rubber glove
featuredImageCaption: "Law enforcement has been told to take ‘an Ice Cube approach' to lubrication"
publicationDate: "2024-11-17T12:28:00.000Z"
authorIds:
- j-w-marceau
publicationDate: 2024-11-17T12:28:00.000Z
authors:
- slug: j-w-marceau
section: business-district
tags:
- news
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ description: The truth is painful sometimes
featuredImage: https://upload.wikimedia.org/wikipedia/commons/thumb/e/e6/Havana_-_Cuba_-_Man_giving_a_V_sign_-_1326.jpg/1024px-Havana_-_Cuba_-_Man_giving_a_V_sign_-_1326.jpg
featuredImageAltText: Man making a V sign
featuredImageCaption: "Credit: Associated Press"
publicationDate: "2024-11-01T12:28:00.000Z"
authorIds:
- tintin
publicationDate: 2024-11-01T12:28:00.000Z
authors:
- slug: tintin
section: new-town
tags:
- news
Expand Down
6 changes: 3 additions & 3 deletions site-template/src/data/articles/man-bites-dog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ description: Park goers were appalled to witness a tussle for a tennis ball go s
featuredImage: https://upload.wikimedia.org/wikipedia/commons/thumb/e/e6/Sad_eyes%5E_Explore_March_18%2C_2023_-_Flickr_-_Dog_Light_Photography.jpg/1024px-Sad_eyes%5E_Explore_March_18%2C_2023_-_Flickr_-_Dog_Light_Photography.jpg
featuredImageAltText: A terribly sad looking dog
featuredImageCaption: "Credit: Associated Press"
publicationDate: "2024-11-27T12:28:00.000Z"
authorIds:
- j-w-marceau
publicationDate: 2024-11-27T12:28:00.000Z
authors:
- slug: j-w-marceau
section: new-town
tags:
- news
Expand Down
13 changes: 13 additions & 0 deletions site-template/src/data/articles/man-bumbles-way-to-cms.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
slug: man-bumbles-way-to-cms
title: Man bumbles way to CMS
description: A halfwit finally got something working
featuredImage: /images/3.jpg
featuredImageAltText: Cat floating in space
featuredImageCaption: Cat floating in space
publicationDate: 2025-01-09T00:27:00.000Z
authors:
- slug: frederick-obrien
section: business-district
---
A halfwit finally got something working. Only took him half the evening.
6 changes: 3 additions & 3 deletions site-template/src/data/articles/person-learns-from-mistake.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
slug: person-learns-from-mistake
title: Person learns from mistake
description: A humble local carpenter made history yesterday by becoming the first human this millennia to genuinely learn from an error
publicationDate: "2024-09-27T12:28:00.000Z"
authorIds:
- j-w-marceau
publicationDate: 2024-09-27T12:28:00.000Z
authors:
- slug: j-w-marceau
section: old-town
tags:
- news
Expand Down
17 changes: 17 additions & 0 deletions site-template/src/data/authors/allAuthors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { Author } from "@gonzo-engineering/libs";

const authorObjects: Author[] = [];

const paths = import.meta.glob("/src/data/authors/*.json", { eager: true });

// Combine all author objects into one array
for (const path in paths) {
const file = paths[path];

if (file && typeof file === "object" && "default" in file) {
const author = file.default as Author;
authorObjects.push(author);
}
}

export const allAuthors = authorObjects;
5 changes: 5 additions & 0 deletions site-template/src/data/authors/frederick-obrien.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"slug": "frederick-obrien",
"name": "Frederick O'Brien",
"bio": "Fred is of no particular consequence."
}
4 changes: 4 additions & 0 deletions site-template/src/data/authors/j-w-marceau.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"slug": "j-w-marceau",
"name": "J. W. Marceau"
}
4 changes: 4 additions & 0 deletions site-template/src/data/authors/tintin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"slug": "tintin",
"name": "Tintin"
}
50 changes: 20 additions & 30 deletions site-template/src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Temporary as a proof of concept before moving out of the template

import type { Author, PublicationDetails } from "@gonzo-engineering/libs";
import type { PublicationDetails } from "@gonzo-engineering/libs";

export const PUBLICATION_DETAILS: PublicationDetails = {
name: "The Daily News",
Expand All @@ -19,33 +19,23 @@ export const PUBLICATION_DETAILS: PublicationDetails = {
{ label: "New Town", slug: "new-town" },
{ label: "Business District", slug: "business-district" },
],
navigation: [
{
label: "Old Town",
url: "/old-town",
subNav: [
{ label: "Council", url: "/tags/council" },
{ label: "Planning", url: "/tags/planning" },
],
},
{ label: "New Town", url: "/new-town" },
{
label: "Business District",
url: "/business-district",
subNav: [{ label: "Culture", url: "/tags/culture" }],
},
],
};

export const AUTHORS: Author[] = [
{
id: "j-w-marceau",
name: "J. W. Marceau",
bio: "J. W. Marceau means well.",
navigation: {
header: [
{
label: "Old Town",
url: "/old-town",
subNav: [
{ label: "Council", url: "/tags/council" },
{ label: "Planning", url: "/tags/planning" },
],
},
{ label: "New Town", url: "/new-town" },
{
label: "Business District",
url: "/business-district",
subNav: [{ label: "Culture", url: "/tags/culture" }],
},
],
footer: [],
},
{
id: "tintin",
name: "Tintin",
bio: "Tintin is a reporter and adventurer.",
},
];
};
2 changes: 1 addition & 1 deletion site-template/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<Header
publicationName={publication.name}
navigation={publication.navigation}
navigation={publication.navigation.header}
/>

<!-- <Ad ad={ads.banner} /> -->
Expand Down
4 changes: 2 additions & 2 deletions site-template/src/routes/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { HydratedArticleMetadata } from "$lib/types";
import type { ArticleMetadata } from "@gonzo-engineering/libs";

export async function load({ fetch }) {
const articlesResponse = await fetch("/api/articles");
const articles: HydratedArticleMetadata[] = await articlesResponse.json();
const articles: ArticleMetadata[] = await articlesResponse.json();
return { articles };
}
14 changes: 14 additions & 0 deletions site-template/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,21 @@
<svelte:head>
<title>{data.publication.name}</title>
<meta name="description" content={data.publication.description} />
<script
src="https://identity.netlify.com/v1/netlify-identity-widget.js"
></script>
{@html `<script type="application/ld+json">${JSON.stringify(makePublicationSchema(data.publication))}</script>`}
<script>
if (window.netlifyIdentity) {
window.netlifyIdentity.on("init", (user) => {
if (!user) {
window.netlifyIdentity.on("login", () => {
document.location.href = "/admin/";
});
}
});
}
</script>
</svelte:head>

<FrontBasic articles={data.articles} />
Expand Down
2 changes: 1 addition & 1 deletion site-template/src/routes/[slug]/[slug]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<article>
<ArticleHead
title={article.title}
description={article.description}
description={article.description ? article.description : ""}
publicationDate={article.publicationDate}
authors={article.authors}
/>
Expand Down
42 changes: 24 additions & 18 deletions site-template/src/routes/api/articles/+server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { json } from "@sveltejs/kit";
import type { HydratedArticleMetadata, RawArticleMetadata } from "$lib/types";
import { AUTHORS } from "$lib/constants";
import type { ArticleMetadata } from "@gonzo-engineering/libs";
import { allAuthors } from "../../../data/authors/allAuthors";

async function getPosts() {
let rawPosts: RawArticleMetadata[] = [];
let rawPosts: ArticleMetadata[] = [];

const paths = import.meta.glob("/src/data/articles/*.md", { eager: true });

Expand All @@ -12,8 +12,7 @@ async function getPosts() {
const slug = path.split("/").at(-1)?.replace(".md", "");

if (file && typeof file === "object" && "metadata" in file && slug) {
const metadata = file.metadata as Omit<RawArticleMetadata, "slug">;
const post = { ...metadata, slug } satisfies RawArticleMetadata;
const post = file.metadata as ArticleMetadata;
rawPosts.push(post);
}
}
Expand All @@ -24,19 +23,26 @@ async function getPosts() {
new Date(first.publicationDate).getTime()
);

// @ts-expect-error -- God knows
const postsWithAuthorObjects: HydratedArticleMetadata[] = rawPosts.map(
(post) => {
// Replace author IDs with author objects
const authorObjects = post.authorIds?.map((authorId: string) => {
return AUTHORS.find((author) => author.id === authorId);
});
return {
...post,
authors: authorObjects,
};
}
);
const postsWithAuthorObjects: ArticleMetadata[] = rawPosts.map((post) => {
// Replace author IDs with author objects
const authorObjects = post.authors?.map(
(rawAuthorObject: { slug: string }) => {
const matchingAuthor = allAuthors.find(
(author) => author.slug === rawAuthorObject.slug
);
if (!matchingAuthor) {
throw new Error(
`Author with slug ${rawAuthorObject.slug} not found in authors data`
);
}
return matchingAuthor;
}
);
return {
...post,
authors: authorObjects,
};
});

return postsWithAuthorObjects;
}
Expand Down
12 changes: 5 additions & 7 deletions site-template/src/routes/api/articles/[slug]/+server.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
// import type { Author } from "$lib/types/index.js";
import { AUTHORS } from "$lib/constants.js";
import type { Article } from "$lib/types";
import type { Article, Author } from "@gonzo-engineering/libs";
import { json } from "@sveltejs/kit";
import { render } from "svelte/server";
import { allAuthors } from "../../../../data/authors/allAuthors";

export async function GET({ params }) {
try {
const post = await import(`../../../../data/articles/${params.slug}.md`);
const content = render(post.default).body;

// // Replace author IDs with author objects
// Replace author slugs with author objects
const postsWithAuthorObjects: Article = {
...post.metadata,
authors: post.metadata.authorIds.map((authorId: string) => {
return AUTHORS.find((author) => author.id === authorId);
authors: post.metadata.authors.map((authorObject: Author) => {
return allAuthors.find((author) => author.slug === authorObject.slug);
}),
content,
};
Expand Down
18 changes: 16 additions & 2 deletions site-template/src/routes/api/authors/+server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import { AUTHORS } from "$lib/constants";
import type { Author } from "@gonzo-engineering/libs";
import { json } from "@sveltejs/kit";

export async function GET() {
return json(AUTHORS);
const authorObjects: Author[] = [];

const paths = import.meta.glob("/src/data/authors/*.json", { eager: true });

// Combine all author objects into one array
for (const path in paths) {
const file = paths[path];

if (file && typeof file === "object" && "default" in file) {
const author = file.default as Author;
authorObjects.push(author);
}
}

return json(authorObjects);
}
Loading