From 7ee87c7e5444ff4a1cbfc2d7f7cd24279e68c477 Mon Sep 17 00:00:00 2001 From: mahula Date: Fri, 17 Nov 2023 17:25:44 +0100 Subject: [PATCH] move theme config in separate file and use theme hope --- .vuepress/config/theme.js | 82 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 .vuepress/config/theme.js diff --git a/.vuepress/config/theme.js b/.vuepress/config/theme.js new file mode 100644 index 000000000..214cfb862 --- /dev/null +++ b/.vuepress/config/theme.js @@ -0,0 +1,82 @@ +import path from 'path' +import fs from 'fs' +import { hopeTheme } from 'vuepress-theme-hope' + +export default hopeTheme({ + favicon: 'favicon.ico', + logo: '/logo.svg', + docsRepo: 'https://github.com/Ocelot-Social-Community/Ocelot-Social', + docsBranch: 'master', + docsDir: '.', + editLink: true, + lastUpdated: false, + contributors: false, + print: false, + pure: true, + displayFooter: true, + footer: 'CC BY busFaktor() e.V. & Authors', + sidebar: generateSidebar('../../SUMMARY.md'), + navbar: [ + { text: 'Home', link: '/' }, + { + text: 'Github', + link: 'https://github.com/Ocelot-Social-Community/Ocelot-Social' + }, + ], + plugins: { + mdEnhance: { + tabs: true, + imgSize: true + } + } +}) + +function generateSidebar(summaryFileName) { + const summaryFile = path.resolve(__dirname, summaryFileName) + + try { + return getSummaryData(summaryFile) + } catch (err) { + console.error(`Error generating sidebar from file ${summaryFileName}:`, err) + process.exit(1) + } +} + +function getSummaryData(file) { + const lines = fs.readFileSync(file, 'utf8').split('\n') + const sidebarStructure = [] + let currentParent = null + + lines.forEach((line, i) => { + const level = line.search(/\S|$/) / 2 + const match = line.match(/^\s*\*\s*\[([^\]]+)\]\(([^)]+)\)/) + + if (match) { + const newEntry = { text: match[1], link: `/${match[2]}`, children: [] } + if (level === 0) { + sidebarStructure.push(newEntry) + currentParent = sidebarStructure[sidebarStructure.length - 1] + } else { + let parent = currentParent + + for (let i = 1; i < level; i++) { + parent = parent.children[parent.children.length - 1] + } + + parent.children.push(newEntry) + } + } + }) + + sidebarStructure.forEach(removeEmptyArrays) + return sidebarStructure +} + +function removeEmptyArrays(item) { + if (item.children && item.children.length === 0) { + delete item.children; + } else if (item.children) { + item.children.forEach(removeEmptyArrays); + } +} +