jxxcarlson/xmarkdown-compiler/2.2.0

NOTE: the latest version is 2.2.6

XMarkdown.API

XMarkdown.API provides the core compilation interface for converting XMarkdown source text into renderable Html. See xmarkdowndemo.netlify.app for a working example.

Overview

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.

Example

    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
          $$
          """

Compilation

compileOutput : CompilerParameters -> String -> CompilerOutput

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.

compileString : CompilerParameters -> String -> List (Html MarkupMsg)
compileStringWithTitle : String -> CompilerParameters -> String -> List (Html MarkupMsg)
defaultCompilerParameters : CompilerParameters

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 }

Viewing

viewEditor : Config msg -> Html msg
viewBodyOnly : Int -> CompilerOutput -> List (Html MarkupMsg)

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 output

This is useful when you want to display the main content separately from other document parts like the table of contents or title.

viewTOC : CompilerOutput -> List (Html MarkupMsg)

Render the table of contents from a CompilerOutput.

Generates a navigable table of contents based on the document structure (sections, subsections, etc.).

toc =
    viewTOC output

The table of contents automatically includes links to document sections and respects the document hierarchy.

type alias BlockMatch =
BlockMatch

Re-export BlockMatch type for searching

fromMsgToSyncHighlight : Int -> MarkupMsg -> Maybe SyncHighlight
searchBlocksContainingText : CompilerParameters -> List String -> String -> List BlockMatch

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"

Parsing

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.WithSectionNumbers

and saves the resulting string.

type alias AST =
AST

A parsed XMarkdown document. It is opaque: make one with parse and pass it to an exporter.

parse : String -> AST

Parse XMarkdown source text.

    -- Render the document body
    body =
        XMarkdown.API.viewBodyOnly 600 output


    -- Render table of contents separately
    toc =
        XMarkdown.API.viewTOC output