2022-10-14 13:11:33 +00:00
|
|
|
import { renderToText, replaceNodesMatching } from "$sb/lib/tree.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
import type { FileMeta } from "../../common/types.ts";
|
2022-10-14 13:11:33 +00:00
|
|
|
import { parseMarkdown } from "$sb/silverbullet-syscall/markdown.ts";
|
2022-10-21 14:56:46 +00:00
|
|
|
import { base64EncodedDataUrl } from "../../plugos/asset_bundle/base64.ts";
|
2022-09-02 13:41:40 +00:00
|
|
|
|
2023-01-17 08:34:49 +00:00
|
|
|
export const cloudPrefix = "💭 ";
|
2022-09-02 13:41:40 +00:00
|
|
|
|
2022-09-12 12:50:37 +00:00
|
|
|
export async function readFileCloud(
|
|
|
|
name: string,
|
2023-05-23 18:53:53 +00:00
|
|
|
): Promise<{ data: string; meta: FileMeta } | undefined> {
|
2022-10-15 17:02:56 +00:00
|
|
|
const originalUrl = name.substring(
|
2023-01-17 08:34:49 +00:00
|
|
|
cloudPrefix.length,
|
2022-10-10 12:50:21 +00:00
|
|
|
name.length - ".md".length,
|
2022-09-12 12:50:37 +00:00
|
|
|
);
|
2022-09-02 13:41:40 +00:00
|
|
|
let url = originalUrl;
|
|
|
|
if (!url.includes("/")) {
|
|
|
|
url += "/index";
|
|
|
|
}
|
|
|
|
if (!url.startsWith("127.0.0.1")) {
|
|
|
|
url = `https://${url}`;
|
|
|
|
} else {
|
|
|
|
url = `http://${url}`;
|
|
|
|
}
|
|
|
|
let text = "";
|
2023-01-08 11:24:12 +00:00
|
|
|
|
2022-09-02 13:41:40 +00:00
|
|
|
try {
|
2023-01-17 08:34:49 +00:00
|
|
|
const r = await fetch(`${encodeURI(url)}.md`);
|
2022-09-02 13:41:40 +00:00
|
|
|
text = await r.text();
|
2023-01-08 11:24:12 +00:00
|
|
|
if (!r.ok) {
|
2022-09-02 13:41:40 +00:00
|
|
|
text = `ERROR: ${text}`;
|
|
|
|
}
|
|
|
|
} catch (e: any) {
|
2023-01-08 11:24:12 +00:00
|
|
|
console.error("ERROR thrown", e.message);
|
|
|
|
text = `ERROR: ${e.message}`;
|
2022-09-02 13:41:40 +00:00
|
|
|
}
|
2022-10-21 14:56:46 +00:00
|
|
|
text = await translateLinksWithPrefix(
|
|
|
|
text,
|
2023-01-17 08:34:49 +00:00
|
|
|
`${cloudPrefix}${originalUrl.split("/")[0]}/`,
|
2022-10-21 14:56:46 +00:00
|
|
|
);
|
2022-09-02 13:41:40 +00:00
|
|
|
return {
|
2023-05-23 18:53:53 +00:00
|
|
|
data: base64EncodedDataUrl(
|
2022-10-21 14:56:46 +00:00
|
|
|
"text/markdown",
|
|
|
|
new TextEncoder().encode(text),
|
2022-09-02 13:41:40 +00:00
|
|
|
),
|
|
|
|
meta: {
|
|
|
|
name,
|
2022-09-12 12:50:37 +00:00
|
|
|
contentType: "text/markdown",
|
2022-09-02 13:41:40 +00:00
|
|
|
lastModified: 0,
|
2022-09-12 12:50:37 +00:00
|
|
|
size: text.length,
|
2022-09-02 13:41:40 +00:00
|
|
|
perm: "ro",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
export function writeFileCloud(
|
|
|
|
name: string,
|
|
|
|
): Promise<FileMeta> {
|
|
|
|
console.log("Writing cloud file", name);
|
|
|
|
return getFileMetaCloud(name);
|
|
|
|
}
|
|
|
|
|
2022-09-02 13:41:40 +00:00
|
|
|
async function translateLinksWithPrefix(
|
|
|
|
text: string,
|
2022-10-10 12:50:21 +00:00
|
|
|
prefix: string,
|
2022-09-02 13:41:40 +00:00
|
|
|
): Promise<string> {
|
2022-10-14 13:11:33 +00:00
|
|
|
const tree = await parseMarkdown(text);
|
2022-09-02 13:41:40 +00:00
|
|
|
replaceNodesMatching(tree, (tree) => {
|
|
|
|
if (tree.type === "WikiLinkPage") {
|
|
|
|
// Add the prefix in the link text
|
2023-01-17 08:34:49 +00:00
|
|
|
if (!tree.children![0].text!.startsWith(cloudPrefix)) {
|
2023-01-16 15:59:45 +00:00
|
|
|
// Only for links that aren't already cloud links
|
|
|
|
tree.children![0].text = prefix + tree.children![0].text;
|
|
|
|
}
|
2022-09-02 13:41:40 +00:00
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
});
|
|
|
|
text = renderToText(tree);
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2022-10-14 13:11:33 +00:00
|
|
|
export function getFileMetaCloud(name: string): Promise<FileMeta> {
|
|
|
|
return Promise.resolve({
|
2022-09-02 13:41:40 +00:00
|
|
|
name,
|
2022-09-12 12:50:37 +00:00
|
|
|
size: 0,
|
|
|
|
contentType: "text/markdown",
|
2022-09-02 13:41:40 +00:00
|
|
|
lastModified: 0,
|
|
|
|
perm: "ro",
|
2022-10-14 13:11:33 +00:00
|
|
|
});
|
2022-09-02 13:41:40 +00:00
|
|
|
}
|