-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
78 lines (72 loc) · 2.73 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// HEAD
//
const makeHreflang = (locale, url) => {
const hreflang = `<link data-hid="alternate-hreflang-${locale}" rel="alternate" href="${url}" hreflang="${locale}">`
return {
source: 'i18nHref',
priority: 100,
string: hreflang
}
}
//
// Dictionnaries
//
const fillDictionary = async (dictionary, allRequests, getExtraData) => {
const getData = getExtraData
? async (request) => {
const extra = await getExtraData(request)
return Object.assign({}, request, extra)
}
: async (request) => request
return allRequests.forEach(async (request) => {
if (request.locale === undefined) return
if (dictionary[request.locale] === undefined) {
console.error(`Error: i18n: ${request.locale} locale is not defined in i18n config`)
return
}
const data = await getData(request)
if (dictionary[request.locale][request.route] === undefined) {
dictionary[request.locale][request.route] = {}
}
dictionary[request.locale][request.route][request.slug] = data
})
}
//
// Set permalinks to request, based on elderjs code
// Basically a copy of src/Elder.ts way, but with a refacto to match my taste
// Need createReadOnlyProxy implementation
const createReadOnlyProxy = (obj, objName, location) => {
try {
if (typeof obj !== 'object' && !Array.isArray(obj)) return obj
return new Proxy(obj, {
set () {
console.log(
`Object ${objName} is not mutable from ${location}. Check the error below for the hook/plugin that is attempting to mutate properties outside of the rules in hookInterface.ts or in other restricted areas.`
)
return false
}
})
} catch (e) {
return obj
}
}
const getPermalink = async (request, { routes, settings, helpers }, lastSlash = true) => {
if (!routes[request.route] || !routes[request.route].permalink) {
const msg = request.route
? 'Request missing permalink but has request.route defined. This shouldn\'t be an Elder.js issue but if you believe it could be please create an issue.'
: 'Request is missing a \'route\' key. This usually happens when request objects have been added to the allRequests array via a hook or plugin.'
console.error(`${msg} ${JSON.stringify(request)}`)
}
const permalink = await routes[request.route].permalink({
request,
settings: createReadOnlyProxy(settings, 'settings', `${request.route} permalink function`),
helpers: createReadOnlyProxy(helpers, 'helpers', `${request.route} permalink function`)
})
return {
permalink: (settings && settings.server && settings.server.prefix)
? settings.server.prefix + permalink
: lastSlash ? permalink : permalink.slice(0, -1)
}
}
module.exports = { fillDictionary, makeHreflang, getPermalink }