Skip to content

Commit

Permalink
feat: default to the day that a file was committed to git for it's pu…
Browse files Browse the repository at this point in the history
…blish date
  • Loading branch information
S1rDev10us committed Nov 26, 2024
1 parent 32b1d9c commit a6f6ac0
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 19 deletions.
10 changes: 7 additions & 3 deletions src/components/CreationPreview.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
import type { CollectionEntry } from "astro:content";
import Icon from "./Icon";
import Countdown from "./Countdown.astro";
import { defaultReleaseDate } from "@scripts/lib";
export type Props = CollectionEntry<"creations">;
const { data, slug } = Astro.props;
const { data, slug, id } = Astro.props;
const dateReleased = defaultReleaseDate(data.released, "creations", id);
---

<div
Expand Down Expand Up @@ -48,11 +52,11 @@ const { data, slug } = Astro.props;
</div>
</div>
{
data.released ? (
dateReleased ? (
<div class="card-footer text-center text-body-secondary">
<div class="row">
<div class="col">
<Countdown date={+data.released} />
<Countdown date={dateReleased} />
</div>
</div>
</div>
Expand Down
12 changes: 7 additions & 5 deletions src/components/PostPreview.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
import type { CollectionEntry } from "astro:content";
import Icon from "./Icon";
import Countdown from "./Countdown.astro";
import { isOnline } from "../scripts/data";
import { isOnline } from "@scripts/data";
import { formatDate, defaultReleaseDate } from "@scripts/lib";
export type Props = CollectionEntry<"posts">;
const { data, slug } = Astro.props;
const { data, slug, id } = Astro.props;
const pubDate = defaultReleaseDate(data.pubDate, "posts", id);
---

<div
Expand Down Expand Up @@ -82,7 +84,7 @@ const { data, slug } = Astro.props;
<div class="row align-items-center">
<div class="col border-end">
{
+data.editDate != +data.pubDate ? (
data.editDate && data.editDate.getTime() != pubDate.getTime() ? (
<div class="row border-bottom">
<Countdown
pastText="Edited"
Expand All @@ -93,11 +95,11 @@ const { data, slug } = Astro.props;
) : undefined
}
<div class="row">
<Countdown date={data.pubDate} />
<Countdown date={pubDate} />
</div>
</div>
<div class="col-auto border-start">
{Intl.DateTimeFormat("en-UK").format(data.pubDate)}
{formatDate(pubDate)}
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/content/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const creations = defineCollection({
})
.array()
.optional(),
released: dateSchema,
released: dateSchema.optional(),
developedStart: dateSchema,
developedEnd: dateSchema.optional(),
}),
Expand Down
2 changes: 1 addition & 1 deletion src/content/creations/cuddly-computing-machine.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ externalLinks:
title: "Source Code"
- url: "https://github.com/S1rDev10us/Python-CuddlyComputingGameData"
title: "Examples"
released: 2023-08-15
released: 2023-09-10
developedStart: 2022-5-12
developedEnd: 2022-10-31
---
Expand Down
2 changes: 1 addition & 1 deletion src/content/creations/website.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: "The website you are looking at right now!"
externalLinks:
- url: "/post/blog/website-release"
title: "Release notes"
released: 2023-08-16
released: 2023-9-10
developedStart: 2023-4-1
---

Expand Down
1 change: 0 additions & 1 deletion src/content/posts/blog/website-release.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
title: "My website releases!"
pubDate: 2023-10-25
editDate: 2023-11-12
draft: false
description: "I have finally reached the end of a long journey to develop my website"
Expand Down
8 changes: 6 additions & 2 deletions src/layouts/PostLayout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import type { CollectionEntry } from "astro:content";
import Overlay from "./Overlay.astro";
import { formatDate } from "../scripts/lib";
import { defaultReleaseDate } from "@scripts/lib";
export interface Props<TCollection extends "creations" | "posts"> {
collection: TCollection;
Expand All @@ -16,8 +17,11 @@ if (!title) throw Error(`Post "${post.id}" does not have a title defined`);
const { minutesRead: readtime } = frontmatter;
const releaseDate = new Date(frontmatter.released ?? frontmatter.pubDate);
const releaseDate = defaultReleaseDate(
frontmatter.released ?? frontmatter.pubDate,
collection,
post.id,
);
if (isNaN(releaseDate.getTime()))
throw Error(`Post "${post.id}" does not have a valid date`);
Expand Down
6 changes: 4 additions & 2 deletions src/pages/rss.xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import rss from "@astrojs/rss";
import type { APIContext } from "astro";
import { name } from "../scripts/data";
import { getCollection } from "astro:content";
import { defaultReleaseDate } from "@scripts/lib";

export async function GET(context: APIContext) {
const posts = await getCollection("posts");
Expand All @@ -13,15 +14,16 @@ export async function GET(context: APIContext) {
items: posts
.sort(
(a, b) =>
(b.data.pubDate?.getTime() ?? 0) - (a.data.pubDate?.getTime() ?? 0)
defaultReleaseDate(b.data.pubDate, "posts", b.id).getTime() -
defaultReleaseDate(a.data.pubDate, "posts", b.id).getTime(),
)
.map((post) => ({
...post.data,
customData:
(post.data.customData ?? "") +
`<editDate>${
post.data.editDate?.toUTCString() ??
post.data.pubDate?.toUTCString()
defaultReleaseDate(post.data.pubDate, "posts", post.id).getTime()
}</editDate>`,
link: `/posts/${post.slug}`,
}))
Expand Down
8 changes: 5 additions & 3 deletions src/scripts/data.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type CollectionEntry, getCollection } from "astro:content";
import { Theme } from "./themes";
import os from "os";
import { range } from "./lib";
import { range, defaultReleaseDate } from "./lib";
import type { iconName } from "../components/Icon";

export async function getPostCollectionLength() {
Expand Down Expand Up @@ -85,7 +85,7 @@ export async function getPosts(): Promise<CollectionEntry<"posts">[]> {
id: "blog/website-release.md",
render,
slug: "blog/website-release",
})
}),
);
} else {
posts = await getCollection("posts");
Expand All @@ -104,7 +104,9 @@ export async function getPosts(): Promise<CollectionEntry<"posts">[]> {
}
export async function getCreations(): Promise<CollectionEntry<"creations">[]> {
const creations = (await getCollection("creations")).sort(
(a, b) => +(b.data.released ?? 0) - +(a.data.released ?? 0)
(a, b) =>
+defaultReleaseDate(b.data.released, "creations", b.id) -
+defaultReleaseDate(a.data.released, "creations", a.id),
);
return creations;
}
Expand Down
21 changes: 21 additions & 0 deletions src/scripts/lib.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { execSync } from "child_process";

function capitaliseWith(str: string, splitter: string) {
return str
.split(splitter)
Expand Down Expand Up @@ -37,3 +39,22 @@ export function formatDate(date: Date) {
date.getUTCDate()
].join(separator);
}

export function defaultReleaseDate(
date: any | undefined,
collections: string,
id: string,
): Date {
if (date) return new Date(date);
return fileCommitedOn(`src/content/${collections}/${id}`);
}
export function fileCommitedOn(fileLoc: string): Date {
const command = `git log --diff-filter=A -1 --follow --format=%ad --date default -- "${fileLoc}"`;
const output = execSync(command).toString();
console.log(command);
console.log(output);
if (output.trim() == "") {
return new Date();
}
return new Date(output);
}

0 comments on commit a6f6ac0

Please sign in to comment.