diff --git a/README.md b/README.md index 1347bd2..8008925 100644 --- a/README.md +++ b/README.md @@ -41,21 +41,6 @@ files of the current workspace. Typing `[^` triggers autocompletion for footnotes. -## rename file ⇒ update links to this file - -When you rename a file, all links to this file in other Markdown files would be -broken. Markdown IDE fixes this by changing the target of these links to the new -filename. - -![demo of the "rename file" feature](https://raw.githubusercontent.com/kevgo/vscode-markdown-ide/main/documentation/rename-file.gif) - -## delete file ⇒ remove links to this file - -When you delete a file, all links to this file in other Markdown files would be -broken. Markdown IDE fixes this by removing these links. - -![demo of the "delete file" feature](https://raw.githubusercontent.com/kevgo/vscode-markdown-ide/main/documentation/delete-file.gif) - ## rename Markdown file title ⇒ update links containing this title Run the "Markdown IDE: Rename document title" command to change the primary @@ -64,6 +49,11 @@ document. ![demo of the "rename document title" feature](https://raw.githubusercontent.com/kevgo/vscode-markdown-ide/main/documentation/rename-document-title.gif) +## update links after file was renamed or deleted + +This is now +[available](vscode://settings/markdown.updateLinksOnFileMove.enabled) in VSCode. + ## "go to definition" for links Markdown IDE supports the diff --git a/documentation/delete-file.gif b/documentation/delete-file.gif deleted file mode 100644 index 72b3440..0000000 Binary files a/documentation/delete-file.gif and /dev/null differ diff --git a/documentation/rename-file.gif b/documentation/rename-file.gif deleted file mode 100644 index 7c93c25..0000000 Binary files a/documentation/rename-file.gif and /dev/null differ diff --git a/src/extension.ts b/src/extension.ts index ff1e551..c2b9743 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -4,8 +4,6 @@ import { createCompletionProvider } from "./autocomplete/provider" import { extractNoteBody, extractNoteTitle, linkToNote, TikibaseProvider } from "./code-action/tikibase-provider" import * as configuration from "./configuration" import * as fileSaved from "./file-saved/file-saved" -import { filesDeleted } from "./files-deleted" -import { filesRenamed } from "./files-renamed" import { MarkdownDefinitionProvider } from "./follow-link/follow-bidi-link" import { renameTitle } from "./rename-title/rename-title" import * as tikibase from "./tikibase" @@ -25,12 +23,6 @@ export async function activate(context: vscode.ExtensionContext): Promise // autocomplete headings by typing `#` context.subscriptions.push(vscode.languages.registerCompletionItemProvider("markdown", completionProvider, "#")) - // file renamed --> update links to this file - vscode.workspace.onDidRenameFiles(filesRenamed) - - // file deleted --> remove links to this file - vscode.workspace.onDidDeleteFiles(filesDeleted) - // rename document title --> update links with the old document title context.subscriptions.push(vscode.commands.registerCommand("markdownIDE.renameDocumentTitle", renameTitle)) diff --git a/src/files-deleted.ts b/src/files-deleted.ts deleted file mode 100644 index f12ea1b..0000000 --- a/src/files-deleted.ts +++ /dev/null @@ -1,47 +0,0 @@ -import * as path from "path" -import * as vscode from "vscode" - -import * as configuration from "./configuration" -import * as files from "./helpers/files" -import * as line from "./helpers/line" -import * as links from "./helpers/links" - -export async function filesDeleted(deletedEvent: vscode.FileDeleteEvent): Promise { - // flush all open changes to the filesystem since we are reading files below - await vscode.workspace.saveAll(false) - - const wsRoot = configuration.workspacePath() - if (!wsRoot) { - return - } - const progressOpts: vscode.ProgressOptions = { - location: vscode.ProgressLocation.Window, - title: "removing links", - cancellable: false - } - await vscode.window.withProgress(progressOpts, async () => { - const edit = new vscode.WorkspaceEdit() - const mdFiles: files.FileResult[] = [] - await files.markdown(wsRoot, mdFiles) - for (const mdFile of mdFiles) { - const oldContent = await mdFile.content - let newContent = oldContent - const fullPath = path.join(wsRoot, mdFile.filePath) - for (const deletedFile of deletedEvent.files) { - newContent = links.removeWithTarget({ - text: newContent, - target: path.relative(path.dirname(fullPath), deletedFile.fsPath) - }) - } - if (newContent === oldContent) { - continue - } - const range = new vscode.Range( - new vscode.Position(0, 0), - new vscode.Position(line.count(oldContent), 0) - ) - edit.replace(vscode.Uri.file(fullPath), range, newContent) - } - await vscode.workspace.applyEdit(edit) - }) -} diff --git a/src/files-renamed.ts b/src/files-renamed.ts deleted file mode 100644 index 7d46a0b..0000000 --- a/src/files-renamed.ts +++ /dev/null @@ -1,46 +0,0 @@ -import * as path from "path" -import * as vscode from "vscode" - -import * as configuration from "./configuration" -import * as files from "./helpers/files" -import * as line from "./helpers/line" -import * as links from "./helpers/links" - -export async function filesRenamed(renamedEvent: vscode.FileRenameEvent): Promise { - // flush all open changes to the filesystem since we are reading files below - await vscode.workspace.saveAll(false) - - const wsRoot = configuration.workspacePath() - if (!wsRoot) { - return - } - const progressOpts: vscode.ProgressOptions = { - location: vscode.ProgressLocation.Window, - title: "updating link targets", - cancellable: false - } - await vscode.window.withProgress(progressOpts, async () => { - const edit = new vscode.WorkspaceEdit() - const mdFiles: files.FileResult[] = [] - await files.markdown(wsRoot, mdFiles) - for (const mdFile of mdFiles) { - const oldContent = await mdFile.content - let newContent = oldContent - const fullPath = path.join(wsRoot, mdFile.filePath) - const fullDir = path.dirname(fullPath) - for (const renamedFile of renamedEvent.files) { - newContent = links.replaceTarget({ - text: newContent, - oldTarget: path.relative(fullDir, renamedFile.oldUri.fsPath), - newTarget: path.relative(fullDir, renamedFile.newUri.fsPath) - }) - } - if (newContent === oldContent) { - continue - } - const range = new vscode.Range(0, 0, line.count(oldContent), 0) - edit.replace(vscode.Uri.file(fullPath), range, newContent) - } - await vscode.workspace.applyEdit(edit) - }) -}