feat: links to new tabs

This commit is contained in:
LordMZTE 2024-08-18 15:09:37 +02:00
parent c0ed06f7c1
commit e24830d4de
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
4 changed files with 55 additions and 21 deletions

View file

@ -12,23 +12,57 @@ cg.opt.renderMarkdown = function(src)
local doc = cmark.parse_document(src, string.len(src), cmark.OPT_UNSAFE)
for cur, entering, node_type in cmark.walk(doc) do
if node_type == cmark.NODE_CODE_BLOCK then
local lang = cmark.node_get_fence_info(cur)
if #lang > 0 then
local tmpfp = os.tmpname()
local proc = io.popen(
"bat --color always --style plain --language " .. lang .. " | aha --no-header >" .. tmpfp,
"w"
)
proc:write(cmark.node_get_literal(cur))
proc:close()
if entering then
if node_type == cmark.NODE_CODE_BLOCK then
-- Syntax highlighting with Bat and AHA
local lang = cmark.node_get_fence_info(cur)
if #lang > 0 then
local tmpfp = os.tmpname()
local proc = io.popen(
"bat --color always --style plain --language " .. lang .. " | aha --no-header >" .. tmpfp,
"w"
)
proc:write(cmark.node_get_literal(cur))
proc:close()
local tmpf = io.open(tmpfp, "r")
local new = cmark.node_new(cmark.NODE_HTML_BLOCK)
cmark.node_set_literal(new, [[<pre class="codeblock"><code>]] .. tmpf:read "*a" .. [[</code></pre>]])
cmark.node_replace(cur, new)
tmpf:close()
os.remove(tmpfp)
local tmpf = io.open(tmpfp, "r")
local new = cmark.node_new(cmark.NODE_HTML_BLOCK)
cmark.node_set_literal(
new,
[[<pre class="codeblock"><code>]] .. tmpf:read "*a" .. [[</code></pre>]]
)
cmark.node_replace(cur, new)
tmpf:close()
os.remove(tmpfp)
end
elseif node_type == cmark.NODE_LINK then
-- This basically reimplements links in order to make them open in a new tab.
-- Opening A
local start = cmark.node_new(cmark.NODE_HTML_INLINE)
cmark.node_set_literal(start, [[<a target="_blank" href="]] .. cmark.node_get_url(cur) .. [[">]])
cmark.node_insert_before(cur, start)
-- Content of link 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_insert_before(cur, c)
end
-- Closing A
local close = cmark.node_new(cmark.NODE_HTML_INLINE)
cmark.node_set_literal(close, [[</a>]])
cmark.node_insert_before(cur, close)
-- Remove original
cmark.node_unlink(cur)
end
end
end

View file

@ -11,7 +11,7 @@
<body>
<div id="topright">
<p><% meta.date %></p>
<p><a href="https://git.mzte.de/LordMZTE/homepage/src/branch/master/src/a/<% meta.id %>/index.html.cgt">[src]</a></p>
<p><a href="https://git.mzte.de/LordMZTE/homepage/src/branch/master/src/a/<% meta.id %>/index.html.cgt" target="_blank">[src]</a></p>
</div>
<h1><% meta.title %></h1>
<p><% meta.summary %></p>

View file

@ -75,8 +75,9 @@ local doc = cmark.parse_document(src, string.len(src), cmark.OPT_UNSAFE)
-- Iterate over all nodes
for cur, entering, node_type in cmark.walk(doc) do
-- If we found a code block...
if node_type == cmark.NODE_CODE_BLOCK then
if not entering then
-- If we found a code block and are entering the node...
if entering and node_type == cmark.NODE_CODE_BLOCK then
-- Get its language
local lang = cmark.node_get_fence_info(cur)

View file

@ -1,5 +1,4 @@
<! local val = {
tags = {},
articles = opt.articles,
} !>
<% cg.fmt.json.serialize(val) %>
} !><% cg.fmt.json.serialize(val) %>