29 lines
1.1 KiB
Lua
29 lines
1.1 KiB
Lua
-- Copyright (c) 2026 Marc Wäckerlin. SPDX-License-Identifier: MIT
|
|||
|
|
-- A link to a Markdown file of the same set points at the PDF of that file.
|
||
|
|
--
|
||
|
|
-- The documents reference each other, `[ARCHITECTURE.md](ARCHITECTURE.md)`, and
|
||
|
|
-- that is right in the repository, where the reader opens the Markdown. In the
|
||
|
|
-- PDF the same target opens nothing: the reader holds a PDF, the file beside it
|
||
|
|
-- is a PDF, and the link reaches for a source he does not have.
|
||
|
|
--
|
||
|
|
-- So a target that names a local Markdown file is rewritten to the PDF that the
|
||
|
|
-- same converter produces from it, with its anchor kept. An address with a
|
||
|
|
-- scheme (https:, mailto:) and an anchor inside the same document stay as they
|
||
|
|
-- are.
|
||
|
|
local function rewritten(target)
|
||
|
|
if target:match('^%a[%w+.-]*:') or target:match('^#') then
|
||
|
|
return nil
|
||
|
|
end
|
||
|
|
local file, anchor = target:match('^([^#]+)(#?.*)$')
|
||
|
|
if not file or not file:lower():match('%.md$') then
|
||
|
|
return nil
|
||
|
|
end
|
||
|
|
return file:sub(1, #file - 3) .. '.pdf' .. anchor
|
||
|
|
end
|
||
|
|
|
||
|
|
function Link(link)
|
||
|
|
local target = rewritten(link.target)
|
||
|
|
if not target then return nil end
|
||
|
|
link.target = target
|
||
|
|
return link
|
||
|
|
end
|