This should do the trick
silverbullet.registerCommand(
{ name: "Add Web Bookmark" },
async () => {
// Prompt the user for the URL
const url = await editor.prompt("Enter the URL:");
if (!url) {
await editor.flashNotification("No URL provided.", "error");
return;
}
// Retrieve the current page name
const currentPageName = await editor.getCurrentPage();
// Prompt the user for the page name, with the current page name as the default value
const pageName = await editor.prompt(
"Enter the page name to insert the URL into:",
"" // Suggest the current page name as the default value
) || currentPageName; // Fallback to the current page if input is empty
try {
// Read the page content, or initialize it if the page does not exist
let pageContent;
try {
pageContent = await space.readPage(pageName);
} catch (e) {
// Assume the page does not exist, initialize with empty content
pageContent = "";
}
// Append the URL as plain text
const updatedContent = `${pageContent}\n${url}`;
// Write the updated content back to the page
await space.writePage(pageName, updatedContent);
await editor.flashNotification(`URL successfully inserted into ${pageName}.`);
} catch (error) {
console.error("Error while inserting URL:", error);
await editor.flashNotification("An error occurred while inserting the URL.", "error");
}
}
);