This repository has been archived by the owner on Jul 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gridsome.server.js
225 lines (190 loc) · 6.32 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const perfMon = require('./registry/perf-mon');
perfMon.start();
const fs = require('fs');
const unified = require('unified');
const rehypeParse = require('rehype-parse');
const rehypeStringify = require('rehype-stringify');
const link = require('rehype-autolink-headings');
const remarkHtml = require('remark-html');
const remarkParse = require('remark-parse');
const remarkExternalLinks = require('remark-external-links');
const path = require('path');
const externalLinks = require('./registry/utils/plugins/external-links');
const { registry } = require('./registry');
const { popularitiesJson } = require('@webdoky/yari-ports');
const { sourceLocale, targetLocale } = require('./registry/config');
const graphqlSchemaTypes = require('./types');
const excludedUrls = require('./noindex-urls');
// TODO: to config
const pathToLocalizedContent = process.env.PATH_TO_LOCALIZED_CONTENT;
const isExternalLink = (ref) =>
ref.startsWith('http://') || ref.startsWith('https://');
// Prepare HTML parser with necessary plugins
const processor = unified()
.use(rehypeParse, { fragment: true })
.use(link) // Wrap headings in links, so they became inteactive
.use(externalLinks, {
target: '_blank',
rel: ['noopener', 'noreferrer'],
})
.use(rehypeStringify);
const changelogProcessor = unified()
.use(remarkParse)
.use([
[
remarkExternalLinks,
{
target: '_blank',
rel: ['noopener', 'noreferrer'],
},
],
])
.use(remarkHtml);
perfMon.markAndMeasure('Dependencies loaded', 'timerStart', 'depLoad');
// 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 require a server restart.
// To restart press CTRL + C in terminal and run `gridsome develop`
module.exports = function (api) {
api.loadSource(async ({ addCollection, addSchemaTypes, addMetadata }) => {
perfMon.mark('loadingSourcesStart');
let orphanedLinksCount = 0;
let contentExcludedFromSitemap = 0;
// Use the Data Store API here: https://gridsome.org/docs/data-store-api/
addMetadata('settings', require('./gridsome.config').settings);
// Loading registry with content pages
await registry.init();
perfMon.markAndMeasure(
'Source Files Loaded:',
'loadingSourcesStart',
'loadingSourcesEnd'
);
addSchemaTypes(graphqlSchemaTypes);
const collection = addCollection({
typeName: 'MdnPage',
});
perfMon.markAndMeasure(
'Prepared Base For Building Registry:',
'loadingSourcesEnd',
'startProcessingPages'
);
for (page of registry.getPagesData()) {
const {
content,
description,
headings,
data,
path,
section,
references,
updatesInOriginalRepo,
originalPath,
} = page;
// Perform some additional validation on the content before starting generating website structure
const pageUrl = `${path}/`;
// Check if the page is excluded from sitemap
if (content && excludedUrls.includes(pageUrl)) {
contentExcludedFromSitemap++;
console.warn(
'\x1b[33mwarn\x1b[0m',
`- content page is excluded from sitemap: ${path}`
);
} else if (!content && !excludedUrls.includes(pageUrl)) {
console.warn(
'\x1b[33mwarn\x1b[0m',
`- empty content page is included in sitemap: ${pageUrl}`
);
}
// Check if links in the content lead to sensible destinations
const { internalLinkDestinations } = registry;
references.forEach((refItem) => {
if (
!isExternalLink(refItem) &&
!internalLinkDestinations.has(refItem)
) {
orphanedLinksCount++;
console.warn(
'\x1b[33mwarn\x1b[0m',
`- found orphaned reference: ${refItem} on page ${path}`
);
}
});
collection.addNode({
content,
description,
hasContent: !!content,
headings,
...data,
path,
originalPath,
updatesInOriginalRepo,
section,
sourceLastUpdatetAt: 0,
translationLastUpdatedAt: 0,
});
}
if (orphanedLinksCount > 0) {
console.warn(
`\x1b[33mfound ${orphanedLinksCount} orphaned reference${
orphanedLinksCount > 1 ? 's' : ''
}\x1b[0m`
);
}
if (contentExcludedFromSitemap > 0) {
console.warn(
`\x1b[33m${contentExcludedFromSitemap} page${
contentExcludedFromSitemap > 1 ? 's' : ''
} excluded from sitemap\x1b[0m`
);
}
// Loading changelog
const changelogCollection = addCollection({
typeName: 'changelog',
});
const changelogResolver = async () => {
const changeLogPath = path.resolve(
`${pathToLocalizedContent}/../CHANGELOG.md`
);
const input = await fs.promises.readFile(changeLogPath);
const ast = await changelogProcessor.parse(input);
const headingIndex = ast.children.findIndex(
({ type, depth }) => type === 'heading' && depth === 2
);
ast.children = ast.children
.filter(
(_a, index) => index >= headingIndex && index < headingIndex + 4 // two latest versions
)
.map((node) => ({ ...node, depth: 4 }));
const content = changelogProcessor.stringify(ast);
const { contents: contentWithProcessedLinks } = await processor.process(
content
);
changelogCollection.addNode({
content: contentWithProcessedLinks,
});
};
await Promise.all([changelogResolver()]);
// Popularities
const popularitiesCollection = addCollection({
typeName: 'popularity',
});
const popularities = popularitiesJson;
Object.keys(popularitiesJson)
.filter((key) => key.includes(sourceLocale))
.forEach((key) => {
popularitiesCollection.addNode({
link: key.replace(`/${sourceLocale}/`, `/${targetLocale}/`),
popularity: popularities[key],
});
});
perfMon.markAndMeasure(
'Added Pages To GrapQL registry:',
'startProcessingPages',
'endProcessingPages'
);
});
api.createPages(async ({ createPage, graphql }) => {
// Use the Pages API here: https://gridsome.org/docs/pages-api/
});
};