Build and hooks

A hook is a Lua function that Merodi calls at a fixed point of the build. Register one with merodi.hook(name, function).

The pipeline

For each Markdown file, in this order:

# What happens Hook runs afterwards merodi.build.content holds
1 Source and output paths are worked out before_read nothing yet
2 The Markdown file is read read_md the raw Markdown
3 Jinja is applied apply_doc_jinja Markdown, after Jinja ran
4 Markdown is converted to HTML doc_content the HTML (a full document if {% document %} was used)
5 The output is ready before_write the same HTML, see the note below
6 The file is written none

Files that are not Markdown are copied with no hooks.

Changing a page

merodi.build.content.set(text) replaces the content, and the next step works on your text. This works in read_md, apply_doc_jinja and doc_content.

merodi.hook("doc_content", function()
	local html = merodi.build.content.get()
	merodi.build.content.set((html:gsub("</body>", "<!-- end -->\n</body>", 1)))
end)

Use doc_content, not before_write, to change the final HTML. By the time before_write runs, the text to write has already been taken, so content.set has no effect there. before_write is still a good place to change the output path or to abort.

Setting content in before_read has no effect either, because the file is read right after.

Paths

merodi.build.md_path.get() is the absolute path of the source file. merodi.build.document_path.get() is the absolute path of the output file. Change the output with merodi.build.document_path.set(path) in before_read, read_md, apply_doc_jinja, doc_content or before_write. Missing folders are created.

Skipping a page

merodi.abort() stops the current page. Nothing is written for it, on_abort runs, and the build goes on with the next file.

merodi.hook("before_read", function()
	if merodi.build.md_path.get():find("/drafts/", 1, true) then
		merodi.abort()
	end
end)

merodi.hook("on_abort", function()
	merodi.log.info("page skipped")
end)

Call merodi.abort only inside a hook that belongs to a page.

All hooks

Hook When Arguments
before_read Before a Markdown file is read none
read_md After the Markdown was read none
apply_doc_jinja After Jinja ran none
doc_content After Markdown became HTML none
before_write Before the output file is written none
on_abort After merodi.abort() skipped a page none
on_start_watching When merodi watch starts none
on_file_changed When a watched file changes mode, path
any other name When you run merodi hook <name> ... the extra words

Only one function can be registered per name. Registering a name again replaces the earlier function.

Custom commands

A hook with your own name becomes a command:

merodi.hook("deploy", function(target)
	os.execute("rsync -av build/release/ " .. target)
end)
merodi hook deploy me@example.com:/var/www/site

Each word after the hook name is passed to the function as a string. A name with no registered function does nothing.

Watching

merodi watch

merodi watch watches the markdown folder, or the folders you add with merodi.watch.add(path) in main.lua. When something changes it calls on_file_changed(mode, path). mode is the event: CREATE, WRITE, REMOVE, RENAME or CHMOD. Repeated events for the same file within 100 ms are ignored.

Watching a folder does not include its subfolders. Add each folder you care about.

Merodi does not rebuild by itself on a change. Do that in the hook, for example by running merodi build:

merodi.watch.add("src/md")
merodi.watch.add("src/templates")

merodi.hook("on_file_changed", function(mode, path)
	if mode:find("WRITE") then
		os.execute("merodi build")
	end
end)