feat: slug and date derivation
Co-Authored-By: Claude
This commit is contained in:
32
src/slug.test.ts
Normal file
32
src/slug.test.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
import { slugify, deriveSlug, deriveDate, postFilename } from "./slug";
|
||||
|
||||
describe("slugify", () => {
|
||||
test("lowercases, drops apostrophes, hyphenates", () => {
|
||||
expect(slugify("On Making a Game")).toBe("on-making-a-game");
|
||||
expect(slugify("It's a Test!")).toBe("its-a-test");
|
||||
expect(slugify(" Spaced out ")).toBe("spaced-out");
|
||||
});
|
||||
});
|
||||
|
||||
describe("deriveSlug", () => {
|
||||
test("prefers title, falls back to filename", () => {
|
||||
expect(deriveSlug({ title: "Hello World", filename: "note" })).toBe("hello-world");
|
||||
expect(deriveSlug({ title: " ", filename: "My Note" })).toBe("my-note");
|
||||
});
|
||||
});
|
||||
|
||||
describe("deriveDate", () => {
|
||||
const now = new Date("2026-06-19T12:00:00Z");
|
||||
test("uses frontmatter Date or string, else now", () => {
|
||||
expect(deriveDate({ frontmatterDate: new Date("2026-02-06T00:00:00Z"), now })).toBe("2026-02-06");
|
||||
expect(deriveDate({ frontmatterDate: "2026-02-18 09:00", now })).toBe("2026-02-18");
|
||||
expect(deriveDate({ now })).toBe("2026-06-19");
|
||||
});
|
||||
});
|
||||
|
||||
describe("postFilename", () => {
|
||||
test("joins date and slug", () => {
|
||||
expect(postFilename({ date: "2026-06-19", slug: "hello" })).toBe("2026-06-19-hello.md");
|
||||
});
|
||||
});
|
||||
29
src/slug.ts
Normal file
29
src/slug.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
export function slugify(s: string): string {
|
||||
return s
|
||||
.toLowerCase()
|
||||
.trim()
|
||||
.replace(/['']/g, "")
|
||||
.replace(/[^a-z0-9]+/g, "-")
|
||||
.replace(/^-+|-+$/g, "");
|
||||
}
|
||||
|
||||
export function deriveSlug(o: { title?: string; filename: string }): string {
|
||||
const base = o.title && o.title.trim() ? o.title : o.filename;
|
||||
return slugify(base);
|
||||
}
|
||||
|
||||
export function deriveDate(o: { frontmatterDate?: unknown; now: Date }): string {
|
||||
const d = o.frontmatterDate;
|
||||
if (d instanceof Date && !isNaN(d.getTime())) return fmt(d);
|
||||
if (typeof d === "string" && /^\d{4}-\d{2}-\d{2}/.test(d)) return d.slice(0, 10);
|
||||
return fmt(o.now);
|
||||
}
|
||||
|
||||
function fmt(d: Date): string {
|
||||
const p = (n: number) => String(n).padStart(2, "0");
|
||||
return `${d.getUTCFullYear()}-${p(d.getUTCMonth() + 1)}-${p(d.getUTCDate())}`;
|
||||
}
|
||||
|
||||
export function postFilename(o: { date: string; slug: string }): string {
|
||||
return `${o.date}-${o.slug}.md`;
|
||||
}
|
||||
Reference in New Issue
Block a user