File size: 4,032 Bytes
e1ae2c6 | 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | const fs = require('node:fs');
const path = require('node:path');
const { assignCategorySlugs } = require('./slugs');
const MENAV_EXTENSION_CONFIG_FILE = 'menav-config.json';
function getSubmenuForNavItem(navItem, config) {
if (!navItem || !navItem.id || !config) {
return null;
}
if (config[navItem.id] && Array.isArray(config[navItem.id].categories))
return config[navItem.id].categories;
return null;
}
function makeJsonSafeForHtmlScript(jsonString) {
if (typeof jsonString !== 'string') {
return '';
}
return jsonString.replace(/<\/script/gi, '<\\/script');
}
function resolveTemplateNameForPage(pageId, config) {
if (!pageId) return 'page';
const pageConfig = config && config[pageId] ? config[pageId] : null;
const explicit = pageConfig && pageConfig.template ? String(pageConfig.template).trim() : '';
if (explicit) return explicit;
const candidatePath = path.join(process.cwd(), 'templates', 'pages', `${pageId}.hbs`);
if (fs.existsSync(candidatePath)) return pageId;
return 'page';
}
function buildExtensionConfig(renderData) {
const version =
renderData &&
renderData._meta &&
renderData._meta.version &&
String(renderData._meta.version).trim()
? String(renderData._meta.version).trim()
: process.env.npm_package_version || '1.0.0';
const pageTemplates = {};
if (renderData && Array.isArray(renderData.navigation)) {
renderData.navigation.forEach((navItem) => {
const pageId = navItem && navItem.id ? String(navItem.id).trim() : '';
if (!pageId) return;
pageTemplates[pageId] = resolveTemplateNameForPage(pageId, renderData);
});
}
const allowedSchemes =
renderData &&
renderData.site &&
renderData.site.security &&
Array.isArray(renderData.site.security.allowedSchemes)
? renderData.site.security.allowedSchemes
: null;
return {
version,
timestamp: new Date().toISOString(),
icons: renderData && renderData.icons ? renderData.icons : undefined,
data: {
homePageId: renderData && renderData.homePageId ? renderData.homePageId : null,
pageTemplates,
site: allowedSchemes ? { security: { allowedSchemes } } : undefined,
},
};
}
function prepareRenderData(config) {
const renderData = { ...config };
renderData._meta = {
generated_at: new Date(),
version: process.env.npm_package_version || '1.0.0',
generator: 'MeNav',
};
if (!Array.isArray(renderData.navigation)) {
renderData.navigation = [];
}
if (Array.isArray(renderData.navigation)) {
renderData.navigation = renderData.navigation.map((item, index) => {
const navItem = {
...item,
isActive: index === 0,
id: item.id || `nav-${index}`,
active: index === 0,
};
const submenu = getSubmenuForNavItem(navItem, renderData);
if (submenu) {
navItem.submenu = submenu;
}
return navItem;
});
}
renderData.homePageId =
renderData.navigation && renderData.navigation[0] ? renderData.navigation[0].id : null;
if (Array.isArray(renderData.navigation)) {
renderData.navigation.forEach((navItem) => {
const pageConfig = renderData[navItem.id];
if (pageConfig && Array.isArray(pageConfig.categories)) {
assignCategorySlugs(pageConfig.categories, new Map());
}
});
}
const extensionConfig = buildExtensionConfig(renderData);
renderData.extensionConfig = extensionConfig;
renderData.extensionConfigUrl = `./${MENAV_EXTENSION_CONFIG_FILE}`;
renderData.configJSON = makeJsonSafeForHtmlScript(
JSON.stringify({
...extensionConfig,
configUrl: renderData.extensionConfigUrl,
})
);
renderData.navigationData = renderData.navigation;
if (Array.isArray(renderData.social)) {
renderData.socialLinks = renderData.social;
}
return renderData;
}
module.exports = {
MENAV_EXTENSION_CONFIG_FILE,
getSubmenuForNavItem,
resolveTemplateNameForPage,
buildExtensionConfig,
prepareRenderData,
};
|