Files

98 lines
2.7 KiB
JavaScript
Raw Permalink Normal View History

2025-09-13 12:20:50 -04:00
#!/usr/bin/env node
/**
* Fixes attachment links after files have been renamed
* Usage: node .scripts/fix-renamed-links.js <old-name> <new-name>
2025-09-15 08:49:07 -04:00
*
2025-09-13 12:20:50 -04:00
* This properly handles the case where files are renamed, not just moved
*/
2025-09-15 08:49:07 -04:00
import fs from 'node:fs'
import path from 'node:path'
2025-09-13 12:20:50 -04:00
2025-09-15 08:49:07 -04:00
const args = process.argv.slice(2)
2025-09-13 12:20:50 -04:00
if (args.length !== 2) {
2025-09-15 08:49:07 -04:00
console.log(
'Usage: node .scripts/fix-renamed-links.js <old-filename> <new-filename>',
)
console.log(
'Example: node .scripts/fix-renamed-links.js "CleanShot 2025-01-01.png" "Project Screenshot.png"',
)
process.exit(1)
2025-09-13 12:20:50 -04:00
}
2025-09-15 08:49:07 -04:00
const [oldName, newName] = args
const newPath = `05_Attachments/Organized/${newName}`
2025-09-13 12:20:50 -04:00
2025-09-15 08:49:07 -04:00
console.log(`Fixing links: ${oldName}${newName}`)
2025-09-13 12:20:50 -04:00
// Function to walk directory
function walkDir(dir, callback) {
2025-09-15 08:49:07 -04:00
fs.readdirSync(dir).forEach((f) => {
const dirPath = path.join(dir, f)
const isDirectory = fs.statSync(dirPath).isDirectory()
if (isDirectory) {
// Skip node_modules, .git
if (!f.includes('node_modules') && !f.includes('.git')) {
walkDir(dirPath, callback)
}
} else {
callback(path.join(dir, f))
}
})
2025-09-13 12:20:50 -04:00
}
// Process all markdown files
2025-09-15 08:49:07 -04:00
let updatedCount = 0
const updatedFiles = []
2025-09-13 12:20:50 -04:00
walkDir('.', (filepath) => {
2025-09-15 08:49:07 -04:00
if (filepath.endsWith('.md')) {
let content = fs.readFileSync(filepath, 'utf8')
const originalContent = content
// Escape special regex characters in filename
const escapedOld = oldName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
// Pattern 1: ![[oldname]] without path
const pattern1 = new RegExp(`!\\[\\[${escapedOld}\\]\\]`, 'g')
content = content.replace(pattern1, `![[${newPath}]]`)
// Pattern 2: ![[05_Attachments/oldname]]
2025-09-15 08:49:07 -04:00
const pattern2 = new RegExp(
`!\\[\\[05_Attachments/${escapedOld}\\]\\]`,
2025-09-15 08:49:07 -04:00
'g',
)
content = content.replace(pattern2, `![[${newPath}]]`)
// Pattern 3: [[oldname]] without ! (for PDFs and other non-embedded)
const pattern3 = new RegExp(`(?<!!)\\[\\[${escapedOld}\\]\\]`, 'g')
content = content.replace(pattern3, `[[${newPath}]]`)
// Pattern 4: [[05_Attachments/oldname]] without !
2025-09-15 08:49:07 -04:00
const pattern4 = new RegExp(
`(?<!!)\\[\\[05_Attachments/${escapedOld}\\]\\]`,
2025-09-15 08:49:07 -04:00
'g',
)
content = content.replace(pattern4, `[[${newPath}]]`)
// Write back if changed
if (content !== originalContent) {
fs.writeFileSync(filepath, content, 'utf8')
updatedFiles.push(filepath)
updatedCount++
2025-09-13 12:20:50 -04:00
}
2025-09-15 08:49:07 -04:00
}
})
2025-09-13 12:20:50 -04:00
// Report results
if (updatedCount > 0) {
2025-09-15 08:49:07 -04:00
console.log(`\nUpdated ${updatedCount} files:`)
updatedFiles.forEach((file) => console.log(` - ${file}`))
2025-09-13 12:20:50 -04:00
} else {
2025-09-15 08:49:07 -04:00
console.log('\nNo files needed updating')
2025-09-13 12:20:50 -04:00
}
2025-09-15 08:49:07 -04:00
console.log('\nDone!')