60 lines
1.7 KiB
TypeScript
60 lines
1.7 KiB
TypeScript
import {
|
|
editor,
|
|
events,
|
|
markdown,
|
|
} from "@silverbulletmd/silverbullet/syscalls";
|
|
import { extractFrontmatter } from "@silverbulletmd/silverbullet/lib/frontmatter";
|
|
import type { PublishEvent } from "../../plug-api/types.ts";
|
|
|
|
export async function publishShareOptions() {
|
|
const text = await editor.getText();
|
|
const tree = await markdown.parseMarkdown(text);
|
|
let { $share } = await extractFrontmatter(tree);
|
|
if (!$share) {
|
|
return [];
|
|
}
|
|
if (!Array.isArray($share)) {
|
|
$share = [$share];
|
|
}
|
|
return [{
|
|
id: "publish",
|
|
name: `Publish to ${$share.map((s: string) => s.split(":")[0]).join(", ")}`,
|
|
}];
|
|
}
|
|
|
|
export async function publishShare() {
|
|
const pageName = await editor.getCurrentPage();
|
|
const text = await editor.getText();
|
|
const tree = await markdown.parseMarkdown(text);
|
|
let { $share } = await extractFrontmatter(tree);
|
|
if (!Array.isArray($share)) {
|
|
$share = [$share];
|
|
}
|
|
await editor.flashNotification("Sharing...");
|
|
try {
|
|
await publish(pageName, $share);
|
|
await editor.flashNotification("Done!");
|
|
} catch (e: any) {
|
|
await editor.flashNotification(e.message, "error");
|
|
}
|
|
}
|
|
|
|
async function publish(pageName: string, uris: string[]) {
|
|
const broadcastResults = await events.dispatchEvent(`share:_`, {
|
|
name: pageName,
|
|
} as PublishEvent);
|
|
for (const uri of uris) {
|
|
const publisher = uri.split(":")[0];
|
|
const results = await events.dispatchEvent(
|
|
`share:${publisher}`,
|
|
{
|
|
uri: uri,
|
|
name: pageName,
|
|
} as PublishEvent,
|
|
);
|
|
if (broadcastResults.length === 0 && results.length === 0) {
|
|
throw new Error(`Unsupported publisher: ${publisher} for URI: ${uri}`);
|
|
}
|
|
}
|
|
}
|