feat(app): add middle truncation for filename in comment card

This commit is contained in:
David Hill
2026-01-23 21:49:24 +00:00
parent 18ea09868a
commit 75cccc305a
2 changed files with 16 additions and 8 deletions

View File

@@ -17,3 +17,16 @@ export function getFileExtension(path: string | undefined) {
const parts = path.split(".")
return parts[parts.length - 1]
}
export function getFilenameTruncated(path: string | undefined, maxLength: number = 20) {
const filename = getFilename(path)
if (filename.length <= maxLength) return filename
const lastDot = filename.lastIndexOf(".")
const name = lastDot <= 0 ? filename : filename.slice(0, lastDot)
const ext = lastDot <= 0 ? "" : filename.slice(lastDot)
const available = maxLength - ext.length - 1 // -1 for ellipsis
if (available <= 0) return filename.slice(0, maxLength - 1) + "…"
const start = Math.ceil(available / 2)
const end = Math.floor(available / 2)
return name.slice(0, start) + "…" + name.slice(-end) + ext
}