Let's try to approach this step-by-step. You already found a very relevant example, meaning you roughly understand how to use lua filters and know, that filter functions act on the elements of the same name. You also know that you'll want to modify links so your filter will look something like this:
function Link (element)
-- fix link targets
return element
end
We don't really know yet how a link element
looks like, so we check the docs by searching the page for link. After some searching, we find the description for the Link
constructor. Somewhere in the docs it's stated that pandoc also uses this to create Link
elements, so understanding this will answer most of our questions.
The properties of an element can be accessed using the name of the parameter in the constructor. To get the link target, we just need to write element.target
.
function Link (element)
element.target = fix_path(element.target)
return element
end
So let's start by thinking about what needs to be done to fix paths. Obviously, we just need to concatenate two strings, viz. the link path and a path prefix.
function fix_path (path)
return 'path/prefix/' .. path
end
So we are basically done. But this only fixes links. However, there are typically other paths in a document. Most notably paths pointing to images. Quickly searching the docs for image reveals that the property holding an image path is named src
. We can use this to also filter on images, so the complete filter looks like this:
function fix_path (path)
return 'you/path/prefix' .. path
end
function Link (element)
element.target = fix_path(element.target)
return element
end
function Image (element)
element.src = fix_path(element.src)
return element
end
Voilà, your filter.