Skip to content

Commit

Permalink
handle markdown links
Browse files Browse the repository at this point in the history
  • Loading branch information
reorx committed Apr 18, 2022
1 parent 4573bfd commit 7a62a26
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { renderTemplate } from './template';
import {
createElementTree, debugLog, path, sanitizer, lockInputMethodComposition,
getVaultConfig,
} from './utils';

interface PluginSettings {
Expand Down Expand Up @@ -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: [
Expand All @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { App, Vault } from 'obsidian';

export const DEBUG = !(process.env.BUILD_ENV === 'production')

export function debugLog(...args: any[]) {
Expand Down Expand Up @@ -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
}

0 comments on commit 7a62a26

Please sign in to comment.