Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
Setting,
TAbstractFile,
TFile,
CachedMetadata,
} from 'obsidian';

import { ImageBatchRenameModal } from './batch';
Expand Down Expand Up @@ -137,7 +138,7 @@ export default class PasteImageRenamePlugin extends Plugin {
return
}

const { stem, newName, isMeaningful }= this.generateNewName(file, activeFile)
const { stem, newName, isMeaningful }= this.generateNewName(file, activeFile, null)
debugLog('generated newName:', newName, isMeaningful)

if (!isMeaningful || !autoRename) {
Expand All @@ -153,9 +154,6 @@ export default class PasteImageRenamePlugin extends Plugin {
debugLog('deduplicated newName:', newName)
const originName = file.name

// generate linkText using Obsidian API, linkText is either ![](filename.png) or ![[filename.png]] according to the "Use [[Wikilinks]]" setting.
const linkText = this.app.fileManager.generateMarkdownLink(file, sourcePath)

// file system operation: rename the file
const newPath = path.join(file.parent.path, newName)
try {
Expand All @@ -169,6 +167,9 @@ export default class PasteImageRenamePlugin extends Plugin {
return
}

// generate linkText using Obsidian API, linkText is either ![](filename.png) or ![[filename.png]] according to the "Use [[Wikilinks]]" setting.
const linkText = this.app.fileManager.generateMarkdownLink(file, sourcePath)

// in case fileManager.renameFile may not update the internal link in the active file,
// we manually replace the current line by manipulating the editor

Expand Down Expand Up @@ -250,7 +251,7 @@ export default class PasteImageRenamePlugin extends Plugin {
if (!m0) return

// rename
const { newName, isMeaningful }= this.generateNewName(file, activeFile)
const { newName, isMeaningful }= this.generateNewName(file, activeFile, fileCache)
debugLog('generated newName:', newName, isMeaningful)
if (!isMeaningful) {
new Notice('Failed to batch rename images: the generated name is not meaningful')
Expand All @@ -262,11 +263,12 @@ export default class PasteImageRenamePlugin extends Plugin {
}

// returns a new name for the input file, with extension
generateNewName(file: TFile, activeFile: TFile) {
generateNewName(file: TFile, activeFile: TFile, fileCache: CachedMetadata) {
let imageNameKey = ''
let firstHeading = ''
let frontmatter
const fileCache = this.app.metadataCache.getFileCache(activeFile)
if (!fileCache)
fileCache = this.app.metadataCache.getFileCache(activeFile)
if (fileCache) {
debugLog('frontmatter', fileCache.frontmatter)
frontmatter = fileCache.frontmatter
Expand Down