XMarkdown.API provides the core compilation interface for converting XMarkdown source text into renderable Html. See xmarkdowndemo.netlify.app for a working example.
The simplest way to compile XMarkdown text is to use compileString, e.g.
compileString defaultCompilerParameters
"# Mathematics\n\nPythagoras sez that $a^2 + b^2 = c^2$"See below for more information on compiler parameters. The DemoTOC and DemoTOC+Sync+Sync examples require a two-step workflow:
(1) Compile source text into a CompilerOutput structure using the function compileOutput, (2) view the CompilerOutput structure using the function
viewBodyOnly for the body of the text viewTOC, for the table of contents, etc.
import XMarkdown.API
import XMarkdown.Types exposing (defaultCompilerParameters)
-- Configure compiler
params =
{ defaultCompilerParameters
| docWidth = 600
}
-- Compile source text
output =
XMarkdown.API.compileOutput params
"""
# Introduction"
XMarkdown can render mathematical formulas in **real time.**
## Examples
Pythagoras says that $a^2 + b^2 = c^2$.
Integration of polynomials depends on the formula
$$
int x^n dx = frac(1,n+1) x^{n+1} + C
$$
"""defaultCompilerParameters = { docWidth = 500 , editCount = 0 , selectedId = "" , selectedSlug = Nothing , theme = Light , backgroundColor = "rgba(255, 255, 255, 1.0)" , highlightColor = "rgba(200, 200, 255, 0.4)" , paddingAboveHeadings = 10 , interBlockSpacing = 0 , lineHeight = 1.5 , fontSize = 16 , windowWidth = 500 , scale = 1 , numberToLevel = 0 , data = Dict.empty }
Render only the body content from a CompilerOutput.
Takes a width parameter (in pixels) and returns a list of Html elements representing the document body without the title or banner.
body =
viewBodyOnly 600 outputThis is useful when you want to display the main content separately from other document parts like the table of contents or title.
Render the table of contents from a CompilerOutput.
Generates a navigable table of contents based on the document structure (sections, subsections, etc.).
toc =
viewTOC outputThe table of contents automatically includes links to document sections and respects the document hierarchy.
Re-export BlockMatch type for searching
Search blocks in the document for text containing the search query.
Returns a list of matching blocks with their metadata (id, lineNumber, numberOfLines, sourceText). The search is case-insensitive.
matches =
searchBlocksContainingText params (String.lines sourceText) "hello"For exporters that take a parsed document. An "Export HTML" button, for example, does
source
|> XMarkdown.API.parse
|> HTML.Export.toTextHtml HTML.Export.WithTOC HTML.Export.WithSectionNumbersand saves the resulting string.
A parsed XMarkdown document. It is opaque: make one with parse and pass
it to an exporter.
Parse XMarkdown source text.
-- Render the document body
body =
XMarkdown.API.viewBodyOnly 600 output
-- Render table of contents separately
toc =
XMarkdown.API.viewTOC output
Compile source text into a CompilerOutput structure.
This is the main compilation function that parses and processes XMarkdown source text. The output contains the rendered body, optional banner, table of contents, and title, which can then be displayed using the view functions.