mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-30 05:25:28 -05:00
535c97e8cf
[frontend] import meta tag processing from das7pads fork GitOrigin-RevId: ca74ff9fbbcb51091a626a45468ff3d24d6136ca
48 lines
1.3 KiB
JavaScript
48 lines
1.3 KiB
JavaScript
import _ from 'lodash'
|
|
|
|
// cache for parsed values
|
|
const cache = new Map()
|
|
|
|
export default function getMeta(name, fallback) {
|
|
if (cache.has(name)) return cache.get(name)
|
|
const element = document.head.querySelector(`meta[name="${name}"]`)
|
|
if (!element) {
|
|
return fallback
|
|
}
|
|
const plainTextValue = element.content
|
|
let value
|
|
switch (element.dataset.type) {
|
|
case 'boolean':
|
|
// in pug: content=false -> no content field
|
|
// in pug: content=true -> empty content field
|
|
value = element.hasAttribute('content')
|
|
break
|
|
case 'json':
|
|
if (!plainTextValue) {
|
|
// JSON.parse('') throws
|
|
value = undefined
|
|
} else {
|
|
value = JSON.parse(plainTextValue)
|
|
}
|
|
break
|
|
default:
|
|
value = plainTextValue
|
|
}
|
|
cache.set(name, value)
|
|
return value
|
|
}
|
|
|
|
function convertMetaToWindowAttributes() {
|
|
window.data = window.data || {}
|
|
Array.from(document.querySelectorAll('meta[name^="ol-"]'))
|
|
.map(element => element.name)
|
|
// process short labels before long ones:
|
|
// e.g. assign 'sharelatex' before 'sharelatex.templates'
|
|
.sort()
|
|
.forEach(nameWithNamespace => {
|
|
const label = nameWithNamespace.slice('ol-'.length)
|
|
_.set(window, label, getMeta(nameWithNamespace))
|
|
_.set(window.data, label, getMeta(nameWithNamespace))
|
|
})
|
|
}
|
|
convertMetaToWindowAttributes()
|