feat: clickable headers

This commit is contained in:
LordMZTE 2024-08-23 17:22:03 +02:00
parent 14313bab21
commit a3256a256a
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
5 changed files with 66 additions and 13 deletions

View file

@ -1,5 +1,10 @@
.PHONY: clean
build: confgen.lua src meta resources hx/out/index.js hx/out/article.js
confgen confgen.lua build
hx/out/%.js: hx/%.hxml hx/src hx/common.hxml
cd $(dir $<) && haxe $(notdir $<) && cd .. && mv $(basename $@).min.js $@
clean:
rm -rf build hx/out

View file

@ -10,7 +10,7 @@ cg.doTemplateFile "lib.cgt"
cg.opt.doctype = "<!DOCTYPE html>"
cg.opt.renderMarkdown = function(src)
cg.opt.renderMarkdown = function(src, no_header_links)
local doc = cmark.parse_document(src, string.len(src), cmark.OPT_UNSAFE)
for cur, entering, node_type in cmark.walk(doc) do
@ -19,10 +19,12 @@ cg.opt.renderMarkdown = function(src)
-- Syntax highlighting with Bat and AHA
local lang = cmark.node_get_fence_info(cur)
local tmpfp = os.tmpname()
local proc = #lang > 0 and io.popen(
"bat --color always --style plain --language " .. lang .. " | aha --no-header >" .. tmpfp,
"w"
) or io.popen("aha --no-header >" .. tmpfp, "w")
local proc = #lang > 0
and io.popen(
"bat --color always --style plain --language " .. lang .. " | aha --no-header >" .. tmpfp,
"w"
)
or io.popen("aha --no-header >" .. tmpfp, "w")
proc:write(cmark.node_get_literal(cur))
proc:close()
@ -59,6 +61,46 @@ cg.opt.renderMarkdown = function(src)
cmark.node_insert_before(cur, close)
-- Remove original
cmark.node_unlink(cur)
elseif not no_header_links and node_type == cmark.NODE_HEADING then
local level = cmark.node_get_heading_level(cur)
local id = ""
for n, e, _ in cmark.walk(cur) do
if e then
id = id .. (cmark.node_get_literal(n) or "")
end
end
id = id:lower():gsub(" ", "-"):gsub("['!.?#@,%(%)%[%]{}%%]+", "")
local pref = cmark.node_new(cmark.NODE_HTML_BLOCK)
cmark.node_set_literal(
pref,
string.format([[<a href="#%s" class="headerlink"><h%d id="%s">]], id, level, id)
)
cmark.node_insert_before(cur, pref)
local text = cmark.node_new(cmark.NODE_CUSTOM_BLOCK)
-- Content of header node
-- Spins in one loop for some reason
local children = {}
local child = cmark.node_first_child(cur)
while child do
table.insert(children, child)
child = cmark.node_next(child)
end
for _, c in ipairs(children) do
cmark.node_append_child(text, c)
end
cmark.node_insert_before(cur, text)
local suff = cmark.node_new(cmark.NODE_HTML_BLOCK)
cmark.node_set_literal(suff, string.format([[</h%d></a>]], level))
cmark.node_insert_before(cur, suff)
cmark.node_unlink(cur)
end
end

View file

@ -6,11 +6,11 @@
"utils": "utils"
},
"locked": {
"lastModified": 1723800090,
"narHash": "sha256-jeECBke8Z7ghi/qsCP8mcrrTheAHKVaaeMGxhpilRDg=",
"lastModified": 1724425982,
"narHash": "sha256-RO0pl2NXVSg1U7DJ3TPF3sVrj0B709JwUlDS2x7NQDU=",
"ref": "refs/heads/master",
"rev": "5dd5861030a2d629ea52debfe97d66d01995e0c1",
"revCount": 102,
"rev": "f53be66e1a6ceefdfba79db1890be6e1050e3056",
"revCount": 105,
"type": "git",
"url": "https://git.mzte.de/LordMZTE/confgen.git"
},
@ -37,11 +37,11 @@
},
"nixpkgs_2": {
"locked": {
"lastModified": 1723637854,
"narHash": "sha256-med8+5DSWa2UnOqtdICndjDAEjxr5D7zaIiK4pn0Q7c=",
"lastModified": 1724224976,
"narHash": "sha256-Z/ELQhrSd7bMzTO8r7NZgi9g5emh+aRKoCdaAv5fiO0=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "c3aa7b8938b17aebd2deecf7be0636000d62a2b9",
"rev": "c374d94f1536013ca8e92341b540eba4c22f9c62",
"type": "github"
},
"original": {

View file

@ -63,3 +63,8 @@ blockquote {
blockquote p {
padding: 4px;
}
.headerlink {
color: inherit;
text-decoration: none;
}

View file

@ -32,7 +32,8 @@
</div>
<div id="content" width="100%">
<! tmpl:pushSubtmpl(function(tmpl) tmpl:setPostProcessor(opt.renderMarkdown) !>
<! tmpl:pushSubtmpl(function(tmpl)
tmpl:setPostProcessor(function(c) return opt.renderMarkdown(c, true) end) !>
# Hey! Welcome to my website!
I'm LordMZTE, a programmer from Thuringia, Germany. I like to code in Zig, sometimes Rust and rarely
also some Minecraft mods. If you're looking for my projects, you should visit [my git server](/git).