forked from slsfi/digital-edition-frontend-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prebuild-generate-sitemap.js
241 lines (209 loc) · 7.84 KB
/
prebuild-generate-sitemap.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
const fs = require('fs');
const path = require('path');
const common = require('./prebuild-common-fns');
const configFilepath = 'src/assets/config/config.ts';
const sitemapFilename = 'sitemap.txt';
generateSitemap();
/**
* Generates a simple sitemap text file in the root folder `src/`.
* The file contains one URL per line. The script uses the settings
* in config.ts to fetch data about the project and generate the
* URLs to all unique pages. The following page types are included:
* - Home
* - About
* - Ebook
* - Collection
* - Media collection
* Because the sitemap is a plain text file, only the URLs to pages
* in the default language are generated.
*/
async function generateSitemap() {
const config = common.getConfig(configFilepath);
const generateSitemap = config.app?.prebuild?.sitemap ?? true;
if (generateSitemap) {
console.log('Generating sitemap ...');
} else {
console.log('Skipping generation of sitemap, disabled in config.\n');
return;
}
const projectName = config.app?.projectNameDB ?? '';
const API = config.app?.backendBaseURL ?? '';
let urlOrigin = config.app?.siteURLOrigin ?? '';
const locale = config.app?.i18n?.defaultLanguage ?? 'sv';
const multilingualCollectionTOC = config.app?.i18n?.multilingualCollectionTableOfContents ?? false;
const collectionCovers = config.collections?.frontMatterPages?.cover ?? false;
const collectionTitles = config.collections?.frontMatterPages?.title ?? false;
const collectionForewords = config.collections?.frontMatterPages?.foreword ?? false;
const collectionIntros = config.collections?.frontMatterPages?.introduction ?? false;
const APIBase = API + '/' + projectName;
if (urlOrigin.length && urlOrigin[urlOrigin.length - 1] === '/') {
// remove trailing slash from url origin
urlOrigin = urlOrigin.slice(0, -1);
}
let urlCounter = 0;
// Initialize sitemap file (adds home page URL to it)
let success = initializeSitemapFile(urlOrigin, locale);
if (!success) {
console.log('Error: unable to initialize sitemap file.');
return;
} else {
urlCounter += 1;
}
// Get about-pages URLs
if (config.component?.mainSideMenu?.items?.about) {
let aboutPages = await common.fetchFromAPI(APIBase + '/static-pages-toc/' + locale);
if (aboutPages && aboutPages.children) {
urlCounter += generateAboutPagesURLs(aboutPages.children, '03', urlOrigin, locale);
}
}
// Get ebook URLs
if (config.component?.mainSideMenu?.items?.ebooks && config.ebooks?.length) {
urlCounter += generateEbookURLs(config.ebooks, urlOrigin, locale);
}
// Get collection URLs
if (config.collections?.order?.length) {
let collectionsEndpoint = APIBase + '/collections';
if (multilingualCollectionTOC) {
collectionsEndpoint += '/' + locale;
}
// Fetch all collections from the backend
const allCollections = await common.fetchFromAPI(collectionsEndpoint);
const includedCollectionIds = config.collections.order.flat();
// Filter out the collections that are not included according to the config
const collections = allCollections.filter(coll => includedCollectionIds.includes(coll.id));
if (collections) {
if (collectionCovers) {
urlCounter += await generateCollectionURLs(collections, 'cover', urlOrigin, locale);
}
if (collectionTitles) {
urlCounter += await generateCollectionURLs(collections, 'title', urlOrigin, locale);
}
if (collectionForewords) {
urlCounter += await generateCollectionURLs(collections, 'foreword', urlOrigin, locale);
}
if (collectionIntros) {
urlCounter += await generateCollectionURLs(collections, 'introduction', urlOrigin, locale);
}
urlCounter += await generateCollectionURLs(collections, 'text', urlOrigin, locale, APIBase, multilingualCollectionTOC);
}
}
// Get media collection URLs
if (config.component?.mainSideMenu?.items?.mediaCollections) {
const mediaCollections = await common.fetchFromAPI(APIBase + '/gallery/data/' + locale);
if (mediaCollections && mediaCollections.length) {
urlCounter += await generateMediaCollectionURLs(mediaCollections, urlOrigin, locale);
}
}
const summary = `Generated URLs: ${urlCounter}\n`;
console.log(summary);
}
function initializeSitemapFile(urlOrigin, locale) {
try {
fs.writeFileSync(path.join(__dirname, 'src/' + sitemapFilename), urlOrigin + '/' + locale + '/\n');
return true;
} catch (err) {
console.error(err);
return false;
}
}
function appendToSitemapFile(content) {
try {
fs.appendFileSync(path.join(__dirname, 'src/' + sitemapFilename), content);
} catch (err) {
console.error(err);
}
}
function generateAboutPagesURLs(aboutPages, mdFolderNumber, urlOrigin, locale) {
for (let p = 0; p < aboutPages.length; p++) {
if (aboutPages[p]['id'] === locale + '-' + mdFolderNumber) {
aboutPages = aboutPages[p];
break;
}
}
let counter = 0;
const aboutPagesList = common.flattenObjectTree(aboutPages, 'children', 'id');
for (let z = 0; z < aboutPagesList.length; z++) {
if (aboutPagesList[z]['type'] === 'file' && aboutPagesList[z]['id']) {
const id = aboutPagesList[z]['id'].replace(locale + '-', '');
const url = `${urlOrigin}/${locale}/about/${id}`;
appendToSitemapFile(url + '\n');
counter += 1;
}
}
return counter;
}
function generateEbookURLs(epubs, urlOrigin, locale) {
let counter = 0;
for (let i = 0; i < epubs.length; i++) {
const url = `${urlOrigin}/${locale}/ebook/${epubs[i]['filename']}`;
appendToSitemapFile(url + '\n');
counter += 1;
}
return counter;
}
async function generateCollectionURLs(collections, part, urlOrigin, locale, API = undefined, multilingualTOC = false) {
let counter = 0;
for (let i = 0; i < collections.length; i++) {
if (part === 'text') {
counter += await generateCollectionTextURLs(collections[i]['id'] || 0, urlOrigin, locale, API, multilingualTOC);
} else {
const url = `${urlOrigin}/${locale}/collection/${collections[i]['id']}/${part}`;
appendToSitemapFile(url + '\n');
counter += 1;
}
}
return counter;
}
async function generateCollectionTextURLs(collection_id, urlOrigin, locale, API, multilingualTOC) {
let endpoint = API + '/toc/' + collection_id;
if (multilingualTOC) {
endpoint += '/' + locale;
}
const tocJSON = await common.fetchFromAPI(endpoint);
const toc = common.flattenObjectTree(tocJSON, 'children', 'itemId');
let counter = 0;
let addedUrls = [];
for (let i = 0; i < toc.length; i++) {
if (toc[i]['itemId']) {
const itemId = toc[i]['itemId'].split(';')[0];
let prevItemId = undefined;
if (i > 0 && toc[i - 1]['itemId']) {
prevItemId = toc[i - 1]['itemId'].split(';')[0];
}
if (itemId !== prevItemId) {
const itemIdParts = itemId.split('_');
if (itemIdParts.length > 1) {
const textId = itemIdParts[1];
const chapterId = itemIdParts[2] || '';
let url = `${urlOrigin}/${locale}/collection/${collection_id}/text/${textId}`;
if (chapterId) {
url += '/' + chapterId;
}
let newUrl = true;
for (let x = 0; x < addedUrls.length; x++) {
if (url === addedUrls[x]) {
newUrl = false;
break;
}
}
if (newUrl) {
addedUrls.push(url);
appendToSitemapFile(url + '\n');
counter++;
}
}
}
}
}
return counter;
}
async function generateMediaCollectionURLs(mediaCollections, urlOrigin, locale) {
let counter = 1;
appendToSitemapFile(`${urlOrigin}/${locale}/media-collection` + '\n');
for (let i = 0; i < mediaCollections.length; i++) {
const url = `${urlOrigin}/${locale}/media-collection/${mediaCollections[i]['id']}`;
appendToSitemapFile(url + '\n');
counter += 1;
}
return counter;
}