Skip to content

Commit 2608b56

Browse files
committed
publishing: Allow setting target format
Adds the ability to choose a default publishing format (currently users can choose between Leaflet.pub, Pckt.blog, or plaintext), as well as adding a per-note override via the `format` frontmatter field.
1 parent 1418f94 commit 2608b56

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

src/commands/publishDocument.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Notice, TFile } from "obsidian";
22
import type AtmospherePlugin from "../main";
3+
import type { ContentFormat } from "../settings";
34
import { createDocument, putDocument, getPublication, markdownToLeafletContent, stripMarkdown, markdownToPcktContent, buildDocumentUrl } from "../lib";
45
import { PublicationSelection, SelectPublicationModal } from "../components/selectPublicationModal";
56
import { type ResourceUri, } from "@atcute/lexicons";
@@ -87,6 +88,17 @@ async function updateFrontMatter(
8788
});
8889
}
8990

91+
function normalizeFormat(raw: unknown): ContentFormat | undefined {
92+
if (typeof raw !== "string") {
93+
return undefined;
94+
}
95+
const trimmed = raw.trim().toLowerCase();
96+
if (trimmed === "leaflet" || trimmed === "pckt" || trimmed === "plaintext") {
97+
return trimmed;
98+
}
99+
return undefined;
100+
}
101+
90102

91103
async function buildDocumentRecord(plugin: AtmospherePlugin, file: TFile): Promise<{ record: SiteStandardDocument.Main; docUri?: ResourceUri }> {
92104
const full = await plugin.app.vault.read(file);
@@ -105,6 +117,7 @@ async function buildDocumentRecord(plugin: AtmospherePlugin, file: TFile): Promi
105117
let path: string | undefined;
106118
let tags: string[] | undefined;
107119
let publishedAt: string | undefined;
120+
let format: ContentFormat | undefined;
108121
if (fm) {
109122
pubUri = fm["atPublication"];
110123
docUri = fm["atDocument"] as ResourceUri;
@@ -114,6 +127,7 @@ async function buildDocumentRecord(plugin: AtmospherePlugin, file: TFile): Promi
114127
tags = fm["tags"] && Array.isArray(fm["tags"]) ? fm["tags"] : undefined;
115128
publishedAt = fm["publishedAt"]; // Preserve existing if updating
116129
}
130+
format = normalizeFormat(fm?.["format"]);
117131

118132
if (!title && plugin.settings.publish.useFirstHeaderAsTitle) {
119133
title = extractFirstH1(content);
@@ -142,10 +156,20 @@ async function buildDocumentRecord(plugin: AtmospherePlugin, file: TFile): Promi
142156
let textContent = stripMarkdown(content);
143157

144158
let richContent: PubLeafletContent.Main | BlogPcktContent.Main | null = null;
159+
const publicationFormat = pubUri ? normalizeFormat(plugin.settings.publicationFormats?.[pubUri]) : undefined;
160+
let contentFormat: ContentFormat = "plaintext";
145161
if (pub?.url.contains("leaflet.pub")) {
146-
richContent = markdownToLeafletContent(content)
162+
contentFormat = "leaflet";
147163
} else if (pub?.url.contains("pckt.blog")) {
148-
richContent = markdownToPcktContent(content)
164+
contentFormat = "pckt";
165+
} else {
166+
contentFormat = format ?? publicationFormat ?? "plaintext";
167+
}
168+
169+
if (contentFormat === "leaflet") {
170+
richContent = markdownToLeafletContent(content);
171+
} else if (contentFormat === "pckt") {
172+
richContent = markdownToPcktContent(content);
149173
}
150174

151175
let record = {

src/settings.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@ export interface AtProtoSettings {
88
publish: {
99
useFirstHeaderAsTitle: boolean;
1010
};
11+
publicationFormats: Record<string, ContentFormat>;
1112
}
1213

14+
export type ContentFormat = "leaflet" | "pckt" | "plaintext";
15+
1316
export const DEFAULT_SETTINGS: AtProtoSettings = {
1417
identifier: "",
1518
appPassword: "",
1619
clipDir: "AtmosphereClips",
1720
publish: {
1821
useFirstHeaderAsTitle: false,
19-
}
22+
},
23+
publicationFormats: {},
2024
};
2125

2226
export class SettingTab extends PluginSettingTab {
@@ -80,5 +84,6 @@ export class SettingTab extends PluginSettingTab {
8084
await this.plugin.saveSettings();
8185
})
8286
);
87+
8388
}
8489
}

0 commit comments

Comments
 (0)