This is an automated email from the ASF dual-hosted git repository. djencks pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-website.git
The following commit(s) were added to refs/heads/master by this push: new e98ada0 turn menu.js into a antora-registered helper, simplify logic new d06c7b5 Merge pull request #257 from djencks/issue-14764-header-menu e98ada0 is described below commit e98ada0b377dfd2dfcbf7413ddcf1071ee2d5ec2 Author: David Jencks <djen...@apache.org> AuthorDate: Sat Mar 21 13:43:06 2020 -0700 turn menu.js into a antora-registered helper, simplify logic --- antora-ui-camel/src/helpers/withMenuData.js | 39 +++++++++++++++++++++++++ antora-ui-camel/src/partials/header-content.hbs | 12 ++------ menu.js | 36 ----------------------- package.json | 2 +- 4 files changed, 42 insertions(+), 47 deletions(-) diff --git a/antora-ui-camel/src/helpers/withMenuData.js b/antora-ui-camel/src/helpers/withMenuData.js new file mode 100644 index 0000000..13e623c --- /dev/null +++ b/antora-ui-camel/src/helpers/withMenuData.js @@ -0,0 +1,39 @@ +'use strict' + +const fs = require('fs') +const path = require('path') +const toml = require('toml') +const matches = /http\S+/ + +const data = fs.readFileSync(path.join(process.cwd(), 'config.toml'), 'utf8') +const hugoConfig = toml.parse(data) +const mainMenu = hugoConfig.menu.main + +const createMenu = (item) => { + return { + url: item.url || '#', + name: item.name, + children: mainMenu.filter((child) => child.parent === item.identifier).map(createMenu), + } +} + +const menuData = mainMenu.filter((item) => (typeof (item.parent) === 'undefined')).map(createMenu) + +module.exports = (options) => { + const siteRootPath = options.data.root.siteRootPath + const mappedMenuData = menuData.map((item) => mapItem(item, siteRootPath)) + return options.fn(this, { + data: { + items: mappedMenuData, + }, + }) +} + +const mapItem = (item, siteRootPath) => { + const url = item.url === '#' ? '#' : matches.test(item.url) ? item.url : siteRootPath + item.url + return { + url, + name: item.name, + children: item.children.map((child) => mapItem(child, siteRootPath)), + } +} diff --git a/antora-ui-camel/src/partials/header-content.hbs b/antora-ui-camel/src/partials/header-content.hbs index 992c01d..671ea92 100644 --- a/antora-ui-camel/src/partials/header-content.hbs +++ b/antora-ui-camel/src/partials/header-content.hbs @@ -9,22 +9,14 @@ {{#if children}} <div class="navbar-item has-dropdown is-hoverable"> <a class="navbar-link navbar-topics" href="#">{{name}}</a> - {{else}} - <a class="navbar-item navbar-topics" href="{{../../siteRootPath}}{{url}}">{{name}}</a> - {{/if}} - {{#if children}} <div class="navbar-dropdown"> {{#each children}} - {{#hasPrefix url }} <a class="navbar-item" href="{{url}}">{{name}}</a> - {{else}} - <a class="navbar-item" href="{{../../../siteRootPath}}{{url}}">{{name}}</a> - {{/hasPrefix}} {{/each}} </div> - {{/if}} - {{#if children}} </div> + {{else}} + <a class="navbar-item navbar-topics" href="{{url}}">{{name}}</a> {{/if}} {{/each}} {{/withMenuData}} diff --git a/menu.js b/menu.js deleted file mode 100644 index 25844a8..0000000 --- a/menu.js +++ /dev/null @@ -1,36 +0,0 @@ -const fs = require('fs'); -const path = require('path'); -const toml = require('toml'); -const Handlebars = require('handlebars'); - -const data = fs.readFileSync(path.join(__dirname, 'config.toml'), 'utf8'); -const hugoConfig = toml.parse(data); -const mainMenu = hugoConfig.menu.main; - -const createMenu = item => { - return { - url: item.url || '#', - name: item.name, - children: mainMenu.filter(child => child.parent === item.identifier).map(createMenu) - } -} - -const menuData = mainMenu.filter(item => typeof(item.parent) === 'undefined').map(createMenu); - -Handlebars.registerHelper('withMenuData', (options) => { - return options.fn(this, { - data: { - items: menuData - } - }); -}); - -Handlebars.registerHelper('hasPrefix', function(str, options) { - str = Handlebars.Utils.escapeExpression(str); - var matches = new RegExp(/http\S+/); - if (matches.test(str)) { - return options.fn(this); - }else { - return options.inverse(this); - } -}); diff --git a/package.json b/package.json index 5d5ed12..fba9efd 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "version": "1.0.0-SNAPSHOT", "license": "Apache-2.0", "scripts": { - "build:antora": "antora --require ./menu.js --clean --fetch antora-playbook.yml", + "build:antora": "antora --clean --fetch antora-playbook.yml", "build:hugo": "hugo --cacheDir ${HUGO_CACHE_DIR:-$(pwd)/.hugo_data}", "build:minify": "gulp minify", "build": "run-s build:*",