Pre-fill new page name with heading (#744)

When extracting a page, this will check if the first line is a markdown
heading. If it is, it will be used as a default name in the prompt
asking for a page name.

This uses a regular expression to check for a heading. It seemed a bit
overkill to do a full markdown parsing for this specific check.
pull/719/head
Patrik Stenmark 2024-02-23 18:27:03 +01:00 committed by GitHub
parent cdefe7351a
commit 64c36f9d4c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 13 additions and 4 deletions

View File

@ -230,7 +230,19 @@ export async function renamePrefixCommand(cmdDef: any) {
}
export async function extractToPageCommand() {
const newName = await editor.prompt(`New page title:`, "new page");
const selection = await editor.getSelection();
let text = await editor.getText();
text = text.slice(selection.from, selection.to);
const match = text.match("#{1,6}\\s+([^\n]*)");
let newName;
if (match) {
newName = match[1];
} else {
newName = "new page";
}
newName = await editor.prompt(`New page title:`, newName);
if (!newName) {
return;
}
@ -252,9 +264,6 @@ export async function extractToPageCommand() {
throw e;
}
}
let text = await editor.getText();
const selection = await editor.getSelection();
text = text.slice(selection.from, selection.to);
await editor.replaceRange(selection.from, selection.to, `[[${newName}]]`);
console.log("Writing new page to space");
await space.writePage(newName, text);