Templates 2.0 (#636)
Templates 2.0 and a whole bunch of other refactoring
This commit is contained in:
@@ -28,3 +28,9 @@ functions:
|
||||
pageNamespace:
|
||||
pattern: "!.+"
|
||||
operation: getFileMeta
|
||||
|
||||
# Library management
|
||||
importLibraryCommand:
|
||||
path: library.ts:importLibraryCommand
|
||||
command:
|
||||
name: "Library: Import"
|
||||
@@ -117,7 +117,7 @@ export async function cacheFileListing(uri: string): Promise<FileMeta[]> {
|
||||
|
||||
export async function readFile(
|
||||
name: string,
|
||||
): Promise<{ data: Uint8Array; meta: FileMeta } | undefined> {
|
||||
): Promise<{ data: Uint8Array; meta: FileMeta }> {
|
||||
const url = federatedPathToUrl(name);
|
||||
console.log("Fetching federated file", url);
|
||||
const r = await nativeFetch(url, {
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import { editor, space } from "$sb/syscalls.ts";
|
||||
import { cacheFileListing, readFile } from "./federation.ts";
|
||||
|
||||
export async function importLibraryCommand(_def: any, uri?: string) {
|
||||
if (!uri) {
|
||||
uri = await editor.prompt("Import library (federation URL):");
|
||||
}
|
||||
if (!uri) {
|
||||
return;
|
||||
}
|
||||
uri = uri.trim();
|
||||
if (!uri.startsWith("!")) {
|
||||
uri = `!${uri}`;
|
||||
}
|
||||
const allTemplates = (await cacheFileListing(uri)).filter((f) =>
|
||||
f.name.endsWith(".md")
|
||||
);
|
||||
if (
|
||||
!await editor.confirm(
|
||||
`You are about to import ${allTemplates.length} templates, want to do this?`,
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
for (const template of allTemplates) {
|
||||
// Clean up file path
|
||||
let pageName = template.name.replace(/\.md$/, "");
|
||||
// Remove the federation part
|
||||
const pieces = pageName.split("/");
|
||||
pageName = pieces.slice(1).join("/");
|
||||
|
||||
// Fetch the file
|
||||
const buf = (await readFile(template.name)).data;
|
||||
|
||||
try {
|
||||
// Check if it already exists
|
||||
await space.getPageMeta(pageName);
|
||||
|
||||
if (
|
||||
!await editor.confirm(
|
||||
`Page ${pageName} already exists, are you sure you want to override it?`,
|
||||
)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
} catch {
|
||||
// Expected
|
||||
}
|
||||
|
||||
// Write to local space
|
||||
await space.writePage(pageName, new TextDecoder().decode(buf));
|
||||
}
|
||||
await editor.reloadSettingsAndCommands();
|
||||
await editor.flashNotification("Import complete!");
|
||||
}
|
||||
Reference in New Issue
Block a user