S&R improved filename sanitizing (#2784)

Co-authored-by: typpos <28550406+typpos@users.noreply.github.com>
This commit is contained in:
Chenlei Hu
2025-03-01 10:47:42 -05:00
committed by GitHub
parent ba4bb5774e
commit 2b212f9701
6 changed files with 165 additions and 71 deletions

View File

@@ -272,3 +272,28 @@ export function parseFilePath(filepath: string): {
subfolder: normalizedPath.slice(0, lastSlashIndex)
}
}
// Simple date formatter
const parts = {
d: (d: Date) => d.getDate(),
M: (d: Date) => d.getMonth() + 1,
h: (d: Date) => d.getHours(),
m: (d: Date) => d.getMinutes(),
s: (d: Date) => d.getSeconds()
}
const format =
Object.keys(parts)
.map((k) => k + k + '?')
.join('|') + '|yyy?y?'
export function formatDate(text: string, date: Date) {
return text.replace(new RegExp(format, 'g'), (text: string): string => {
if (text === 'yy') return (date.getFullYear() + '').substring(2)
if (text === 'yyyy') return date.getFullYear().toString()
if (text[0] in parts) {
const p = parts[text[0] as keyof typeof parts](date)
return (p + '').padStart(text.length, '0')
}
return text
})
}