2021-03-22 05:51:07 -04:00
|
|
|
import _ from 'lodash'
|
|
|
|
|
|
|
|
// cache for parsed values
|
2021-12-14 08:24:53 -05:00
|
|
|
window.metaAttributesCache = window.metaAttributesCache || new Map()
|
2021-03-22 05:51:07 -04:00
|
|
|
|
|
|
|
export default function getMeta(name, fallback) {
|
2021-12-14 08:24:53 -05:00
|
|
|
if (window.metaAttributesCache.has(name)) {
|
|
|
|
return window.metaAttributesCache.get(name)
|
|
|
|
}
|
2021-03-22 05:51:07 -04:00
|
|
|
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
|
|
|
|
}
|
2021-12-14 08:24:53 -05:00
|
|
|
window.metaAttributesCache.set(name, value)
|
2021-03-22 05:51:07 -04:00
|
|
|
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:
|
2024-02-01 06:37:16 -05:00
|
|
|
// e.g. assign 'foo' before 'foo.bar'
|
2021-03-22 05:51:07 -04:00
|
|
|
.sort()
|
|
|
|
.forEach(nameWithNamespace => {
|
|
|
|
const label = nameWithNamespace.slice('ol-'.length)
|
|
|
|
_.set(window, label, getMeta(nameWithNamespace))
|
|
|
|
_.set(window.data, label, getMeta(nameWithNamespace))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
convertMetaToWindowAttributes()
|