Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/commands/publishDocument.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Notice, TFile } from "obsidian";
import type AtmospherePlugin from "../main";
import type { ContentFormat } from "../settings";
import { createDocument, putDocument, getPublication, markdownToLeafletContent, stripMarkdown, markdownToPcktContent, buildDocumentUrl, resolveWikilinks } from "../lib";
import { PublicationSelection, SelectPublicationModal } from "../components/selectPublicationModal";
import { type ResourceUri, } from "@atcute/lexicons";
Expand Down Expand Up @@ -87,6 +88,17 @@ async function updateFrontMatter(
});
}

function normalizeFormat(raw: unknown): ContentFormat | undefined {
if (typeof raw !== "string") {
return undefined;
}
const trimmed = raw.trim().toLowerCase();
if (trimmed === "leaflet" || trimmed === "pckt" || trimmed === "plaintext") {
return trimmed;
}
return undefined;
}


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

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same thing here, format property from the frontmatter is not respected if the publication has URL that matches leaflet or pckt.

I've thought that mapping a publication to a content type format might be better done via some type of UI interaction. But for now I think a frontmatter property is a good option.

Let's update this PR to remove the publicationFormats setting since:

  1. its not shown in the settings UI and is more verbose
  2. showing it in the settings may be confusing for people who don't need it
  3. frontmatter property is simpler and more explicit.

If the property is set, we should not even bother checking the pub url and only use the property's format.

In the future I think we'd want to use full lexicon name, but the short leaflet and pckt are good for now

However, lets rename the property to be docContentFormat


if (!title && plugin.settings.publish.useFirstHeaderAsTitle) {
title = extractFirstH1(content);
Expand Down Expand Up @@ -144,10 +158,20 @@ async function buildDocumentRecord(plugin: AtmospherePlugin, file: TFile): Promi
let textContent = stripMarkdown(resolved);

let richContent: PubLeafletContent.Main | BlogPcktContent.Main | null = null;
const publicationFormat = pubUri ? normalizeFormat(plugin.settings.publicationFormats?.[pubUri]) : undefined;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here we are choosing format based on pub URI, but below we are checking pub URL. I know you were testing with non leaflet/pckt publication so it likely works well in that case.

But If you try to use something like this in the settings (a leaflet pub mapped to pckt format)

  "publicationFormats": {
	  "at://did:plc:sppiplftd2sxt3hbw7htj3b5/site.standard.publication/3mdmrc7ka4c2w": "pckt"
  }

The mapping is overridden since the pub URL still contains leaflet. I'm aware that the pub url based selection is not great, and I intend to improve that. And I want to unblock you in the meantime.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't worry too much about blocking me! I didn't think about URI vs URL here

let contentFormat: ContentFormat = "plaintext";
if (pub?.url.contains("leaflet.pub")) {
richContent = await markdownToLeafletContent(resolved)
contentFormat = "leaflet";
} else if (pub?.url.contains("pckt.blog")) {
richContent = markdownToPcktContent(resolved)
contentFormat = "pckt";
} else {
contentFormat = format ?? publicationFormat ?? "plaintext";
}

if (contentFormat === "leaflet") {
richContent = await markdownToLeafletContent(resolved);
} else if (contentFormat === "pckt") {
richContent = markdownToPcktContent(resolved);
}

let record = {
Expand Down
5 changes: 5 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ export interface AtProtoSettings {
publish: {
useFirstHeaderAsTitle: boolean;
};
publicationFormats: Record<string, ContentFormat>;
}

export type ContentFormat = "leaflet" | "pckt" | "plaintext";

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets remove plaintext as an option, because the plaintext is always set for textContent regardless of the content field's format. If no content format is matched, then it can be left unset


export const DEFAULT_SETTINGS: AtProtoSettings = {
clipDir: "AtmosphereClips",
bookmarks: {
Expand All @@ -24,6 +27,7 @@ export const DEFAULT_SETTINGS: AtProtoSettings = {
publish: {
useFirstHeaderAsTitle: false,
},
publicationFormats: {},
};

export class SettingTab extends PluginSettingTab {
Expand Down Expand Up @@ -164,5 +168,6 @@ export class SettingTab extends PluginSettingTab {
await this.plugin.saveSettings();
})
);

}
}
Loading