Skip to content

Commit

Permalink
moved modified times to build time
Browse files Browse the repository at this point in the history
  • Loading branch information
harrybrwn committed Jan 12, 2023
1 parent 167aec4 commit d930deb
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 34 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
dist/
*.tar.gz
*.zip
src/modified.json

# dependencies
node_modules/
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
"url": "https://github.com/harrybrwn/harrybrwn.github.io.git"
},
"scripts": {
"dev": "astro dev",
"start": "astro dev",
"dev": "node scripts/generate.js && astro dev",
"build": "astro build",
"preview": "astro preview",
"astro": "astro",
"clean": "rm -rf ./dist *.tar.gz *.zip",
"package": "scripts/package.sh",
"index": "scripts/gather-links.sh",
"new": "node packages/astro/new/index.js"
"new": "node packages/astro/new/index.js",
"gen": "node scripts/generate.js"
},
"workspaces": [
"./packages/astro/*"
Expand Down
29 changes: 29 additions & 0 deletions scripts/generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import path from "path";
import fs from "fs";
import { spawnSync } from "child_process";
import walk from "walkdir";

const gatherModDates = (dir) => {
let files = {};
walk.sync(dir, function (filename, stat) {
if (stat.isDirectory()) return;
if (path.extname(filename) !== ".md") return;
const name = path.relative(process.cwd(), filename);
const cmd = spawnSync("git", [
"--no-pager",
"log",
"--pretty=format:%cd",
name,
]);
files[name] = cmd.stdout
.toString()
.split("\n")
.map((s) => (s.length > 0 ? new Date(s) : new Date()));
});
return files;
};

const modified = gatherModDates("./content");
const out = "src/modified.json";
fs.writeFileSync(out, JSON.stringify(modified, null, 2));
console.log("modified times written to", out);
32 changes: 3 additions & 29 deletions src/lib/blog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import path from "path";
import fs from "fs";
import type { MarkdownInstance } from "astro";
import GithubSlugger, { slug as toSlugBase } from "github-slugger";
// import modified from "~/modified.json";
import { spawnSync } from "child_process";
import walk from "walkdir";
import modified from "~/modified.json";

export interface Frontmatter extends Record<string, any> {
title: string;
Expand Down Expand Up @@ -50,30 +48,6 @@ export const slug = (
}
};

type Dates = Record<string, Date[]>;

const gatherModDates = (dir: string) => {
let files: Dates = {};
walk.sync(dir, function (filename: string, stat: fs.Stats) {
if (stat.isDirectory()) return;
if (path.extname(filename) !== ".md") return;
const name = path.relative(process.cwd(), filename);
const cmd = spawnSync("git", [
"--no-pager",
"log",
"--pretty=format:%cd",
name,
]);
files[name] = cmd.stdout
.toString()
.split("\n")
.map((s) => (s.length > 0 ? new Date(s) : new Date()));
});
return files;
};

const modified = gatherModDates("./content");

const validate = (frontmatter: Frontmatter) => {
if (!frontmatter.title || frontmatter.title.length === 0) {
throw new Error("markdown post must have a title in the frontmatter");
Expand Down Expand Up @@ -117,8 +91,8 @@ const preparePosts = (
} else {
let name = path.relative(process.cwd(), p.file);
if (name in modified) {
const dates = modified[name];
const d = dates[dates.length - 1];
const dates = (modified as { [key: string]: string[] })[name];
const d = new Date(dates[dates.length - 1]);
p.frontmatter.pubDate = d.toISOString();
} else {
let stat = fs.statSync(p.file);
Expand Down
39 changes: 39 additions & 0 deletions src/modified.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"content/Building Habits.md": [
"2023-01-11T04:58:30.000Z",
"2023-01-09T18:48:15.000Z"
],
"content/C.md": [
"2023-01-11T21:08:36.000Z",
"2023-01-11T04:58:30.000Z"
],
"content/GPG.md": [
"2023-01-12T18:21:03.000Z"
],
"content/Information Retrieval.md": [
"2023-01-12T18:21:03.000Z"
],
"content/Kubernetes Tooling.md": [
"2023-01-11T05:54:40.000Z",
"2023-01-10T02:27:29.000Z",
"2023-01-09T18:48:15.000Z"
],
"content/Open Source Closed Contribution.md": [
"2023-01-12T18:21:03.000Z"
],
"content/blog/against-golang-templates.md": [
"2023-01-11T04:58:30.000Z",
"2023-01-09T03:11:58.000Z"
],
"content/blog/goals-2023.md": [
"2023-01-09T03:11:58.000Z"
],
"content/blog/rebuilding-my-website-in-astro.md": [
"2023-01-11T04:58:30.000Z",
"2023-01-09T03:11:58.000Z"
],
"content/flagship projects.md": [
"2023-01-11T21:08:36.000Z",
"2023-01-09T03:11:58.000Z"
]
}

0 comments on commit d930deb

Please sign in to comment.