Lua API

Everything lives in the global table merodi. Paths are relative to the project root unless noted.

Overview

Name Purpose
merodi.action() The running command
merodi.hook(name, fn) Register a hook
merodi.abort() Skip the current page
merodi.build.* Mode, content and paths of the current page
merodi.jinja.* Variables for templates
merodi.config.* Values from config.toml
merodi.log.* Print messages
merodi.compile.* Convert Markdown from Lua
merodi.watch.add(path) Watch another path
merodi.enable.*, merodi.disable.* Markdown features and attribute filters

merodi.action

merodi.action()   -- "build", "watch" or "hook"

Returns the command that is running. Plugins are not loaded for init and clean, so those never show up.

merodi.hook

merodi.hook(name, function(...) end)

Registers fn for the hook name. One function per name: a second call with the same name replaces the first. Hook names and arguments are listed in Build and hooks.

merodi.abort

merodi.abort()

Stops the current page. Nothing is written for it, the on_abort hook runs, and the build continues with the next file. Use it inside a hook only.

merodi.build

State of the page being built. These work inside hooks.

Function Returns / does
merodi.build.mode() "release" for --release, "Draft" otherwise
merodi.build.content.get() The current content, see the stage table in Build and hooks
merodi.build.content.set(text) Replace the content
merodi.build.md_path.get() Absolute path of the source .md file
merodi.build.md_path.set(path) Change the source path
merodi.build.document_path.get() Absolute path of the output file
merodi.build.document_path.set(path) Change where the output is written

mode() returns "Draft" with a capital D. Compare against "release" and treat everything else as draft.

merodi.jinja

Variables for templates.

merodi.jinja.set("key", value)
merodi.jinja.get("key")

set takes a string, number, boolean or table. A Lua list becomes a Jinja list, any other table a mapping. Use it as {{ key }} in pages.

get returns strings, numbers and booleans. It returns nil for a missing key and for table values.

merodi.config

Read-only values from config.toml.

Name Value
merodi.config.project.name Project name
merodi.config.project.version Project version
merodi.config.project.description Project description
merodi.config.tree.markdown Markdown source folder
merodi.config.tree.templates Templates folder
merodi.config.tree.plugins Plugins folder
merodi.config.tree.draft_dest Draft output folder
merodi.config.tree.release_dest Release output folder

merodi.log

Function Output
merodi.log.info(text) [INFO]: text
merodi.log.warn(text) [WARN]: text, the build goes on
merodi.log.die(text) [ERROR]: text, then Merodi exits with status 1 at once

merodi.compile

Convert Markdown without writing a file. The extensions you enabled apply.

merodi.compile.convert("# Hello\nSome **markdown**")
merodi.compile.mdtohtml("# Hello\nSome **markdown**")

Neither wraps the result in a full document. Trailing newlines are removed.

merodi.watch

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

Adds a path for merodi watch. Without any add call, watch uses the markdown folder. See Watching.

merodi.enable

Turns on a Markdown feature. Call the name as a function: merodi.enable.GFM(). An unknown name is an error. See Writing pages for what each does.

Extensions:

GFM, Table, Strikethrough, Linkify, TaskList, DefinitionList, Footnote, Typographer, CJK, HTMLAttr

Parser options:

AutoHeadingID, EscapedSpace

Core parsers, already active by default:

SetextHeading, ThematicBreak, List, ListItem, CodeBlock, ATXHeading, FencedCodeBlock, Blockquote, HTMLBlock, Paragraph, CodeSpan, Link, AutoLink, RawHTML, Emphasis

merodi.disable

Switches off the attribute filter of an element, so any attribute written with HTMLAttr is kept for it. Call the name as a function: merodi.disable.GlobalAttribute().

Name Element
GlobalAttribute all elements
HeadingAttribute headings
BlockquoteAttribute block quotes
CodeAttribute code
EmphasisAttribute emphasis
ImageAttribute images
LinkAttribute links
ListAttribute lists
ListItemAttribute list items
ParagraphAttribute paragraphs
ThematicAttribute thematic breaks (---)