From 99008a241cf4c09d8dc6e9e270621f775eb95c0a Mon Sep 17 00:00:00 2001 From: wass Date: Mon, 18 Dec 2023 17:06:11 +0100 Subject: [PATCH] bugfixing copy_assets mode --- .gitignore | 2 ++ config.js | 9 +++++---- src/components/markdown/code/MermaidCli.astro | 4 ++-- src/components/markdown/code/diagram.js | 4 ++-- src/components/markdown/code/highlighter.js | 2 +- src/components/markdown/directive/Directive.astro | 9 ++++++++- src/libs/assets.js | 8 ++++---- src/pages/assets/[...path].js | 5 ++++- src/pages/codes/[...path].js | 2 +- 9 files changed, 29 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index ceb65a8..2cbd1d6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,11 @@ dist/ +public/_astro public/raw public/menu.json public/codes/ .structure/ + # dependencies node_modules/ diff --git a/config.js b/config.js index 5c13930..f57f103 100644 --- a/config.js +++ b/config.js @@ -15,13 +15,14 @@ const config = { rootdir: rootdir, outDir: outdir, content: "content", - code_out: join(content_out,"codes"), + content_out: content_out, + code_dir: "codes", plantuml_server: "https://www.plantuml.com/plantuml/svg", kroki_server: "https://kroki.io", client_menu:true, - copy_assets:false, - copy_assets_path: join(content_out,"raw"), - assets_hash_dir:false, + copy_assets:true, + copy_assets_dir: "_astro", + assets_hash_dir:true, highlighter:{ theme:"dark-plus", langs:['javascript','js','python','yaml'] diff --git a/src/components/markdown/code/MermaidCli.astro b/src/components/markdown/code/MermaidCli.astro index b6137ca..c5fdfbd 100644 --- a/src/components/markdown/code/MermaidCli.astro +++ b/src/components/markdown/code/MermaidCli.astro @@ -15,9 +15,9 @@ export interface Props { const { code, params } = Astro.props as Props; async function generator(code){ - const temp_file = join(config.rootdir,config.code_out,"temp.mmd") + const temp_file = join(config.rootdir,config.content_out,config.code_dir,"temp.mmd") await fs.writeFile(temp_file,code) - const out_file = join(config.rootdir,config.code_out,"mmd.svg") + const out_file = join(config.rootdir,config.content_out,config.code_dir,"mmd.svg") await run(temp_file, out_file); const svg_text = await fs.readFile(out_file,'utf-8') await fs.unlink(temp_file) diff --git a/src/components/markdown/code/diagram.js b/src/components/markdown/code/diagram.js index f7ac401..3a609d4 100644 --- a/src/components/markdown/code/diagram.js +++ b/src/components/markdown/code/diagram.js @@ -4,7 +4,7 @@ import {join} from 'path' async function diagram_cache(code,generator){ const hash = shortMD5(code) - const file_path = join(config.rootdir,config.code_out,hash,"diagram.svg") + const file_path = join(config.rootdir,config.content_out,config.code_dir,hash,"diagram.svg") const file_exists = await exists(file_path) if(file_exists){ console.log(`* returning diagram from cache '${file_path}'`) @@ -13,7 +13,7 @@ async function diagram_cache(code,generator){ console.log(`* generating diagram as not in cache`) const svg_text = await generator(code) await save_file(file_path,svg_text) - const code_path = join(config.rootdir,config.code_out,hash,"code.txt") + const code_path = join(config.rootdir,config.content_out,config.code_dir,hash,"code.txt") await save_file(code_path,code) return hash } diff --git a/src/components/markdown/code/highlighter.js b/src/components/markdown/code/highlighter.js index bdfa514..1de4048 100644 --- a/src/components/markdown/code/highlighter.js +++ b/src/components/markdown/code/highlighter.js @@ -23,7 +23,7 @@ async function codeToHtml(code, highlighter_config){ const html = highlighter.codeToHtml(code, { lang: lang, theme:config.highlighter.theme }) const hash = shortMD5(code) - const file_path = join(config.rootdir,config.code_out,hash,"code.txt") + const file_path = join(config.rootdir,config.content_out,config.code_dir,hash,"code.txt") //persist for highlighter copy, for code not saved by a diag gen if(!await exists(file_path)){ await save_file(file_path,code) diff --git a/src/components/markdown/directive/Directive.astro b/src/components/markdown/directive/Directive.astro index cb10ab5..5f64054 100644 --- a/src/components/markdown/directive/Directive.astro +++ b/src/components/markdown/directive/Directive.astro @@ -1,5 +1,12 @@ --- -import ImageDirective from './ImageDirective.astro'; +//to use Optimized Images, do the following : +// - uncomment import line with OptimizedImageDirective.astro +// - set config.copy_assets = true +// - set config.copy_assets_path = join(content_out,"_astro") +// - set assets_hash_dir = true + +//import ImageDirective from './ImageDirective.astro'; +import ImageDirective from './OptimizedImageDirective.astro'; import ButtonDirective from './ButtonDirective.astro' export interface Props { diff --git a/src/libs/assets.js b/src/libs/assets.js index a5486a9..c34004e 100644 --- a/src/libs/assets.js +++ b/src/libs/assets.js @@ -47,14 +47,14 @@ async function relAssetToUrlCopy(relativepath,dirpath){ if(await exists(file_abs)){ if(config.assets_hash_dir){ const target_filename = hashed_filename(relativepath,join(dirpath,relativepath)) - const target_file_abs = join(config.rootdir,config.copy_assets_path,target_filename) + const target_file_abs = join(config.rootdir,config.content_out,config.copy_assets_dir,target_filename) await copy_if_newer(file_abs,target_file_abs) - const newurl = join("raw",target_filename) + const newurl = join(config.copy_assets_dir,target_filename) return "/"+newurl.replaceAll('\\','/') }else{ - const target_file_abs = join(config.rootdir,config.copy_assets_path,dirpath,relativepath) + const target_file_abs = join(config.rootdir,config.content_out,config.copy_assets_dir,dirpath,relativepath) await copy_if_newer(file_abs,target_file_abs) - const newurl = join("raw",dirpath,relativepath) + const newurl = join(config.copy_assets_dir,dirpath,relativepath) return "/"+newurl.replaceAll('\\','/') } }else{ diff --git a/src/pages/assets/[...path].js b/src/pages/assets/[...path].js index 670e5e0..475b9c0 100644 --- a/src/pages/assets/[...path].js +++ b/src/pages/assets/[...path].js @@ -5,7 +5,10 @@ import {load_json} from '@/libs/utils.js' import {file_mime} from '@/libs/assets.js' export async function GET({params}){ - const imagePath = resolve(join(config.rootdir,config.content,params.path)); + let imagePath = resolve(join(config.rootdir,config.content,params.path)); + if(import.meta.env.DEV && config.copy_assets){ + imagePath = resolve(join(config.rootdir,config.content_out,config.copy_assets_dir,params.path)); + } console.log(`assets> serving '${imagePath}'`) try { const stream = createReadStream(imagePath); diff --git a/src/pages/codes/[...path].js b/src/pages/codes/[...path].js index d5fa92e..d089ab5 100644 --- a/src/pages/codes/[...path].js +++ b/src/pages/codes/[...path].js @@ -5,7 +5,7 @@ import {load_json} from '@/libs/utils.js' import {file_mime} from '@/libs/assets.js' export async function GET({params}){ - const imagePath = resolve(join(config.rootdir,config.code_out,params.path)); + const imagePath = resolve(join(config.rootdir,config.content_out,config.code_dir,params.path)); console.log(`codes> serving '${imagePath}'`) try { const stream = createReadStream(imagePath);