diff --git a/src/main.ts b/src/main.ts index 3b63a4c..a33e2de 100644 --- a/src/main.ts +++ b/src/main.ts @@ -16,6 +16,7 @@ import { import { renderTemplate } from './template'; import { createElementTree, debugLog, path, sanitizer, lockInputMethodComposition, + getVaultConfig, } from './utils'; interface PluginSettings { @@ -117,11 +118,19 @@ export default class PasteImageRenamePlugin extends Plugin { return } + // get vault config, determine whether useMarkdownLinks is set + const vaultConfig = getVaultConfig(this.app) + let useMarkdownLinks = false + if (vaultConfig && vaultConfig.useMarkdownLinks) { + useMarkdownLinks = true + } + const cursor = editor.getCursor() const line = editor.getLine(cursor.line) + debugLog('current line', line) // console.log('editor context', cursor, ) - const linkText = `[[${originName}]]`, - newLinkText = `[[${newName}]]`; + const linkText = this.makeLinkText(originName, useMarkdownLinks), + newLinkText = this.makeLinkText(newName, useMarkdownLinks); debugLog('replace text', linkText, newLinkText) editor.transaction({ changes: [ @@ -136,6 +145,14 @@ export default class PasteImageRenamePlugin extends Plugin { new Notice(`Renamed ${originName} to ${newName}`) } + makeLinkText(fileName: string, useMarkdownLinks: boolean): string { + if (useMarkdownLinks) { + return `[](${encodeURI(fileName)})` + } else { + return `[[${fileName}]]` + } + } + openRenameModal(file: TFile, newName: string) { const modal = new ImageRenameModal( this.app, file as TFile, newName, diff --git a/src/utils.ts b/src/utils.ts index 1b026d2..cec3308 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,3 +1,5 @@ +import { App, Vault } from 'obsidian'; + export const DEBUG = !(process.env.BUILD_ENV === 'production') export function debugLog(...args: any[]) { @@ -97,3 +99,17 @@ export function lockInputMethodComposition(el: HTMLInputElement): CompositionSta }) return state } + + +interface VaultConfig { + useMarkdownLinks?: boolean +} + +interface VaultWithConfig extends Vault { + config?: VaultConfig, +} + +export function getVaultConfig(app: App): VaultConfig|null { + const vault = app.vault as VaultWithConfig + return vault.config +}