Introduction
In this file we will see how to create an filter extention for quarto using Lua.
A filter extention is a flexible and powerful tool for introducing new global behaviors and/or new markdown rendering behaviors.
For example, you could create filters to implement output folding, an image carousel, or just about anything you can imagine!
Creation
To create a extention we need to have an "_extensions" folder
and in this folder we need to create a folder for each extention.
For each extention we need to have a file name "_extension.yml" to tell quarto the structure of this extention.
Here is an exemple:
title: Leandre Name Filter
author: Léandre
version: 1.0.0
quarto-required: ">=1.3.0"
contributes:
filters:
- leandre.lua
In this file we can see that it include a file in .lua and that where the logic of the extention will be coded.
So in resume we have this file structure
_extensions/
└── leandre-filter
├── _extension.yml
└── leandre.lua
Code Exemple
For this exemple we will talk about the file leandre.lua
function Str(el)
el.text = el.text:gsub("[Ll][Ee][Aa][Nn][Dd][Rr][Ee]", "Léandre")
return el
end
What do this small code do is using the function gsub for each part of the document.
The gsub function search for a patern and replace it with the given string.
The patern given here "[Ll][Ee][Aa][Nn][Dd][Rr][Ee]" to permit to have evry possibility covered of the word léandre.
More resources
In these links you can find more information about the diferent function that can be use in the extention.
https://quarto.org/docs/extensions/lua.html
https://quarto.org/docs/extensions/lua-api.html
More about filter
https://quarto.org/docs/extensions/filters.html
And other type of extension
https://quarto.org/docs/extensions/creating.html#development