-
Notifications
You must be signed in to change notification settings - Fork 1
/
hooks.js
106 lines (102 loc) · 3.29 KB
/
hooks.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const { fillDictionary, makeHreflang, getPermalink } = require('./utils')
const { i18nHelpers } = require('./helpers')
const defaultHooks = [
{
hook: 'bootstrap',
name: 'i18nLocalesOrigin',
description: 'Set i18n locales origin and hrefOrigin',
priority: 100,
run: async ({ settings, plugin }) => {
plugin.config.locales.all.forEach((locale) => {
const validLocale = Object.assign({}, locale, {
prefix: '/' + (locale.prefix !== undefined ? locale.prefix : locale.iso),
hrefOrigin: locale.origin || settings.origin,
origin: locale.origin || ''
})
plugin.dictionaries.locales[locale.code] = validLocale
})
}
},
{
hook: 'bootstrap',
name: 'i18nHelpers',
description: 'Set i18n helpers for route file, except generatePermalink who need i18nLocalesOrigin',
priority: 99,
run: async ({ helpers, settings, routes, plugin }) => {
Object.assign(helpers, { i18n: i18nHelpers(helpers, settings, routes, plugin) })
}
},
{
hook: 'allRequests',
name: 'i18nRequestDictionary',
description: 'Fill i18n requests dictionary',
priority: 99,
run: async ({ plugin, allRequests, routes, settings, helpers }) => {
await fillDictionary(
plugin.dictionaries.requests,
allRequests,
(request) => getPermalink(request, { routes, settings, helpers }, plugin.config.permalink.lastSlash)
)
}
}
]
const optionalHooks = {
hreflang: {
hook: 'stacks',
name: 'i18nHreflang',
description: 'Set i18n hreflangs',
priority: 100,
run: async ({ headStack, plugin, request }) => {
const hreflangs = plugin.locales.map((locale) => {
const origin = plugin.dictionaries.locales[locale].hrefOrigin
const route = plugin.dictionaries.requests[locale][request.route]
if (route === undefined) return {}
const permalink = route[request.slug].permalink
return makeHreflang(locale, origin + permalink)
})
headStack.push(...hreflangs)
}
},
lang: {
hook: 'stacks',
name: 'i18nHtmlLang',
description: 'Set i18n lang attribute to html tag',
priority: 100,
run: async ({ htmlAttributesStack, request, plugin }) => {
const locale = plugin.config.locales.all.find((locale) => locale.code === request.locale)
if (locale === undefined) return {}
return {
htmlAttributesStack: [
...htmlAttributesStack.filter((attr) => attr.source !== 'elderAddHtmlLangAttributes'),
{
source: 'i18nHtmlLang',
priority: 100,
string: `lang="${locale.iso}"`
}
]
}
}
},
enableExcludeLocales: {
hook: 'allRequests',
name: 'i18nExcludeLocales',
description: 'Remove requests according to excludeLocales config',
priority: 90,
run: async ({ allRequests, plugin }) => {
return {
allRequests: allRequests.filter((request) => !plugin.config.locales.excludes.includes(request.locale))
}
}
}
}
const getOptionalHooks = (config) => {
const keys = Object.keys(optionalHooks)
const hooks = []
keys.forEach((key) => {
if (config.seo[key] === true || config[key] === true) {
hooks.push(optionalHooks[key])
}
})
return hooks
}
module.exports = { defaultHooks, getOptionalHooks }