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 Oct 1, 2023
1 parent 09eab5a commit 6273f90
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 85 deletions.
8 changes: 4 additions & 4 deletions scripts/generateGardenSettings.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,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
2 changes: 1 addition & 1 deletion src/compiler/DataviewCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class DataviewCompiler {

const dataviewResult = dvApi.tryEvaluate(query.trim(), {
// @ts-expect-error errors are caught
this: dvApi.page(path),
this: dvApi.page(file.getPath()),
});

if (dataviewResult) {
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
2 changes: 1 addition & 1 deletion src/compiler/GardenPageCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
} from "../utils/regexes";
import Logger from "js-logger";
import { DataviewCompiler } from "./DataviewCompiler";
import { PublishFile } from "../publisher/PublishFile";
import { PublishFile } from "../publishFile/PublishFile";

export interface Asset {
path: string;
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 @@ -143,7 +146,7 @@ export default class Publisher {
}

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

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

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

return false;
}
}
Expand Down
36 changes: 25 additions & 11 deletions src/test/snapshot/snapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,8 @@ I'm a list of all files in this folder:
- [[P Plugins/PD Dataview/PD0 - note with summary\|PD0 - note with summary]]
- [[P Plugins/PD Dataview/PD1 Dataview\|PD1 Dataview]]
- [[P Plugins/PD Dataview/PD2 Inline queries\|PD2 Inline queries]]
- [[P Plugins/PE Excalidraw/PE1 Embedded excalidraw\|PE1 Embedded excalidraw]]
- [[P Plugins/PE Excalidraw/PE1 Transcluded excalidraw\|PE1 Transcluded excalidraw]]


{ .block-language-dataview}

==========
Expand Down Expand Up @@ -273,6 +271,22 @@ E Embeds/E02 PNG published.md

![travolta.png](/img/user/A%20Assets/travolta.png)
==========
011 Custom updatedAt.md
==========
---
{"dg-publish":true,"permalink":"/011-custom-updated-at/","updated":"2021-01-01"}
---

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

This file should have createdAt: 2020-01-01
==========
009 Comments.md
==========
---
Expand Down Expand Up @@ -300,6 +314,15 @@ Hello! I'm a pinned note (should be at the top yeah!)
[Custom permalink](https://dg-docs.ole.dev/advanced/note-specific-settings/)


==========
006 Custom title.md
==========
---
{"dg-publish":true,"permalink":"/006-custom-title/","title":"006 THIS IS A CUSTOM TITLE"}
---

[Custom title](https://dg-docs.ole.dev/advanced/note-specific-settings/)

==========
005 Custom filters.md
==========
Expand All @@ -314,15 +337,6 @@ this plugin has custom filter that turns 🌞 (snow emoji) into 🌞 (THE SUN).

🌞🌞🌞🌞🌞🌞🌞🌞🌞🌞🌞🌞🌞

==========
006 Custom title.md
==========
---
{"dg-publish":true,"permalink":"/006-custom-title/","title":"006 THIS IS A CUSTOM TITLE"}
---

[Custom title](https://dg-docs.ole.dev/advanced/note-specific-settings/)

==========
004 Publishing this garden.md
==========
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 6273f90

Please sign in to comment.