mirror of
https://github.com/nexus-stc/hyperboria
synced 2024-12-21 17:17:47 +01:00
d51e5ab65d
- fix: Translation fixes - fix: Various fixes - feat: PB translations, configuration changes - fix: Bugfixes GitOrigin-RevId: 55f8b148c42a296162fc707c36a5146ca0073b4b
69 lines
1.5 KiB
JavaScript
69 lines
1.5 KiB
JavaScript
import { castStringToSingleString, escapeFormat, quoteUrl } from './utils'
|
|
import { getFirstAuthors, getIssuedDate } from './helpers'
|
|
|
|
function getRobustTitle (title, volume) {
|
|
if (volume) {
|
|
if (title) {
|
|
title = `${title} ${volume}`
|
|
} else {
|
|
title = volume
|
|
}
|
|
}
|
|
return escapeFormat(title)
|
|
}
|
|
|
|
export function getFilename (authors, title, doi, issuedDate, md5, extension) {
|
|
const limit = 55
|
|
|
|
const processedAuthor = castStringToSingleString((getFirstAuthors(authors, false, 1)).toLowerCase())
|
|
const processedTitle = castStringToSingleString(getRobustTitle(title)).toLowerCase()
|
|
|
|
const parts = []
|
|
if (processedAuthor) {
|
|
parts.push(processedAuthor)
|
|
}
|
|
if (processedTitle) {
|
|
parts.push(processedTitle)
|
|
}
|
|
|
|
let filename = parts.join('-')
|
|
const chars = []
|
|
let size = 0
|
|
let hitLimit = false
|
|
|
|
for (const c of filename) {
|
|
const currentSize = size + c.length
|
|
if (currentSize > limit) {
|
|
hitLimit = true
|
|
break
|
|
}
|
|
chars.push(c)
|
|
size = currentSize
|
|
}
|
|
|
|
filename = chars.join('')
|
|
if (hitLimit) {
|
|
const glyph = filename.lastIndexOf('-')
|
|
if (glyph !== -1) {
|
|
filename = filename.substr(0, glyph)
|
|
}
|
|
}
|
|
|
|
if (!filename) {
|
|
if (doi) {
|
|
filename = quoteUrl(doi, '')
|
|
} else {
|
|
filename = md5
|
|
}
|
|
}
|
|
|
|
const year = getIssuedDate(issuedDate)
|
|
|
|
if (year) {
|
|
filename = `${filename}-${year}`
|
|
}
|
|
filename = filename.replace(/-+/g, '-')
|
|
|
|
return `${filename}.${extension}`
|
|
}
|