Skip to content

Commit

Permalink
bugfixing copy_assets mode
Browse files Browse the repository at this point in the history
  • Loading branch information
wassfila committed Dec 18, 2023
1 parent dc2b5e4 commit 99008a2
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
dist/
public/_astro
public/raw
public/menu.json
public/codes/
.structure/


# dependencies
node_modules/

Expand Down
9 changes: 5 additions & 4 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
4 changes: 2 additions & 2 deletions src/components/markdown/code/MermaidCli.astro
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/components/markdown/code/diagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}'`)
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/markdown/code/highlighter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion src/components/markdown/directive/Directive.astro
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions src/libs/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
5 changes: 4 additions & 1 deletion src/pages/assets/[...path].js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/pages/codes/[...path].js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 99008a2

Please sign in to comment.