Plugins

Plugins are Lua scripts. They run inside Merodi with a global table named merodi. A plugin can enable Markdown extensions, give templates variables, run code while pages build, and define your own commands.

The language is Lua 5.1, with the standard libraries (string, table, os, io, math).

main.lua

The file main.lua in the plugins folder is the only file Merodi loads. It runs at the start of build, watch and hook. It does not run for init or clean.

A small main.lua:

-- Markdown features
merodi.enable.GFM()
merodi.enable.AutoHeadingID()

-- A variable for templates
merodi.jinja.set("site_name", "My Site")

-- Code that runs while building
merodi.hook("before_read", function()
	merodi.log.info("reading " .. merodi.build.md_path.get())
end)

The code at the top level of main.lua runs once. Functions you pass to merodi.hook run later, at the stage you name. See Build and hooks.

What a plugin can do

Goal Use
Turn on Markdown features merodi.enable.*, merodi.disable.*
Pass data to templates merodi.jinja.set
Read project settings merodi.config
Run code at a build step merodi.hook
Change the text of a page merodi.build.content
Skip a page merodi.abort
Change where a page is written merodi.build.document_path
Convert Markdown to HTML in Lua merodi.compile
Watch more folders merodi.watch.add
Print messages merodi.log

All of these are described in the Lua API.

Splitting a plugin into files

require searches from the project root, not from the plugins folder. Add the plugins folder to package.path first:

package.path = "src/plugins/?.lua;" .. package.path

require("markdown")   -- loads src/plugins/markdown.lua
require("nav")        -- loads src/plugins/nav.lua

Errors

If a plugin has a syntax or runtime error, Merodi stops and shows the file, the line and a few lines around it:

[ERROR]: src/plugins/main.lua:1: unknown parser or extension: Nope

Calling merodi.enable or merodi.disable with a name that does not exist is an error too.

Keep plugins quick

main.lua is loaded on every command, and hooks run once per page. Do heavy work only when you need it, for example behind merodi.action() or merodi.build.mode().