diff --git a/lib/helpers.js b/lib/helpers.js index 23939eb93..d42ff9fe8 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -28,31 +28,6 @@ function generateSri(file, fromString) { return SRI_CACHE[file]; } -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 generateDataJson() { const data = { timestamp: new Date().toISOString(), @@ -106,7 +81,67 @@ function generateBodyClass(pathname) { 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 = req.query.theme; + + // Safety checks + if (Number.isNaN(query) || query < 0 || query >= totalThemes) { + return ''; + } + + return 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: { diff --git a/routes/404.js b/routes/404.js index b1b12bfe6..772f77cbf 100644 --- a/routes/404.js +++ b/routes/404.js @@ -1,7 +1,7 @@ 'use strict'; const express = require('express'); -const appendLocals = require('./appendLocals'); +const { appendLocals } = require('../lib/helpers'); const router = express.Router(); diff --git a/routes/about.js b/routes/about.js index 4824e3952..dd74d0ceb 100644 --- a/routes/about.js +++ b/routes/about.js @@ -1,7 +1,7 @@ 'use strict'; const express = require('express'); -const appendLocals = require('./appendLocals'); +const { appendLocals } = require('../lib/helpers'); const router = express.Router(); diff --git a/routes/appendLocals.js b/routes/appendLocals.js deleted file mode 100644 index db0faa307..000000000 --- a/routes/appendLocals.js +++ /dev/null @@ -1,45 +0,0 @@ -'use strict'; - -const { app, files } = require('../config'); -const { - getCurrentSiteurl, - generateBodyClass -} = require('../lib/helpers'); - -function getThemeQuery(req) { - const totalThemes = files.bootswatch4.themes.length; - const query = req.query.theme; - - // Safety checks - if (Number.isNaN(query) || query < 0 || query >= totalThemes) { - return ''; - } - - return 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; - -// vim: ft=javascript sw=4 sts=4 et: diff --git a/routes/books.js b/routes/books.js index 62ec73dc1..b5be81f85 100644 --- a/routes/books.js +++ b/routes/books.js @@ -1,7 +1,7 @@ 'use strict'; const express = require('express'); -const appendLocals = require('./appendLocals'); +const { appendLocals } = require('../lib/helpers'); const router = express.Router(); diff --git a/routes/bootlint.js b/routes/bootlint.js index cb3e5d888..7f83f09b6 100644 --- a/routes/bootlint.js +++ b/routes/bootlint.js @@ -1,7 +1,7 @@ 'use strict'; const express = require('express'); -const appendLocals = require('./appendLocals'); +const { appendLocals } = require('../lib/helpers'); const router = express.Router(); diff --git a/routes/bootswatch.js b/routes/bootswatch.js index 485c09516..652e29bbe 100644 --- a/routes/bootswatch.js +++ b/routes/bootswatch.js @@ -1,7 +1,7 @@ 'use strict'; const express = require('express'); -const appendLocals = require('./appendLocals'); +const { appendLocals } = require('../lib/helpers'); const router = express.Router(); diff --git a/routes/bootswatch4.js b/routes/bootswatch4.js index 0175ead67..f543e7dcb 100644 --- a/routes/bootswatch4.js +++ b/routes/bootswatch4.js @@ -1,7 +1,7 @@ 'use strict'; const express = require('express'); -const appendLocals = require('./appendLocals'); +const { appendLocals } = require('../lib/helpers'); const router = express.Router(); diff --git a/routes/fontawesome.js b/routes/fontawesome.js index 4d2373be2..47ccd31e1 100644 --- a/routes/fontawesome.js +++ b/routes/fontawesome.js @@ -1,7 +1,7 @@ 'use strict'; const express = require('express'); -const appendLocals = require('./appendLocals'); +const { appendLocals } = require('../lib/helpers'); const router = express.Router(); diff --git a/routes/home.js b/routes/home.js index 53f2a48f3..d43915304 100644 --- a/routes/home.js +++ b/routes/home.js @@ -1,7 +1,7 @@ 'use strict'; const express = require('express'); -const appendLocals = require('./appendLocals'); +const { appendLocals } = require('../lib/helpers'); const router = express.Router(); diff --git a/routes/integrations.js b/routes/integrations.js index 9fdd30582..779cd458d 100644 --- a/routes/integrations.js +++ b/routes/integrations.js @@ -1,7 +1,7 @@ 'use strict'; const express = require('express'); -const appendLocals = require('./appendLocals'); +const { appendLocals } = require('../lib/helpers'); const router = express.Router(); diff --git a/routes/jobs.js b/routes/jobs.js index 35b9e96ab..65b5344e3 100644 --- a/routes/jobs.js +++ b/routes/jobs.js @@ -1,7 +1,7 @@ 'use strict'; const express = require('express'); -const appendLocals = require('./appendLocals'); +const { appendLocals } = require('../lib/helpers'); const router = express.Router(); diff --git a/routes/legacy.js b/routes/legacy.js index 06b8b3e53..24a47d3ef 100644 --- a/routes/legacy.js +++ b/routes/legacy.js @@ -1,7 +1,7 @@ 'use strict'; const express = require('express'); -const appendLocals = require('./appendLocals'); +const { appendLocals } = require('../lib/helpers'); const router = express.Router(); diff --git a/routes/privacyPolicy.js b/routes/privacyPolicy.js index 17678bdf2..3a5791fcb 100644 --- a/routes/privacyPolicy.js +++ b/routes/privacyPolicy.js @@ -1,7 +1,7 @@ 'use strict'; const express = require('express'); -const appendLocals = require('./appendLocals'); +const { appendLocals } = require('../lib/helpers'); const router = express.Router(); diff --git a/routes/redirectToRoot.js b/routes/redirectToRoot.js index aa23a1525..578f8165d 100644 --- a/routes/redirectToRoot.js +++ b/routes/redirectToRoot.js @@ -1,7 +1,7 @@ 'use strict'; const express = require('express'); -const appendLocals = require('./appendLocals'); +const { appendLocals } = require('../lib/helpers'); const router = express.Router(); diff --git a/routes/showcase.js b/routes/showcase.js index a3e3b725d..82f1ae235 100644 --- a/routes/showcase.js +++ b/routes/showcase.js @@ -1,7 +1,7 @@ 'use strict'; const express = require('express'); -const appendLocals = require('./appendLocals'); +const { appendLocals } = require('../lib/helpers'); const router = express.Router(); diff --git a/routes/themes.js b/routes/themes.js index aa75315d9..670b2060e 100644 --- a/routes/themes.js +++ b/routes/themes.js @@ -1,7 +1,7 @@ 'use strict'; const express = require('express'); -const appendLocals = require('./appendLocals'); +const { appendLocals } = require('../lib/helpers'); const router = express.Router();