Skip to content

Commit

Permalink
Merge 0286e00 into 7cafe6d
Browse files Browse the repository at this point in the history
  • Loading branch information
XhmikosR committed May 22, 2018
2 parents 7cafe6d + 0286e00 commit 72fb82d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
3 changes: 2 additions & 1 deletion config/_app.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
port: 3333
theme: 1
siteurl: 'https://www.bootstrapcdn.com'
description: 'The recommended CDN for Bootstrap, Font Awesome and Bootswatch.'
title_suffix: 'BootstrapCDN by StackPath'
authors:
- name: Justin Dorfman
twitter: jdorfman
Expand All @@ -17,7 +19,6 @@ authors:
- name: XhmikosR
twitter: XhmikosR
url: 'https://xhmikosr.io/'
description: 'The recommended CDN for Bootstrap, Font Awesome and Bootswatch.'
favicon:
uri: /assets/img/favicons/favicon.ico
stylesheet:
Expand Down
66 changes: 47 additions & 19 deletions routes/appendLocals.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,71 @@ const helpers = require('../lib/helpers.js');
const config = require('../config');

const digest = helpers.sri.digest;
const PUBLIC_DIR = path.join(__dirname, '../public/');
const SRI_CACHE = {};

function appendLocals(req, res) {
const totalThemes = config.bootswatch4.themes.length;
const TITLE_SUFFIX = 'BootstrapCDN by StackPath';
function getProto(req) {
let proto = req.get('x-forwarded-proto');

if (typeof proto === 'undefined') {
proto = req.protocol;
}

res.locals.canonicalUrl = `${req.config.siteurl}${req.path}`;
return proto;
}

res.locals.siteUrl = `${proto}://${req.hostname}`;
function getSiteurl(req) {
const proto = getProto(req);

return `${proto}://${req.hostname}`;
}

function getCanonicalUrl(req) {
return `${config.siteurl}${req.path}`;
}

function getPageTitle(pageTitle) {
return `${pageTitle} · ${config.title_suffix}`;
}

res.locals.theme = req.query.theme < totalThemes ?
function getThemeQuery(req) {
const totalThemes = config.bootswatch4.themes.length;

return req.query.theme < totalThemes ?
req.query.theme :
'';
}

res.locals.displayTitle = (title) => `${title} · ${TITLE_SUFFIX}`;
function generateBodyClass(pageTitle) {
// Remove any whitespace from the page title
let str = pageTitle.replace(/\s/g, '');

res.locals.bodyClass = (title) => {
// Remove any whitespace from the title
let str = title.replace(/\s/g, '');
// Make the first letter lowercase
str = str.charAt(0).toLowerCase() + str.slice(1);

// Make the first letter lowercase
str = str.charAt(0).toLowerCase() + str.slice(1);
return `page-${str}`;
}

return `page-${str}`;
};
function generateSRI(file) {
if (typeof SRI_CACHE[file] === 'undefined') {
SRI_CACHE[file] = digest(path.join(PUBLIC_DIR, file));
}

res.locals.generateSRI = (file) => {
if (typeof SRI_CACHE[file] === 'undefined') {
SRI_CACHE[file] = digest(path.join(__dirname, '../public', file));
}
return SRI_CACHE[file];
}

return SRI_CACHE[file];
function appendLocals(req, res) {
const siteUrl = getSiteurl(req);
const canonicalUrl = getCanonicalUrl(req);
const theme = getThemeQuery(req);

res.locals = {
siteUrl,
canonicalUrl,
theme,
displayTitle: getPageTitle,
bodyClass: generateBodyClass,
generateSRI
};

return res;
Expand Down

0 comments on commit 72fb82d

Please sign in to comment.