forked from jsdelivr/bootstrapcdn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
167 lines (129 loc) · 3.78 KB
/
helpers.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
'use strict';
const fs = require('fs');
const path = require('path');
const semver = require('semver');
const sri = require('sri-toolbox');
const { app, files } = require('../config');
const PUBLIC_DIR = path.join(__dirname, '../public/');
const SRI_CACHE = {};
function capitalize(str) {
if (typeof str !== 'string') {
return '';
}
return str.charAt(0).toUpperCase() + str.slice(1);
}
function generateSri(file, fromString) {
if (typeof SRI_CACHE[file] === 'undefined') {
file = fromString ? file : fs.readFileSync(file);
SRI_CACHE[file] = sri.generate({ algorithms: ['sha384'] }, file);
}
return SRI_CACHE[file];
}
// This is used only in the templates
function getSri(file) {
if (typeof SRI_CACHE[file] === 'undefined') {
SRI_CACHE[file] = generateSri(path.join(PUBLIC_DIR, file));
}
return SRI_CACHE[file];
}
function generateDataJson() {
const data = {
timestamp: new Date().toISOString(),
bootstrap: {},
fontawesome: {}
};
files.bootstrap.forEach((bootstrap) => {
const bootstrapVersion = bootstrap.version;
if (semver.satisfies(semver.coerce(bootstrapVersion), '<4')) {
data.bootstrap[bootstrapVersion] = {
css: bootstrap.stylesheet,
js: bootstrap.javascript
};
}
});
files['@fortawesome/fontawesome-free'].forEach((fontawesome) => {
data.fontawesome[fontawesome.version] = fontawesome.stylesheet;
});
return data;
}
function getCurrentSiteurl(req) {
let proto = req.get('x-forwarded-proto');
if (typeof proto === 'undefined') {
proto = req.protocol;
}
return `${proto}://${req.hostname}`;
}
function getPageTitle(pageTitle) {
return `${pageTitle} · ${app.title_suffix}`;
}
function generateBodyClass(pathname) {
if (pathname === '/') {
return 'page-home'; // only for the homepage
}
// Remove any backslashes
pathname = pathname.replace(/\//g, '');
// Make the first letter lowercase
pathname = pathname.charAt(0).toLowerCase() + pathname.slice(1);
return `page-${pathname}`;
}
function selectedTheme(selected) {
if (typeof selected === 'undefined' || selected === '') {
return app.theme;
}
const theme = Number.parseInt(selected, 10);
return theme === 0 || theme ?
theme :
app.theme;
}
function getTheme(selected) {
const { themes } = files.bootswatch4;
selected = selectedTheme(selected);
return {
uri: files.bootswatch4.bootstrap
.replace('SWATCH_VERSION', files.bootswatch4.version)
.replace('SWATCH_NAME', themes[selected].name),
sri: themes[selected].sri
};
}
function getThemeQuery(req) {
const totalThemes = files.bootswatch4.themes.length;
const query = Number.parseInt(req.query.theme, 10);
// Safety checks
if (Number.isNaN(query) || query < 0 || query >= totalThemes) {
return '';
}
return String(query);
}
function appendLocals(req, res) {
const siteUrl = getCurrentSiteurl(req);
const pageUrl = req.originalUrl;
// OK, hack-ish way...
const pathname = pageUrl.split('?')[0];
const canonicalUrl = new URL(pathname, app.siteurl);
const theme = getThemeQuery(req);
const bodyClass = generateBodyClass(pathname);
const locals = {
siteUrl,
canonicalUrl,
pageUrl,
theme,
bodyClass
};
res.locals = Object.assign(res.locals, locals);
return res;
}
module.exports = {
appendLocals,
capitalize,
generateDataJson,
theme: {
get: getTheme,
selected: selectedTheme
},
getCurrentSiteurl,
getPageTitle,
generateBodyClass,
generateSri,
getSri
};
// vim: ft=javascript sw=4 sts=4 et: