Import Markdown into a Moodle Book, one chapter per heading

Paste Markdown from ChatGPT, Notion, Obsidian or GitHub into a Moodle Book and every heading becomes a chapter. Our free booktool_markdown plugin does it.

Drawing of a pair of scissors cutting through the pages of an open book with a blue cover.

Short answer: Moodle’s Book can import chapters from a zip of HTML files, and nothing else (Book settings docs). Course notes today are mostly Markdown: answers from ChatGPT or Claude, Notion and Obsidian pages, GitHub READMEs. Our free plugin, booktool_markdown, adds Import from Markdown to the book’s More menu. Paste the text or upload a .md file, and each heading becomes a chapter.

This is the twenty-seventh plugin in our series: one plugin for each Moodle plugin type, each one teaching DevLMS Studio, our AI plugin builder, how that type works. The plugins directory has Book imports for Word, EPUB and PowerPoint, but none for Markdown.

How the text is split

  1. Open a Book as a teacher. In the More menu choose Import from Markdown.
  2. Paste the Markdown or choose a file and press Import. The new chapters go at the end of the book, and you land on the first one.
  • The highest heading level in the text makes the chapters. AI answers often start at ##, so if there is no #, the ## headings become the chapters.
  • The next level down makes subchapters. You can turn that off and keep them inside their chapter.
  • Deeper headings, lists, tables, links, images with web addresses and code blocks stay in the chapter text. Code blocks keep their language, so Moodle’s code highlighter colours them.
  • Text before the first heading becomes a chapter called “Introduction”.
  • A # inside a code block (a shell comment, say) does not start a chapter. Front matter at the top of Obsidian and static-site files is dropped.

It supports Moodle 4.5 LTS to 5.3. The code is on GitHub.

How a Book tool works

From Moodle 5.3’s Book code:

  1. A Book tool is a small plugin inside the Book activity. It adds its page to the book’s More menu through one function in lib.php. Core ships three: HTML import, IMS content package export, and print.
  2. Chapters are rows in one table, ordered by page number, each marked as a chapter or a subchapter. A tool that adds chapters must renumber the book afterwards, make sure it does not start with a subchapter, and raise the book’s revision so the next page load is fresh.
  3. The detail that matters most: Book prints chapter text without Moodle’s usual HTML cleaning. Whatever an import tool saves goes straight to students’ screens. Markdown allows raw HTML, so our plugin cleans the HTML (scripts, event handlers, javascript: links) before it saves anything. We tested the test: with the cleaning taken out, it fails.

Core’s own HTML import has an option for “one HTML file with headings as chapters”. The option is commented out of the form and its code is a TODO, so core cannot split one document into chapters by its headings.

The browser test runs without JavaScript: a teacher pastes three headings, presses Import, and sees three chapters in the table of contents. A student does not see the menu item. Unit tests cover heading levels, code blocks, front matter, titles with formatting, Windows line endings, events and the page order.

Need a plugin like this for your own Moodle?

Describe it in DevLMS Studio and watch it built and tested in a real Moodle, or ask our team for a fixed price.

Open DevLMS Studio