Skip to content

Commit

Permalink
frontmatterCompiler: mpv new compiler model
Browse files Browse the repository at this point in the history
  • Loading branch information
julesvirallinen committed Sep 29, 2023
1 parent cc191cf commit e1ca591
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 72 deletions.
8 changes: 4 additions & 4 deletions scripts/generateGardenSettings.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ const gardenSettings = {
showNoteIconInFileTree: false,
showNoteIconOnInternalLink: false,
showNoteIconOnBackLink: false,
showCreatedTimestamp: false,
createdTimestampKey: "",
showUpdatedTimestamp: false,
updatedTimestampKey: "",
showCreatedTimestamp: true,
createdTimestampKey: "customCreated",
showUpdatedTimestamp: true,
updatedTimestampKey: "customUpdated",
timestampFormat: "MMM dd, yyyy h:mm a",
styleSettingsCss: "",
pathRewriteRules: `
Expand Down
79 changes: 22 additions & 57 deletions src/compiler/FrontmatterCompiler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { DateTime } from "luxon";
import { FrontMatterCache, MetadataCache, TFile } from "obsidian";
import { FrontMatterCache } from "obsidian";
import {
getGardenPathForNote,
sanitizePermalink,
Expand All @@ -9,6 +8,7 @@ import {
} from "../utils/utils";
import DigitalGardenSettings from "../models/settings";
import { PathRewriteRules } from "../publisher/DigitalGardenSiteManager";
import { PublishFile } from "../publishFile/PublishFile";

export type TFrontmatter = Record<string, unknown> & {
"dg-path"?: string;
Expand Down Expand Up @@ -38,14 +38,7 @@ export class FrontmatterCompiler {
this.rewriteRules = getRewriteRules(settings.pathRewriteRules);
}

getFrontMatterFromFile(file: TFile, metadataCache: MetadataCache): string {
const frontmatter =
metadataCache.getCache(file.path)?.frontmatter ?? {};

return this.compile(file, frontmatter);
}

compile(file: TFile, frontmatter: FrontMatterCache): string {
compile(file: PublishFile, frontmatter: FrontMatterCache): string {
const fileFrontMatter = { ...frontmatter };
delete fileFrontMatter["position"];

Expand All @@ -56,7 +49,7 @@ export class FrontmatterCompiler {
publishedFrontMatter = this.addPermalink(
fileFrontMatter,
publishedFrontMatter,
file.path,
file.getPath(),
);

publishedFrontMatter = this.addDefaultPassThrough(
Expand Down Expand Up @@ -84,11 +77,8 @@ export class FrontmatterCompiler {
publishedFrontMatter,
);

publishedFrontMatter = this.addTimestampsFrontmatter(
fileFrontMatter,
publishedFrontMatter,
file,
);
publishedFrontMatter =
this.addTimestampsFrontmatter(file)(publishedFrontMatter);

const fullFrontMatter = publishedFrontMatter?.dgPassFrontmatter
? { ...fileFrontMatter, ...publishedFrontMatter }
Expand Down Expand Up @@ -213,53 +203,28 @@ export class FrontmatterCompiler {
return publishedFrontMatter;
}

private addTimestampsFrontmatter(
baseFrontMatter: TFrontmatter,
newFrontMatter: TPublishedFrontMatter,
file: TFile,
) {
if (!baseFrontMatter) {
baseFrontMatter = {};
}
/**
* Adds the created and updated timestamps to the compiled frontmatter if specified in user settings
*/
private addTimestampsFrontmatter =
(file: PublishFile) => (newFrontMatter: TPublishedFrontMatter) => {
//If all note icon settings are disabled, don't change the frontmatter, so that people won't see all their notes as changed in the publication center
const { showCreatedTimestamp, showUpdatedTimestamp } =
this.settings;

//If all note icon settings are disabled, don't change the frontmatter, so that people won't see all their notes as changed in the publication center
if (
!this.settings.showCreatedTimestamp &&
!this.settings.showUpdatedTimestamp
) {
return newFrontMatter;
}
const updatedAt = file.meta.getUpdatedAt();
const createdAt = file.meta.getCreatedAt();

const publishedFrontMatter = { ...newFrontMatter };
const createdKey = this.settings.createdTimestampKey;
const updatedKey = this.settings.updatedTimestampKey;

if (createdKey.length) {
if (typeof baseFrontMatter[createdKey] == "string") {
publishedFrontMatter["created"] = baseFrontMatter[createdKey];
} else {
publishedFrontMatter["created"] = "";
if (createdAt && showCreatedTimestamp) {
newFrontMatter["created"] = createdAt;
}
} else {
publishedFrontMatter["created"] = DateTime.fromMillis(
file.stat.ctime,
).toISO();
}

if (updatedKey.length) {
if (typeof baseFrontMatter[updatedKey] == "string") {
publishedFrontMatter["updated"] = baseFrontMatter[updatedKey];
} else {
publishedFrontMatter["updated"] = "";
if (updatedAt && showUpdatedTimestamp) {
newFrontMatter["updated"] = updatedAt;
}
} else {
publishedFrontMatter["updated"] = DateTime.fromMillis(
file.stat.mtime,
).toISO();
}

return publishedFrontMatter;
}
return newFrontMatter;
};

private addNoteIconFrontMatter(
baseFrontMatter: TFrontmatter,
Expand Down
3 changes: 2 additions & 1 deletion src/dg-testVault/.obsidian/types.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"tags": "tags",
"dg-publish": "checkbox",
"dg-home": "checkbox",
"dg-pinned": "checkbox"
"dg-pinned": "checkbox",
"customCreated": "text"
}
}
5 changes: 5 additions & 0 deletions src/dg-testVault/010 custom createdAt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
dg-publish: true
customCreated: 2020-01-01
---
This file should have createdAt: 2020-01-01
5 changes: 5 additions & 0 deletions src/dg-testVault/011 Custom updatedAt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
dg-publish: true
customUpdated: 2021-01-01
---
This file should have createdAt: 2021-01-01
41 changes: 37 additions & 4 deletions src/publishFile/FileMetaDataManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FrontMatterCache, MetadataCache } from "obsidian";
import { FrontMatterCache, TFile } from "obsidian";
import DigitalGardenSettings from "../models/settings";
import { DateTime } from "luxon";

// This should soon contain all the magic keys instead of them being hardcoded (with documentation)
export enum FRONTMATTER_KEYS {
Expand All @@ -9,21 +10,53 @@ export enum FRONTMATTER_KEYS {
}

export class FileMetadataManager {
file: TFile;
frontmatter: FrontMatterCache;
metadataCache: MetadataCache;
settings: DigitalGardenSettings;

constructor(
file: TFile,
frontmatter: FrontMatterCache,
metadataCache: MetadataCache,
settings: DigitalGardenSettings,
) {
this.file = file;
this.frontmatter = frontmatter;
this.metadataCache = metadataCache;
this.settings = settings;
}

isHome(): boolean {
return !!this.frontmatter[FRONTMATTER_KEYS.HOME];
}

getCreatedAt(): string {
const createdKey = this.settings.createdTimestampKey;

if (createdKey) {
const customCreatedDate = this.frontmatter[createdKey];

if (!customCreatedDate) {
return "";
}

return customCreatedDate;
}

return DateTime.fromMillis(this.file.stat.ctime).toISO() as string;
}

getUpdatedAt(): string {
const updatedKey = this.settings.updatedTimestampKey;

if (updatedKey) {
const customUpdatedDate = this.frontmatter[updatedKey];

if (!customUpdatedDate) {
return "";
}

return this.frontmatter[updatedKey];
}

return DateTime.fromMillis(this.file.stat.mtime).toISO() as string;
}
}
5 changes: 3 additions & 2 deletions src/publishFile/PublishFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export class PublishFile {
this.settings = settings;
this.vault = vault;
this.frontmatter = this.getFrontmatter();
this.meta = new FileMetadataManager(this.frontmatter, metadataCache);

this.meta = new FileMetadataManager(file, this.frontmatter, settings);
}

async compile(): Promise<CompiledPublishFile> {
Expand Down Expand Up @@ -101,7 +102,7 @@ export class PublishFile {
const metadata =
this.metadataCache.getCache(this.file.path)?.frontmatter ?? {};

return frontmatterCompiler.compile(this.file, metadata);
return frontmatterCompiler.compile(this, metadata);
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/publisher/Publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { MetadataCache, Notice, TFile, Vault } from "obsidian";
import { Base64 } from "js-base64";
import { Octokit } from "@octokit/core";
import { getRewriteRules } from "../utils/utils";
import { hasPublishFlag } from "../publishFile/Validator";
import {
hasPublishFlag,
isPublishFrontmatterValid,
} from "../publishFile/Validator";
import { PathRewriteRules } from "./DigitalGardenSiteManager";
import DigitalGardenSettings from "../models/settings";
import { Assets, GardenPageCompiler } from "../compiler/GardenPageCompiler";
Expand Down Expand Up @@ -159,7 +162,7 @@ export default class Publisher {
}

async publish(file: CompiledPublishFile): Promise<boolean> {
if (file.shouldPublish()) {
if (!isPublishFrontmatterValid(file.frontmatter)) {
return false;
}

Expand All @@ -169,7 +172,9 @@ export default class Publisher {
await this.uploadAssets(assets);

return true;
} catch {
} catch (error) {
console.error(error);

return false;
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/snapshot/snapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ I'm a list of all files in this folder:

{ .block-language-dataview}

---
{"dg-publish":true,"permalink":"/011-custom-updated-at/","updated":"2021-01-01"}
---

This file should have createdAt: 2021-01-01
---
{"dg-publish":true,"permalink":"/010-custom-created-at/","created":"2020-01-01"}
---

This file should have createdAt: 2020-01-01
---
{"dg-publish":true,"permalink":"/009-comments/"}
---
Expand Down
2 changes: 1 addition & 1 deletion src/ui/PublicationCenter.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { onMount } from "svelte";
import Publisher from "src/publisher/Publisher";
import Icon from "./Icon.svelte";
import { CompiledPublishFile } from "src/publisher/PublishFile";
import { CompiledPublishFile } from "src/publishFile/PublishFile";
export let publishStatusManager: IPublishStatusManager;
export let publisher: Publisher;
export let showDiff: (path: string) => void;
Expand Down

0 comments on commit e1ca591

Please sign in to comment.