-
Notifications
You must be signed in to change notification settings - Fork 0
/
gridsome.server.js
130 lines (115 loc) · 3.75 KB
/
gridsome.server.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Server API makes it possible to hook into various parts of Gridsome
// on server-side and add custom data to the GraphQL data layer.
// Learn more: https://gridsome.org/docs/server-api
// Changes here requires a server restart.
// To restart press CTRL + C in terminal and run `gridsome develop`
const path = require('path')
const moment = require('moment')
const envLocale = process.env.LOCALE
const dateFormat = {
'ru-ru': 'D MMMM YYYY',
'en-en': 'MMMM D YYYY',
}
/**
* Gets first line and strips tags.
* @param {String} text
* @returns
*/
function getSafeDescription(text) {
return text.split('\n', 1)[0].replace(/<\/?[^>]+(>|$)/g, '')
}
/**
* Exports.
*/
module.exports = function (api) {
// Set locale custom client context.
api._app.pages.hooks.pageContext.tap('siteLocale', (context) => {
context.locale = envLocale
return context
})
// Load catalog series photos.
const collectionsNetlify = {
ProductEndMill: 'photos',
ProductDrill: 'photos',
ProductThreadMill: 'photos',
News: 'image',
Tech: 'image',
}
const titleMap =
envLocale === 'ru-ru'
? {
ProductEndMill: 'Концевая фреза',
ProductDrill: 'Сверло',
ProductThreadMill: 'Резьбовая фреза',
}
: {
ProductEndMill: 'End mill',
ProductDrill: 'Drill',
ProductThreadMill: 'Thread mill',
}
const seriesCollectionMap = {
ProductEndMill: 'ProductItemEndMill',
ProductDrill: 'ProductItemDrill',
ProductThreadMill: 'ProductItemThreadMill',
}
// Collections with 'i18n' field. Contains multilingual content.
const i18nCollections = {
...seriesCollectionMap,
ProductType: true,
}
/**
* Implements hook on each node creation.
*/
api.onCreateNode((options) => {
// We should fix image paths broken by netlifyCMS
// If image path match 'fileName.jpg' without leading './'
// then change it to path relative to content markdown file.
const fieldName = collectionsNetlify[options.internal.typeName]
if (
fieldName &&
options[fieldName] &&
typeof options[fieldName] === 'string'
) {
const imagePath = options[fieldName]
// If image path match 'fileName.jpg' without leading './'
if (/^\w/.test(imagePath) && !/^[a-z][a-z0-9+.-]*:/i.test(imagePath)) {
const out = path.join(path.dirname(options.internal.origin), imagePath)
console.warn(
`Broken image on ${options.internal.typeName}/{${fieldName}}:: `,
out
)
options[fieldName] = out
}
}
// Insert localized date value manually.
// @todo Remove it when fucking gridsome fix context query variables.
if (options.internal.typeName === 'News') {
options = {
...options,
description: getSafeDescription(options.content),
localeDate: moment(options.date)
.locale(envLocale)
.format(dateFormat[envLocale]),
}
}
// If node type is multilingual then process localized data
if (i18nCollections[options.internal.typeName] && options.i18n) {
for (const [key, value] of Object.entries(options.i18n[envLocale])) {
options[key] = value
}
delete options.i18n
}
// If node type is series than:
if (seriesCollectionMap[options.internal.typeName]) {
// Compose title value.
const productItems = api._store
.getCollection(seriesCollectionMap[options.internal.typeName])
.findNodes({ series: options.id })
options.title =
titleMap[options.internal.typeName] + ' ' + options.id.toUpperCase()
options.description = options.content
options.keywords = productItems.map((i) => i.series + i.name).join(', ')
}
return options
})
}