From aeb05bf6ec687fbfa434a5fc0dfb0c9c7fc771ef Mon Sep 17 00:00:00 2001 From: wass Date: Thu, 9 May 2024 20:12:26 +0200 Subject: [PATCH] updated to new content structure --- .vscode/settings.json | 5 +++ config.js | 11 ++++--- content/3dprinting/readme.md | 2 +- content/frameworks/readme.md | 2 +- content/home/readme.md | 1 + content/icons.yaml | 3 ++ content/other/readme.md | 2 +- content/protocols/readme.md | 5 +++ content/web/readme.md | 2 +- integrations/create_menu.js | 61 +++++++++++++++++++++++++++++++---- menu.yaml | 29 ----------------- package.json | 2 +- src/layout/AppBar.astro | 11 ++----- src/layout/layout_utils.js | 5 +++ src/libs/utils.js | 7 ++++ src/pages/assets/[...path].js | 4 +-- src/pages/codes/[...path].js | 4 +-- 17 files changed, 98 insertions(+), 58 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 content/icons.yaml create mode 100644 content/protocols/readme.md delete mode 100644 menu.yaml diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..bb67143 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "yaml.schemas": { + "https://json.schemastore.org/github-workflow.json": "file:///d%3A/Dev/HomeSmartMesh/website/.github/workflows/deploy.yml" + } +} \ No newline at end of file diff --git a/config.js b/config.js index be2d95c..4b768b2 100644 --- a/config.js +++ b/config.js @@ -1,18 +1,20 @@ //in DEV Mode process.env does not have .env content import * as dotenv from 'dotenv' +import {join} from 'path' dotenv.config() const rootdir = process.cwd() const outdir = (process.env.OUT_DIR==null)?"dist":process.env.OUT_DIR const base = (process.env.PUBLIC_BASE==null)?"":process.env.PUBLIC_BASE -const contentdir = "content" +const contentdir = join(rootdir,"content") +const structuredir = join(rootdir,".structure") const config = { rootdir: rootdir, outDir: outdir, base: base, - content_path: `${rootdir}/${contentdir}`, + content_path: contentdir, code_path: `${rootdir}/${outdir}/codes`, plantuml_server: "https://www.plantuml.com/plantuml/svg", kroki_server: "https://kroki.io", @@ -28,11 +30,10 @@ const config = { config.collect_content = { rootdir:config.rootdir, - rel_contentdir:contentdir, + contentdir:contentdir, content_ext:["md"], assets_ext:["svg","webp","png","jpeg","jpg","xlsx","glb","hdr","ico"], - rel_outdir:".structure",//dist does not persist before build - raw_menu:"menu.yaml", + outdir:structuredir, out_menu:"public/menu.json",//used by src\layout\client_nav_menu.js debug:false } diff --git a/content/3dprinting/readme.md b/content/3dprinting/readme.md index 5d6254d..fb6fb8c 100644 --- a/content/3dprinting/readme.md +++ b/content/3dprinting/readme.md @@ -2,7 +2,7 @@ title : 3D Printing slug: 3dprinting description: 3D Models for Smart Home, electronics and house utilities -order: 9 +order: 5 date: 2021-01-17T00:00:00+00:00 image: ./3dmodels.png features: diff --git a/content/frameworks/readme.md b/content/frameworks/readme.md index ed43895..bd86ec2 100644 --- a/content/frameworks/readme.md +++ b/content/frameworks/readme.md @@ -1,7 +1,7 @@ --- title : Frameworks description: Services, middleware and environments for applications eco-systems -order: 1 +order: 3 date: 2020-10-06T08:48:23+00:00 lastmod: 2020-10-06T08:48:23+00:00 --- diff --git a/content/home/readme.md b/content/home/readme.md index 4366d4f..2d5dd46 100644 --- a/content/home/readme.md +++ b/content/home/readme.md @@ -5,6 +5,7 @@ date: 2020-10-06T08:48:23+00:00 lastmod: 2021-02-14T08:00:00+00:00 image: /images/floor_temperature.png toc: false +order: 1 --- ```yaml cards diff --git a/content/icons.yaml b/content/icons.yaml new file mode 100644 index 0000000..8915e6f --- /dev/null +++ b/content/icons.yaml @@ -0,0 +1,3 @@ +- icon: github + align: right + link: https://github.com/HomeSmartMesh/website-astro diff --git a/content/other/readme.md b/content/other/readme.md index f5b5d3b..8ce04d6 100644 --- a/content/other/readme.md +++ b/content/other/readme.md @@ -1,7 +1,7 @@ --- title : Other description: Other Miscelaneous projects e.g. Sound processing, Home Robotics and electromechanical smart automation -order: 4 +order: 7 date: 2021-03-07T08:00:00+00:00 lastmod: 2021-03-07T08:00:00+00:00 --- diff --git a/content/protocols/readme.md b/content/protocols/readme.md new file mode 100644 index 0000000..e159f88 --- /dev/null +++ b/content/protocols/readme.md @@ -0,0 +1,5 @@ +--- +title: Protocols +order: 4 +--- +protocols page \ No newline at end of file diff --git a/content/web/readme.md b/content/web/readme.md index c7cb096..b6cffbe 100644 --- a/content/web/readme.md +++ b/content/web/readme.md @@ -2,5 +2,5 @@ title : Web description: Front-end Webapps, smartphone and real world use cases with buttons date: 2020-12-31T00:00:00+00:00 -order: 10 +order: 6 --- diff --git a/integrations/create_menu.js b/integrations/create_menu.js index 1486779..3622c48 100644 --- a/integrations/create_menu.js +++ b/integrations/create_menu.js @@ -1,10 +1,12 @@ import {dirname, join} from 'path' -import { load_yaml, save_json } from '../src/libs/utils.js'; +import { exists, load_yaml, load_yaml_abs,save_json } from '../src/libs/utils.js'; import { section_from_pathname,add_base } from '../src/libs/assets.js'; import {pages_list_to_tree} from './process_menu.js' import {getDocuments} from 'content-structure' import {createHash} from 'crypto' import { config } from '../config.js'; +import { readdir, stat } from 'fs/promises'; +import path from 'path'; function content_entry_to_level(entry){ const base_level = 1 @@ -62,18 +64,63 @@ async function get_section_menu(section,raw_menu){ return result_items } +async function create_raw_menu(content_path){ + const items = await readdir(content_path, { withFileTypes: true }); + const directories = items.filter(dirent => dirent.isDirectory()); + // Map the directories to your desired structure + const structure = directories.map(dir => ({ + label: dir.name.charAt(0).toUpperCase() + dir.name.slice(1), + link: `/${dir.name}`, + autogenerate: { + directory: dir.name + } + })); + + let homeIndex = -1; + const homeStructure = structure.map((entry, index) => { + if (entry.link === "/home") { + homeIndex = index; // Capture the index of the modified entry + return { + label: entry.label, + link: "/" + }; + } + return entry; + }); + + // If "/home" was modified, move it to the beginning of the array + if (homeIndex !== -1) { + const [homeEntry] = homeStructure.splice(homeIndex, 1); // Remove the entry + homeStructure.unshift(homeEntry); // Unshift to the beginning + } + + const icons_file = join(content_path,"icons.yaml") + if(await exists(icons_file)){ + const icons_list = await load_yaml_abs(icons_file) + homeStructure.push(...icons_list) + } + return homeStructure; +} + async function create_menu(collect_config){ - let raw_menu = await load_yaml(collect_config.raw_menu) - for(const menu_entry of raw_menu){ + const menu_file = join(collect_config.contentdir,"menu.yaml") + let raw_menu + if(await exists(menu_file)){ + raw_menu = await load_yaml_abs(menu_file) + }else{ + raw_menu = await create_raw_menu(join(collect_config.contentdir)) + } + const base_menu = JSON.parse(JSON.stringify(raw_menu)) + for(let menu_entry of base_menu){ menu_entry.link = add_base(menu_entry.link) } let menu = { - raw_menu:raw_menu, + base_menu:base_menu, sections:{} } - for(const menu_entry of raw_menu){ + for(const menu_entry of base_menu){ const section = section_from_pathname(menu_entry.link); - menu.sections[section] = await get_section_menu(section,raw_menu) + menu.sections[section] = await get_section_menu(section,base_menu) } const menu_text = JSON.stringify(menu) @@ -81,7 +128,7 @@ async function create_menu(collect_config){ menu = {hash:hash,...menu} await save_json(menu,collect_config.out_menu) - console.log(`create_menu> ${collect_config.raw_menu} -> saved new menu in '${collect_config.out_menu}' `) + console.log(`create_menu> content -> saved new menu in '${collect_config.out_menu}' `) } export{ diff --git a/menu.yaml b/menu.yaml deleted file mode 100644 index 189b98d..0000000 --- a/menu.yaml +++ /dev/null @@ -1,29 +0,0 @@ -- label: Home - link: / -- label: Microcontrollers - link: /microcontrollers - autogenerate: - directory: microcontrollers -- label: Frameworks - link: /frameworks/raspi-iot - autogenerate: - directory: frameworks -- label: Protocols - link: /protocols/thread - autogenerate: - directory: protocols -- label: 3D Printing - link: /3dprinting - autogenerate: - directory: 3dprinting -- label: Web - link: /web - autogenerate: - directory: web -- label: Other - link: /other/sound-processing - autogenerate: - directory: other -- icon: github - link: https://github.com/HomeSmartMesh/website-astro - align: right diff --git a/package.json b/package.json index 7f3705f..0f3dd41 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "@google/model-viewer": "^3.4.0", "@svgdotjs/svg.js": "^3.1.2", "astro": "^4.5.10", - "content-structure": "^1.0.7", + "content-structure": "^1.1.1", "cookie-parser": "^1.4.6", "cors": "^2.8.5", "datatables.net-dt": "^1.13.7", diff --git a/src/layout/AppBar.astro b/src/layout/AppBar.astro index e39ca80..1cd993d 100644 --- a/src/layout/AppBar.astro +++ b/src/layout/AppBar.astro @@ -1,15 +1,10 @@ --- import './colors.css'; import Icon from "@/components/icon.astro" -import { load_yaml } from '@/libs/utils'; -import { get_active_appbar_menu } from './layout_utils.js'; -import { add_base } from '@/libs/assets.js'; +import { get_base_menu,get_active_appbar_menu } from './layout_utils.js'; -let raw_menu = await load_yaml("menu.yaml") -for(const menu_entry of raw_menu){ - menu_entry.link = add_base(menu_entry.link) - } -const menu = get_active_appbar_menu(raw_menu,Astro.url.pathname) +let base_menu = get_base_menu() +const menu = get_active_appbar_menu(base_menu,Astro.url.pathname) const left_side = menu.filter((item)=>(!Object.hasOwn(item,"align") || !(item.align=="right"))) const right_side = menu.filter((item)=>(Object.hasOwn(item,"align") && (item.align=="right"))) diff --git a/src/layout/layout_utils.js b/src/layout/layout_utils.js index bf6d31d..30ee06f 100644 --- a/src/layout/layout_utils.js +++ b/src/layout/layout_utils.js @@ -101,9 +101,14 @@ function get_menu_hash(){ return generated_menu.hash } +function get_base_menu(){ + return generated_menu.base_menu +} + export{ process_toc_list, get_generated_section_menu, get_menu_hash, + get_base_menu, get_active_appbar_menu } diff --git a/src/libs/utils.js b/src/libs/utils.js index 44f1dd5..f53bf1b 100644 --- a/src/libs/utils.js +++ b/src/libs/utils.js @@ -43,6 +43,12 @@ async function save_json(data,file_path){ await writeFile(file_abs,JSON.stringify(data,undefined, 2)) } +async function load_yaml_abs(abs_path){ + const fileContent = await readFile(abs_path,'utf-8') + const data = yaml.load(fileContent); + return data; +} + async function load_yaml(rel_path){ const path = join(config.rootdir,rel_path) const fileContent = await readFile(path,'utf-8') @@ -67,6 +73,7 @@ export{ load_json, load_json_abs, load_yaml, + load_yaml_abs, save_json, save_file, get_dir_files diff --git a/src/pages/assets/[...path].js b/src/pages/assets/[...path].js index 801160a..db22eb0 100644 --- a/src/pages/assets/[...path].js +++ b/src/pages/assets/[...path].js @@ -1,7 +1,7 @@ import { createReadStream } from 'fs'; import {resolve,join} from 'path' import { config } from "@/config"; -import {load_json} from '@/libs/utils.js' +import {load_json_abs} from '@/libs/utils.js' import {file_mime} from '@/libs/assets.js' import {remove_base} from '@/libs/assets.js' @@ -32,7 +32,7 @@ export async function getStaticPaths(){ return [] } - const asset_list = await load_json(join(config.collect_content.rel_outdir,'asset_list.json')) + const asset_list = await load_json_abs(join(config.collect_content.outdir,'asset_list.json')) const paths = asset_list.filter((asset)=>(Object.hasOwn(asset,"path"))).map((entry)=>(entry.path)) console.log(`serving API endpoit ${paths.length} assets`) return paths.map((path)=>({params:{path:path}})) diff --git a/src/pages/codes/[...path].js b/src/pages/codes/[...path].js index 21617e5..dc53921 100644 --- a/src/pages/codes/[...path].js +++ b/src/pages/codes/[...path].js @@ -1,7 +1,7 @@ import { createReadStream } from 'fs'; import {resolve,join} from 'path' import { config } from "@/config"; -import {load_json} from '@/libs/utils.js' +import {load_json_abs} from '@/libs/utils.js' import {file_mime} from '@/libs/assets.js' export async function GET({params}){ @@ -21,7 +21,7 @@ export async function GET({params}){ export async function getStaticPaths(){ if(import.meta.env.DEV){ - const asset_list = await load_json(join(config.collect_content.rel_outdir,'asset_list.json')) + const asset_list = await load_json_abs(join(config.collect_content.outdir,'asset_list.json')) const codes_diagrams = ["plantuml", "blockdiag", "mermaid"] const diagrams = asset_list.filter((asset)=> ((asset.type == "code") && (codes_diagrams.includes(asset.language)))