Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package-lock.json
# build output
dist/
.cache/
src/content/posts/

# generated types
.astro/
Expand Down
3 changes: 3 additions & 0 deletions __mocks__/astro:content.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { vi } from "vitest";

export const getCollection = vi.fn();
13 changes: 8 additions & 5 deletions astro.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineConfig } from "astro/config";
import prefetch from "@astrojs/prefetch";
import getRedirects from "./src/lib/getRedirects";
// import getRedirects from "./src/lib/getRedirects";

import sitemap from "@astrojs/sitemap";
import "dotenv/config";
Expand All @@ -11,14 +11,17 @@ export default defineConfig({
image: {
domains: ["blog.beeminder.com", "user-images.githubusercontent.com"],
},
redirects: await getRedirects().catch((e) => {
console.error(e);
throw e;
}),
// redirects: await getRedirects().catch((e) => {
// console.error(e);
// throw e;
// }),
integrations: [
prefetch({
selector: "a",
}),
sitemap(),
],
experimental: {
contentCollectionCache: true,
},
});
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"scripts": {
"dev": "astro dev",
"start": "astro dev",
"build": "tsx ./scripts/image-cache.ts && astro build",
"build": " pnpm md:pull && tsx ./scripts/image-cache.ts && astro build",
"preview": "astro preview",
"astro": "astro",
"test": "vitest",
Expand All @@ -22,7 +22,8 @@
"lint": "eslint .",
"format": "prettier --write .",
"preinstall": "npx only-allow pnpm",
"puppeteer": "tsx ./scripts/puppeteer.ts"
"puppeteer": "tsx ./scripts/puppeteer.ts",
"md:pull": "tsx ./scripts/pull.ts"
},
"dependencies": {
"@astrojs/prefetch": "^0.4.1",
Expand All @@ -42,6 +43,7 @@
"memize": "^2.1.0",
"node-fetch-cache": "^3.1.3",
"p-limit": "^4.0.0",
"sanitize-filename": "^1.6.3",
"sanitize-html": "^2.11.0",
"sharp": "^0.32.5",
"striptags": "^3.2.0",
Expand Down
19 changes: 19 additions & 0 deletions pnpm-lock.yaml

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

26 changes: 26 additions & 0 deletions scripts/pull.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import fs from "fs";
import path from "path";
import "dotenv/config";
import fetchPosts from "../src/lib/fetchPosts";
import sanitize from "sanitize-filename";

const promises = fetchPosts();
const dir = "./src/content/posts";

console.log("Delete the directory if it does exist");
if (fs.existsSync(dir)) {
fs.rmdirSync(dir, { recursive: true });
}

console.log("Create directory");
fs.mkdirSync(dir, { recursive: true });

promises.forEach((p) => {
p.then((post) => {
console.log(post.slug, post.source);
fs.writeFileSync(
path.join(dir, `${sanitize(String(post.source))}.json`),
JSON.stringify(post, null, 2),
);
});
});
7 changes: 7 additions & 0 deletions src/content/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineCollection } from "astro:content";

export const collections = {
posts: defineCollection({
type: "data",
}),
};
1 change: 1 addition & 0 deletions src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/// <reference path="../.astro/types.d.ts" />
/// <reference types="astro/client" />
2 changes: 1 addition & 1 deletion src/lib/env.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// WORKAROUND: `import.meta.env` is not available during Astro config evaluation.
// https://docs.astro.build/en/guides/configuring-astro/#environment-variables
export default function env(key: string): string | undefined {
return import.meta.env[key] || process.env[key];
return import.meta.env?.[key] || process.env[key];
}
50 changes: 44 additions & 6 deletions src/lib/getArchives.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@ import { describe, it, expect, beforeEach, vi } from "vitest";
import getArchives from "./getArchives";
import readSources from "./readSources";
import meta from "./test/meta";
import ether from "./test/ether";
import { getCollection } from "astro:content";

describe("getArchives", () => {
beforeEach(() => {
vi.mocked(readSources).mockReturnValue([meta()]);
vi.mocked(getCollection).mockReturnValue([
{
data: {
...meta(),
md: ether(),
},
},
] as any);
});

it("batches by month", async () => {
Expand All @@ -32,18 +41,47 @@ describe("getArchives", () => {
});

it("handles three posts in one month", async () => {
vi.mocked(readSources).mockReturnValue([meta(), meta(), meta()]);
vi.mocked(getCollection).mockReturnValue([
{
data: {
...meta(),
md: ether(),
},
},
{
data: {
...meta(),
md: ether(),
},
},
{
data: {
...meta(),
md: ether(),
},
},
] as any);

const result = await getArchives();

expect(result[0]?.months[0]?.posts).toHaveLength(3);
});

it("sorts posts by date", async () => {
vi.mocked(readSources).mockReturnValue([
meta({ title: "A", date: "2013-02-22" }),
meta({ title: "B", date: "2013-02-21" }),
]);
vi.mocked(getCollection).mockReturnValue([
{
data: {
...meta({ title: "A", date: "2013-02-22" }),
md: ether(),
},
},
{
data: {
...meta({ title: "B", date: "2013-02-21" }),
md: ether(),
},
},
] as any);

const result = await getArchives();

Expand Down
Loading