From f49493f3daf5512b31c0f549a0569851d59e5fb9 Mon Sep 17 00:00:00 2001 From: F Lengyel Date: Sun, 25 Jun 2023 18:41:42 -0400 Subject: [PATCH] Update ID and rename files. --- package.json | 13 ++++++++----- src/extension.ts | 25 ++++++++++++++----------- src/replaceLinks.ts | 10 +++++----- src/util.ts | 25 ++++++++++++++++--------- 4 files changed, 43 insertions(+), 30 deletions(-) diff --git a/package.json b/package.json index 368abf4..867b002 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,13 @@ "group": "navigation" } ], + "explorer/context": [ + { + "command": "zettelView.renameEntry", + "when": "resourceExtname == .md", + "group": "1_modification" + } + ], "viewItem": [ { "command": "zettelView.renameEntry", @@ -58,11 +65,7 @@ }, { "command": "zettelView.renameEntry", - "title": "Rename and replace links", - "icon": { - "light": "resources/light/rename.svg", - "dark": "resources/dark/rename.svg" - } + "title": "Rename and replace links" } ], "configuration": { diff --git a/src/extension.ts b/src/extension.ts index ea51a6e..8b2c66a 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -5,7 +5,7 @@ import * as fs from 'fs'; import * as path from 'path'; import * as readline from 'readline'; -import { myLogger,id } from './util'; +import { myLogger,id, IDregex } from './util'; import { replaceLinks } from './replaceLinks'; class AsyncZettelViewTreeItem extends vscode.TreeItem { @@ -19,7 +19,7 @@ class AsyncZettelViewTreeItem extends vscode.TreeItem { public readonly command?: vscode.Command, ) { super(basename, collapsibleState); - this.contextValue = "zettelItem"; // this is the key to the context menu + //this.contextValue = "zettelItem"; // this is the key to the context menu this.label = basename; // assume the label is the basename // myLogger.logMsg(`Regex: ${id.regex}`); @@ -42,9 +42,9 @@ class AsyncZettelViewTreeItem extends vscode.TreeItem { for await (const line of rl) { - //const match = line?.match(/^# ((\w{1,4}\.){2,}\d\w{3}) (.+)$/); + //const match = line?.match(/^# ((\w{1,4}\.){2,}\d\w{3})/); // id is non-local - const match = id.re.exec(line); + const match = id.h1re.exec(line); if (match) { // check if basename == match[1].md if (basename !== `${match[1]}.md`) { @@ -117,6 +117,8 @@ class ZettelViewTreeDataProvider implements vscode.TreeDataProvider { // Prompt the user for the new name - const newName = await vscode.window.showInputBox({ prompt: 'Enter the new ID' }); - - if (newName && node) { - // Validate the new name against the regex - if (!id.re.test(newName)) { - vscode.window.showErrorMessage('The new ID is invalid. Please try again.'); + const newID = await vscode.window.showInputBox({ prompt: 'Enter the new ID' }); + if (newID && node) { + // Validate the new ID against the regex + myLogger.logMsg(`New name: ${newID}`); + if (!id.idre.test(newID)) { + vscode.window.showErrorMessage(`The new ID ${newID} does not match ${id.idregex}. Please try again.`); return; } try { // Assume node.fsPath is the file path of the file to be renamed const oldPath = node.fsPath; + //concatenate the new ID with the extension ".md" + const newName = `${newID}.md`; const newPath = path.join(path.dirname(oldPath), newName); // Rename the file await fs.promises.rename(oldPath, newPath); // Now find and replace all the links in the workspace - // Assume that we have a function findAndReplaceAllLinks(oldPath, newPath) await replaceLinks(oldPath, newPath); // Refresh the tree view diff --git a/src/replaceLinks.ts b/src/replaceLinks.ts index d807010..f7639ea 100644 --- a/src/replaceLinks.ts +++ b/src/replaceLinks.ts @@ -7,8 +7,8 @@ const readFile = util.promisify(fs.readFile); const writeFile = util.promisify(fs.writeFile); export async function replaceLinks(oldPath: string, newPath: string): Promise { - const oldFilename = path.basename(oldPath, '.md'); - const newFilename = path.basename(newPath, '.md'); + const oldID = path.basename(oldPath, '.md'); + const newID = path.basename(newPath, '.md'); const dirPath = path.dirname(oldPath); // Get all markdown files in the root directory @@ -24,10 +24,10 @@ export async function replaceLinks(oldPath: string, newPath: string): Promise