From 64c36f9d4c44b0a0e7c90cb00ce86076371a5762 Mon Sep 17 00:00:00 2001 From: Patrik Stenmark Date: Fri, 23 Feb 2024 18:27:03 +0100 Subject: [PATCH] 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. --- plugs/index/refactor.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/plugs/index/refactor.ts b/plugs/index/refactor.ts index 127ce815..e93d0c35 100644 --- a/plugs/index/refactor.ts +++ b/plugs/index/refactor.ts @@ -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);