Remove the default date formatting
This commit is contained in:
@@ -1083,7 +1083,7 @@ Co-Authored-By: Claude"
|
|||||||
- Create: `src/publish.ts`, `src/publish.test.ts`
|
- Create: `src/publish.ts`, `src/publish.test.ts`
|
||||||
|
|
||||||
**Interfaces:**
|
**Interfaces:**
|
||||||
- Consumes: `parseNote`, `Pair` (frontmatter); `findImageRefs`, `planImages`, `Strategy` (images); `buildPost`; `postFilename` (slug); `GitClient`, `GitFile` (git); `JekyllPublishSettings`.
|
- Consumes: `parseNote`, `Pair` (frontmatter); `findImageRefs`, `planImages`, `Strategy` (images); `buildPost`; `pageFilename` (slug); `GitClient`, `GitFile` (git); `JekyllPublishSettings`.
|
||||||
- Produces:
|
- Produces:
|
||||||
- `interface PublishInput { noteText: string; frontmatterPairs: Pair[]; slug: string; date: string; strategy: Strategy; commitMessage: string }`
|
- `interface PublishInput { noteText: string; frontmatterPairs: Pair[]; slug: string; date: string; strategy: Strategy; commitMessage: string }`
|
||||||
- `interface ImageResolver { (linktext: string): Promise<Buffer | null> }`
|
- `interface ImageResolver { (linktext: string): Promise<Buffer | null> }`
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import { deriveDate, deriveSlug } from "./slug";
|
|||||||
export interface ModalResult {
|
export interface ModalResult {
|
||||||
frontmatterPairs: Pair[];
|
frontmatterPairs: Pair[];
|
||||||
slug: string;
|
slug: string;
|
||||||
date: string;
|
date?: string;
|
||||||
strategy: Strategy;
|
strategy: Strategy;
|
||||||
commitMessage: string;
|
commitMessage: string;
|
||||||
}
|
}
|
||||||
@@ -38,7 +38,7 @@ export class PublishModal extends Modal {
|
|||||||
this.result = {
|
this.result = {
|
||||||
frontmatterPairs: [],
|
frontmatterPairs: [],
|
||||||
slug: deriveSlug({ title, filename }),
|
slug: deriveSlug({ title, filename }),
|
||||||
date: deriveDate({ frontmatterDate: frontmatter.date, now: new Date() }),
|
date: deriveDate({ frontmatterDate: frontmatter.date }),
|
||||||
strategy: settings.defaultImageStrategy,
|
strategy: settings.defaultImageStrategy,
|
||||||
commitMessage: settings.commitMessageTemplate.replace("{{title}}", title || filename),
|
commitMessage: settings.commitMessageTemplate.replace("{{title}}", title || filename),
|
||||||
};
|
};
|
||||||
@@ -57,8 +57,9 @@ export class PublishModal extends Modal {
|
|||||||
t.setValue(this.result.slug).onChange((v) => (this.result.slug = v))
|
t.setValue(this.result.slug).onChange((v) => (this.result.slug = v))
|
||||||
);
|
);
|
||||||
new Setting(contentEl).setName("Date").addText((t) =>
|
new Setting(contentEl).setName("Date").addText((t) =>
|
||||||
t.setValue(this.result.date).onChange((v) => (this.result.date = v))
|
t.setValue(this.result.date ?? "").onChange((v) => (this.result.date = v))
|
||||||
);
|
).setDesc("The date to prepend to the filename. Useful for blog posts.");
|
||||||
|
|
||||||
new Setting(contentEl).setName("Image strategy").addDropdown((d) =>
|
new Setting(contentEl).setName("Image strategy").addDropdown((d) =>
|
||||||
d.addOption("flat-slug", "Flat, renamed to slug")
|
d.addOption("flat-slug", "Flat, renamed to slug")
|
||||||
.addOption("per-post-folder", "Per-post subfolder")
|
.addOption("per-post-folder", "Per-post subfolder")
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ export class JekyllPublishSettingTab extends PluginSettingTab {
|
|||||||
new Setting(containerEl).setName("Branch")
|
new Setting(containerEl).setName("Branch")
|
||||||
.addText((t) => t.setValue(s.branch).onChange((v) => { s.branch = v; save(); }));
|
.addText((t) => t.setValue(s.branch).onChange((v) => { s.branch = v; save(); }));
|
||||||
new Setting(containerEl).setName("Posts directory")
|
new Setting(containerEl).setName("Posts directory")
|
||||||
.addText((t) => t.setValue(s.defaultPublishDir).onChange((v) => { s.defaultPublishDir = v; save(); }));
|
.addText((t) => t.setValue(s.publishDir).onChange((v) => { s.publishDir = v; save(); }));
|
||||||
new Setting(containerEl).setName("Images directory")
|
new Setting(containerEl).setName("Images directory")
|
||||||
.addText((t) => t.setValue(s.imagesDir).onChange((v) => { s.imagesDir = v; save(); }));
|
.addText((t) => t.setValue(s.imagesDir).onChange((v) => { s.imagesDir = v; save(); }));
|
||||||
new Setting(containerEl).setName("Default image strategy")
|
new Setting(containerEl).setName("Default image strategy")
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { parseNote, Pair } from "./frontmatter";
|
import { parseNote, Pair } from "./frontmatter";
|
||||||
import { buildPost } from "./buildPost";
|
import { buildPost } from "./buildPost";
|
||||||
import { postFilename } from "./slug";
|
import { pageFilename } from "./slug";
|
||||||
import { findImageRefs, planImages, Strategy } from "./images";
|
import { findImageRefs, planImages, Strategy } from "./images";
|
||||||
import { GitClient, GitFile } from "./git";
|
import { GitClient, GitFile } from "./git";
|
||||||
import { JekyllPublishSettings } from "./settings";
|
import { JekyllPublishSettings } from "./settings";
|
||||||
@@ -9,7 +9,7 @@ export interface PublishInput {
|
|||||||
noteText: string;
|
noteText: string;
|
||||||
frontmatterPairs: Pair[];
|
frontmatterPairs: Pair[];
|
||||||
slug: string;
|
slug: string;
|
||||||
date: string;
|
date?: string;
|
||||||
strategy: Strategy;
|
strategy: Strategy;
|
||||||
commitMessage: string;
|
commitMessage: string;
|
||||||
}
|
}
|
||||||
@@ -51,7 +51,7 @@ export async function publish(
|
|||||||
});
|
});
|
||||||
|
|
||||||
const postText = buildPost({ frontmatterPairs: input.frontmatterPairs, body: rewrittenBody });
|
const postText = buildPost({ frontmatterPairs: input.frontmatterPairs, body: rewrittenBody });
|
||||||
const notePath = `${settings.defaultPublishDir}/${postFilename({ date: input.date, slug: input.slug })}`;
|
const notePath = `${settings.publishDir}/${pageFilename({ date: input.date, slug: input.slug })}`;
|
||||||
|
|
||||||
const files: GitFile[] = [{ repoPath: notePath, data: postText }];
|
const files: GitFile[] = [{ repoPath: notePath, data: postText }];
|
||||||
for (const item of plan) {
|
for (const item of plan) {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { Pair } from "./frontmatter";
|
|||||||
export interface JekyllPublishSettings {
|
export interface JekyllPublishSettings {
|
||||||
remoteUrl: string;
|
remoteUrl: string;
|
||||||
branch: string;
|
branch: string;
|
||||||
defaultPublishDir: string;
|
publishDir: string;
|
||||||
imagesDir: string;
|
imagesDir: string;
|
||||||
defaultImageStrategy: Strategy;
|
defaultImageStrategy: Strategy;
|
||||||
presetFrontmatter: Pair[];
|
presetFrontmatter: Pair[];
|
||||||
@@ -16,7 +16,7 @@ export interface JekyllPublishSettings {
|
|||||||
export const DEFAULT_SETTINGS: JekyllPublishSettings = {
|
export const DEFAULT_SETTINGS: JekyllPublishSettings = {
|
||||||
remoteUrl: "",
|
remoteUrl: "",
|
||||||
branch: "main",
|
branch: "main",
|
||||||
defaultPublishDir: "_posts",
|
publishDir: "_posts",
|
||||||
imagesDir: "assets/img",
|
imagesDir: "assets/img",
|
||||||
defaultImageStrategy: "flat-slug",
|
defaultImageStrategy: "flat-slug",
|
||||||
presetFrontmatter: [],
|
presetFrontmatter: [],
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { describe, expect, test } from "vitest";
|
import { describe, expect, test } from "vitest";
|
||||||
import { slugify, deriveSlug, deriveDate, postFilename } from "./slug";
|
import { slugify, deriveSlug, deriveDate, pageFilename } from "./slug";
|
||||||
|
|
||||||
describe("slugify", () => {
|
describe("slugify", () => {
|
||||||
test("lowercases, drops apostrophes, hyphenates", () => {
|
test("lowercases, drops apostrophes, hyphenates", () => {
|
||||||
@@ -19,18 +19,18 @@ describe("deriveSlug", () => {
|
|||||||
describe("deriveDate", () => {
|
describe("deriveDate", () => {
|
||||||
const now = new Date("2026-06-19T12:00:00Z");
|
const now = new Date("2026-06-19T12:00:00Z");
|
||||||
test("uses frontmatter Date or string, else now", () => {
|
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: new Date("2026-02-06T00:00:00Z"), })).toBe("2026-02-06");
|
||||||
expect(deriveDate({ frontmatterDate: "2026-02-18 09:00", now })).toBe("2026-02-18");
|
expect(deriveDate({ frontmatterDate: "2026-02-18 09:00" })).toBe("2026-02-18");
|
||||||
expect(deriveDate({ now })).toBe("2026-06-19");
|
expect(deriveDate({ frontmatterDate: "unparsable" })).toBeUndefined();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("postFilename", () => {
|
describe("postFilename", () => {
|
||||||
test("joins date and slug", () => {
|
test("joins date and slug", () => {
|
||||||
expect(postFilename({ date: "2026-06-19", slug: "hello" })).toBe("2026-06-19-hello.md");
|
expect(pageFilename({ date: "2026-06-19", slug: "hello" })).toBe("2026-06-19-hello.md");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("renders correctly without date", () => {
|
test("renders correctly without date", () => {
|
||||||
expect(postFilename({ slug: "hello-without-date" })).toBe("hello-without-date.md");
|
expect(pageFilename({ slug: "hello-without-date" })).toBe("hello-without-date.md");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -12,11 +12,10 @@ export function deriveSlug(o: { title?: string; filename: string }): string {
|
|||||||
return slugify(base);
|
return slugify(base);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function deriveDate(o: { frontmatterDate?: unknown; now: Date }): string {
|
export function deriveDate(o: { frontmatterDate?: unknown }): string | undefined {
|
||||||
const d = o.frontmatterDate;
|
const d = o.frontmatterDate;
|
||||||
if (d instanceof Date && !isNaN(d.getTime())) return fmt(d);
|
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);
|
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 {
|
function fmt(d: Date): string {
|
||||||
@@ -24,7 +23,7 @@ function fmt(d: Date): string {
|
|||||||
return `${d.getUTCFullYear()}-${p(d.getUTCMonth() + 1)}-${p(d.getUTCDate())}`;
|
return `${d.getUTCFullYear()}-${p(d.getUTCMonth() + 1)}-${p(d.getUTCDate())}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function postFilename(o: { date?: string; slug: string }): string {
|
export function pageFilename(o: { date?: string; slug: string }): string {
|
||||||
if (o.date) {
|
if (o.date) {
|
||||||
return `${o.date}-${o.slug}.md`;
|
return `${o.date}-${o.slug}.md`;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user