spiegel-keyman/resources/build/htm-link.lua
Marc Durdin d45bc712d1 fix(windows): open chm external links in user's browser
Transform external links to add target=_blank, and add a title and ↗️
icon to make it clear the link opens in an external browser.

Fixes: #16507
Test-bot: skip
2026-09-03 07:08:53 +02:00

40 lines
1.3 KiB
Lua

local redirect
function Link(elem)
if isLocalLink(elem) then
if isFolderLink(elem) then
return pandoc.Link(elem.content, elem.target .. 'index.htm', elem.title, elem.attr)
else
return pandoc.Link(elem.content, elem.target .. '.htm', elem.title, elem.attr)
end
end
-- target=_blank opens external links in browser
table.insert(elem.content, ' ↗️')
return pandoc.Link(elem.content, elem.target, 'This link opens in your web browser', { target = '_blank' })
end
function isLocalLink(elem)
return string.match(elem.target, "://") == nil
end
function isFolderLink(elem)
return not ( string.match(elem.target, "/$") == nil )
end
-- Support for redirect meta key, adds a meta refresh to the resulting document
function Meta(meta)
if meta.redirect then
redirect = pandoc.utils.stringify(meta.redirect)
end
return meta
end
function Pandoc(doc)
if redirect then
local metaRefresh = pandoc.RawBlock('html', "<meta http-equiv='refresh' content='0;url=" .. redirect .. ".htm'>")
local redirectText = pandoc.Para({pandoc.Str("This content can be found "), pandoc.Link("here", redirect .. ".htm"), pandoc.Str(".")})
table.insert(doc.blocks, metaRefresh)
table.insert(doc.blocks, redirectText)
end
return pandoc.Pandoc(doc.blocks, doc.meta)
end