From e471cd6e263d9868b096ec17ec1b1f1604fb871c Mon Sep 17 00:00:00 2001 From: hoyce mac work Date: Mon, 6 Mar 2017 11:45:00 +0100 Subject: [PATCH] Added optional override for site name url --- README.md | 1 + index.js | 26 ++++++++++++++++---------- package.json | 2 +- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index adf18c8..31ab4fd 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,7 @@ function prepare(blocks, req, config) { - `request` is required. Usually the express `req.url` value. - `app` is optional, defaults to an empty string. Usually the host URL and the proxy prefix path. + - `siteUrl` is optional, overides default url on the site name only. - `selectors` is optional, defaults to a plain object with the following properties: - `logo` is optional, defaults to `.imageWrapper img`. diff --git a/index.js b/index.js index 72242f9..5a225e3 100644 --- a/index.js +++ b/index.js @@ -30,7 +30,8 @@ const prepareDefaults = { urls: { prod: '//www.kth.se', request: null, - app: '' + app: '', + siteUrl: null }, siteName: null, localeText: null, @@ -79,12 +80,12 @@ function _getAll (config) { return Promise.all( handleBlocks(config) ).then(function (results) { - let result = {} - results.forEach(function (block) { - result[block.blockName] = block.result - }) - return result + let result = {} + results.forEach(function (block) { + result[block.blockName] = block.result }) + return result + }) } /** @@ -96,8 +97,8 @@ function handleBlocks (config) { let blocks = [] let blocksObj = config.blocks for (let i in blocksObj) { - if(blocksObj.hasOwnProperty(i)) { - if(isLanguage (blocksObj[i])) { + if (blocksObj.hasOwnProperty(i)) { + if (isLanguage(blocksObj[i])) { blocks.push(_getBlock(config, 'language', true)) } else { blocks.push(_getBlock(config, i)) @@ -112,7 +113,7 @@ function handleBlocks (config) { * @param blockObj the given object. * @returns {boolean} true if language object. */ -function isLanguage(blockObj) { +function isLanguage (blockObj) { return typeof blockObj === 'object' } @@ -219,6 +220,7 @@ module.exports = function (config) { * @param {String} [config.urls.prod='//www.kth.se'] * @param {String} config.urls.request - Current request URL (usually from req.url). * @param {String} config.urls.app - The application host name and prefix path. + * @param {String} config.urls.siteUrl - The url to overide app url in sitename if needed. * @param {Object} [config.selectors] - Optional plain object with CSS selectors. * @param {String} [config.selectors.logo='.imageWrapper img'] * @param {String} [config.selectors.siteName='.siteName a'] @@ -242,7 +244,11 @@ module.exports.prepare = function (blocks, config) { $ = cheerio.load(blocks.title) $el = $(config.selectors.siteName) if ($el.length) { - $el.attr('href', config.urls.app) + if (config.urls.siteUrl) { + $el.attr('href', config.urls.siteUrl) + } else { + $el.attr('href', config.urls.app) + } if (config.siteName) { $el.text(config.siteName) diff --git a/package.json b/package.json index f8248b5..f77eacf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kth-node-cortina-block", - "version": "1.0.1", + "version": "1.0.2", "description": "Node.js module for fetching Cortina blocks and optionally cache using Redis.", "main": "index.js", "scripts": {