forked from apache/hop-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.js
37 lines (32 loc) · 978 Bytes
/
menu.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
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,
target: item.pre,
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);
}
});