I’ve been trying to modularize my Franklin-powered website’s code by using Julia to build pieces of JavaScript code in the website that are fairly repetitive. To achieve this, I used this Julia code in my utils.jl file:
function render_js(pageTitle::String, type, vars)
title = replace(pageTitle, " " => "_")
if (type=="attractor")
artTitle = pageTitle
artTitle = replace(artTitle, r" solver"=>"")
if (occursin(r"Chen", artTitle))
title = "Chen system"
view = "[0, 2, 0]";
elseif (occursin(r"Lorenz", artTitle))
title = "Lorenz system"
view = "[2, 0, 0]";
elseif (occursin(r"Rabinovich–Fabrikant", artTitle))
title = "Rabinovich–Fabrikant system"
view = "[2, 0, 0]";
elseif (occursin(r"Rössler", artTitle))
title = "Rössler system"
else
title = artTitle;
end
if (@isdefined(view))
viewStr1 = ", {view: $view}"
viewStr2 = "view: $view, "
else
viewStr1 = ""
viewStr2 = ""
end
targetFile = "_libs/rendered/attractor_$title.js"
baseFile = "_libs/common/attractor.js"
elseif (vars !== nothing)
varsList = vars
conds = ""
for (i, name) in enumerate(varsList)
conds *= name * "0"
if i != length(varsList)
conds *= ", "
end
end
targetFile = "_libs/rendered/RKF45_$title.js"
baseFile = "_libs/common/RKF45_base.js"
else
return;
end
if !isdir("_libs/rendered")
mkpath("_libs/rendered/")
end
if !isdir("__site/libs/rendered/")
mkpath("__site/libs/rendered/")
end
if !isfile(targetFile) || stat(baseFile).mtime > stat(targetFile).mtime
cp(baseFile, targetFile, force=true)
content = read(targetFile, String)
# Replace characters or strings
if (@isdefined(conds))
content = replace(content, "\$conds" => "$conds")
end
if @isdefined(viewStr1)
content = replace(content, "\$viewStr1" => "$viewStr1")
content = replace(content, "\$viewStr2" => "$viewStr2")
end
content = replace(content, "\$title" => "$title")
# Write the modified content back to the file
write(targetFile, content)
end
end
function hfun_render_js()
title = locvar("title")
type = locvar("type")
vars = locvar("vars")
render_js(title, type, vars)
title = replace(title, " " => "_")
if (type == "attractor")
HTML = """<script src="/libs/rendered/attractor_$title.js"></script>"""
else
HTML = """<script src="/libs/rendered/RKF45_$title.js"></script>"""
end
return HTML
end
This is meant to create JS scripts within _libs/rendered/ based on a template. This works as expected when Franklin is deployed locally, but not when it’s deployed to GitHub Pages. In fact, on GitHub Pages if I don’t commit my local copies of these _libs/rendered scripts, the pages that depend on them won’t be included in my website (here’s the GitHub actions log of this happening). I have also tried solving this problem by adding a deploy.jl file to my website and calling it in GitHub actions with the contents:
include("utils.jl")
function eager_generate_js()
md_dir = joinpath(@__DIR__, ".")
for (root, _, files) in walkdir(md_dir)
for file in files
if endswith(file, ".md")
md_path = joinpath(root, file)
content = read(md_path, String)
title = match(r"title *= *\"([^\"]+)\"", content)
type = match(r"type *= *\"([^\"]+)\"", content)
vars = match(r"vars *= *\[([^\]]+)\]", content)
if isnothing(type) || isnothing(vars)
continue
end
# extract values
page_title = isnothing(title) ? "Untitled" : title.captures[1]
page_type = type.captures[1]
page_vars = split(vars.captures[1], r"\s*,\s*")
try
render_js(page_title, page_type, page_vars)
catch e
@warn "Error generating JS for $file" exception = e
end
end
end
end
end
eager_generate_js()
optimize()
And even though it is called by my .github/actions/deploy.yml file, this does not seem to fix the problem. I have also tried having hfun_render_js()
insert JS code within <script>
tags (instead of writing it to a JS script file and including that in the HTML with <script src=...></script>
). But the JS functions defined within those tags were not callable within the page nor within functions defined within JS script files called via <script src=...></script>
. I guess I could prefix every function with window.
to fix that, but that’s a tedious solution I’d rather avoid.