Implementation of #117: sharing

This commit is contained in:
Zef Hemel
2022-11-24 16:08:51 +01:00
parent 5b1ec14891
commit b61e8ff681
12 changed files with 78 additions and 25 deletions
+6
View File
@@ -15,6 +15,12 @@ functions:
path: "./collab.ts:shareCommand"
command:
name: "Share: Collab"
shareNoop:
path: "./collab.ts:shareNoop"
events:
- share:collab
# Space extension
readPageCollab:
path: ./collab.ts:readFileCollab
env: client
+4 -1
View File
@@ -15,7 +15,6 @@ import {
collab,
editor,
markdown,
space,
} from "$sb/silverbullet-syscall/mod.ts";
import { nanoid } from "https://esm.sh/nanoid@4.0.0";
@@ -122,6 +121,10 @@ export async function detectPage() {
}
}
export function shareNoop() {
return true;
}
export async function readFileCollab(
name: string,
encoding: FileEncoding,
+3 -2
View File
@@ -386,17 +386,18 @@ functions:
path: ./stats.ts:statsCommand
command:
name: "Stats: Show"
key: "Ctrl-s"
mac: "Cmd-s"
key: "Shift-Alt-s"
# Cloud pages
readPageCloud:
path: ./cloud.ts:readFileCloud
env: server
pageNamespace:
pattern: "💭 .+"
operation: readFile
getPageMetaCloud:
path: ./cloud.ts:getFileMetaCloud
env: server
pageNamespace:
pattern: "💭 .+"
operation: getFileMeta
+26 -3
View File
@@ -4,7 +4,9 @@ import { replaceAsync } from "$sb/lib/util.ts";
import { directiveRegex, renderDirectives } from "./directives.ts";
import { extractFrontmatter } from "$sb/lib/frontmatter.ts";
export async function updateDirectivesOnPageCommand() {
export async function updateDirectivesOnPageCommand(arg: any) {
// If `arg` is a string, it's triggered automatically via an event, not explicitly via a command
const explicitCall = typeof arg !== "string";
const pageName = await editor.getCurrentPage();
const text = await editor.getText();
const tree = await markdown.parseMarkdown(text);
@@ -14,6 +16,22 @@ export async function updateDirectivesOnPageCommand() {
return;
}
// If this page is shared ($share) via collab: disable directives as well
// due to security concerns
if (metaData.$share) {
for (const uri of metaData.$share) {
if (uri.startsWith("collab:")) {
if (explicitCall) {
await editor.flashNotification(
"Directives are disabled for 'collab' pages (safety reasons).",
"error",
);
}
return;
}
}
}
// Collect all directives and their body replacements
const replacements: { fullMatch: string; text?: string }[] = [];
@@ -62,10 +80,15 @@ export async function updateDirectivesOnPageCommand() {
);
continue;
}
const from = index, to = index + replacement.fullMatch.length;
if (text.substring(from, to) === replacement.text) {
// No change, skip
continue;
}
await editor.dispatch({
changes: {
from: index,
to: index + replacement.fullMatch.length,
from,
to,
insert: replacement.text,
},
});
+1 -1
View File
@@ -15,7 +15,7 @@ export const directiveRegex =
/**
* Looks for directives in the text dispatches them based on name
*/
export async function directiveDispatcher(
export function directiveDispatcher(
pageName: string,
text: string,
tree: ParseTree,
+1 -1
View File
@@ -1,8 +1,8 @@
import { markdown, space } from "$sb/silverbullet-syscall/mod.ts";
import { fs } from "$sb/plugos-syscall/mod.ts";
import { asset } from "$sb/plugos-syscall/mod.ts";
import type { PublishEvent } from "../share/publish.ts";
import { renderMarkdownToHtml } from "./markdown_render.ts";
import { PublishEvent } from "$sb/app_event.ts";
export async function sharePublisher(event: PublishEvent) {
const path = event.uri.split(":")[1];
+9 -9
View File
@@ -1,26 +1,26 @@
import { events } from "$sb/plugos-syscall/mod.ts";
import { editor, markdown, system } from "$sb/silverbullet-syscall/mod.ts";
import { extractFrontmatter } from "$sb/lib/frontmatter.ts";
export type PublishEvent = {
uri: string;
// Page name
name: string;
};
import { PublishEvent } from "$sb/app_event.ts";
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 } = extractFrontmatter(tree);
const { $share } = extractFrontmatter(tree);
if (!$share) {
await editor.flashNotification("No $share directive found", "error");
await editor.flashNotification("Saved.");
return;
}
if (!Array.isArray($share)) {
$share = [$share];
await editor.flashNotification(
"$share front matter must be an array.",
"error",
);
return;
}
await editor.flashNotification("Sharing...");
// Delegate actual publishing to the server
try {
await system.invokeFunction("server", "publish", pageName, $share);
+2
View File
@@ -4,6 +4,8 @@ functions:
path: publish.ts:publishCommand
command:
name: "Share: Publish"
key: "Ctrl-s"
mac: "Cmd-s"
publish:
path: publish.ts:publish
env: server