mermaid.source

index · mermaid

Overview

A diagram's source text, cut into statements. Every diagram type shares this step: comments, the front matter and directives go, and what is left splits on newlines and semicolons.

A source may come from an untrusted page, so its size is capped here, before any parser reads it.

The accessibility statements, accTitle and accDescr, are read here too. They belong to every diagram type. No layout reads them: they go on the header, and from there on the frame, for a painter that names a picture.

Functions

NameSignature
keywordkeyword(text) -> word, rest, spaced
refuserefuse(line, reason) -> nil, err
statementsstatements(source) -> statements, header_or_err

keyword(text) -> word, rest, spaced

Read the keyword a statement opens with

Returns the word of letters a statement opens with, the text after the word and its blanks, and true when a blank followed the word. A keyword is a whole word: a blank, :, { or the statement's end follows it. Returns nil for a statement that opens with none, so style1 and loopback->>A open with no keyword.

refuse(line, reason) -> nil, err

Refuse a statement with the source line it stands on

Returns nil and line N: reason, the form of every refusal a diagram's parser gives for one statement.

statements(source) -> statements, header_or_err

Cut a diagram's source into statements

Returns the statements after the header, each { text, line } with the source line it came from, and the header in the same shape: the first statement, which names the diagram type. Its text is the header and its line is the header's source line.

Dropped on the way: a leading front-matter block between --- lines, %%{ … }%% directives, lines that open with %%, and blank statements. A directive keeps its newlines, so every line after it keeps its number.

The accessibility statements after the header are no statements. They go on the header: header.title from accTitle: text, header.descr from accDescr: text or accDescr { … }. Their keywords are read in any case. A value runs to the end of its line, semicolons included. A block runs to its first }, and what follows the brace on its line is cut into statements. A block keeps its lines, each trimmed, with the blank ones gone. A value holds no control character, and a run of blanks is one space. A later statement replaces an earlier one, and an empty value is none.

Returns nil and a reason for a source over 64 KB, a line over 2000 bytes, and a source with no statement. Returns nil and line N: reason for an accTitle or an accDescr in neither form, and for an accDescr block left open: accTitle --> B names no node.