1
0
silverbullet/plugs/share/publish.ts

43 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-11-24 11:04:00 +00:00
import { events } from "$sb/plugos-syscall/mod.ts";
import { editor, markdown } from "$sb/silverbullet-syscall/mod.ts";
2022-11-24 11:04:00 +00:00
import { extractFrontmatter } from "$sb/lib/frontmatter.ts";
2022-11-24 15:08:51 +00:00
import { PublishEvent } from "$sb/app_event.ts";
2022-11-24 11:04:00 +00:00
export async function publishCommand() {
await editor.save();
const text = await editor.getText();
const pageName = await editor.getCurrentPage();
const tree = await markdown.parseMarkdown(text);
let { $share } = await extractFrontmatter(tree);
2022-11-24 11:04:00 +00:00
if (!$share) {
// Nothing to do here
2022-11-24 11:04:00 +00:00
return;
}
if (!Array.isArray($share)) {
$share = [$share];
2022-11-24 11:04:00 +00:00
}
2022-11-24 15:08:51 +00:00
await editor.flashNotification("Sharing...");
2022-11-24 11:04:00 +00:00
try {
await publish(pageName, $share);
2022-11-24 11:04:00 +00:00
await editor.flashNotification("Done!");
} catch (e: any) {
await editor.flashNotification(e.message, "error");
}
}
async function publish(pageName: string, uris: string[]) {
2022-11-24 11:04:00 +00:00
for (const uri of uris) {
const publisher = uri.split(":")[0];
const results = await events.dispatchEvent(
`share:${publisher}`,
{
uri: uri,
name: pageName,
} as PublishEvent,
);
if (results.length === 0) {
throw new Error(`Unsupported publisher: ${publisher} for URI: ${uri}`);
}
}
}