internal

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package internal package is used to store the internal implementation of the mermaid package.

Package internal package is used to store the internal implementation of the mermaid package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EntityEscape added in v1.0.0

func EntityEscape(r rune) string

EntityEscape returns the mermaid escape for r, for example "#quot;" for a double quote.

The numeric form is used for everything except the double quote, which is written by name because that is the form mermaid's own documentation shows and the form a reader of the generated diagram will recognize.

func EscapeBareAngle added in v1.0.0

func EscapeBareAngle(s string) string

EscapeBareAngle returns s with every "<" that does not open a "<br/>" written as the entity form "#60;", and everything else untouched.

The renderer's sanitizer reads a bare "<" followed by a letter as the start of an HTML tag and eats the rest of the text, in quoted labels as much as in titles: a block label, a git graph commit id, a requirement name and an xy chart label each drew "a" where the caller asked for "a<b then c", measured by rendering. "#60;" decodes to the character in every one of them. "<br/>" is left alone because it renders today, and run this after EscapeEntityOpeners: the "#60;" written here must not be escaped again.

func EscapeEntityOpeners added in v1.0.0

func EscapeEntityOpeners(s string) string

EscapeEntityOpeners returns s with every "#" that starts an entity written as "#35;", and every other byte untouched.

It is the first pass of an escape that goes on to write entities of its own: run before those are inserted, it keeps a caller's literal "#92;" distinct from the character the escape writes as "#92;", and it never touches the escape's own output because that output is inserted afterwards.

func EscapeTitle added in v1.0.0

func EscapeTitle(title string) string

EscapeTitle returns a title ready to be written into a `title` statement whose diagram type accepts a line break in it: the angles and entity-opening hashes are escaped by EscapeTitleAngle, and then every line ending is written as "#10;", which was measured to decode to a real line break in a title. The order matters: the hash escape runs first so that a caller's literal "#10;" stays distinct from a caller's newline.

"<br/>" is not the spelling here because a title draws it as the literal text "<br>", measured directly; it is the line the renderer puts between a title and a label.

func EscapeTitleAngle added in v1.0.0

func EscapeTitleAngle(title string) string

EscapeTitleAngle returns a title ready to be written into the `title` statement of a diagram, with every "<" that does not open a "<br/>" written as the entity form "#60;".

The renderer's sanitizer reads a bare "<" in a title as the start of an HTML tag and eats the rest of the text, so "cost < 10" reaches the drawing as "cost " or not at all; sixteen diagram types lose their title this way, and "#60;" was measured to decode to a literal "<" in every one of them, front matter included. "<br/>" is left alone because it renders today, as the literal text "<br>", and output that renders is not changed.

A "#" that would start an entity is escaped for the same reason the label escapes do it: a title now written with entities has to keep a caller's literal "#60;" distinct from a caller's "<". A "#" anywhere else reaches the drawing intact and is left alone.

func FoldFrontMatterTitleCR added in v1.0.0

func FoldFrontMatterTitleCR(title string) string

FoldFrontMatterTitleCR returns a title ready for FrontMatterTitle in a diagram type whose renderer draws the front matter title.

The YAML quoting keeps every character parseable, but a carriage return still loses the title in the drawing where a line feed is drawn as a line break, so a CRLF pair or a lone CR is folded into the line feed that works.

A bare "<" is the one character left broken here: the sanitizer eats the rest of the title, and unlike in a title statement no escape helps, because a front matter title draws every entity form as the literal text it is — "#60;", "&lt;" and "&#60;" were each rendered and each came back verbatim. There is nothing to escape to, so the limit is documented rather than papered over with text the caller never wrote. The diagram types whose renderer never draws a title keep their bytes either way: their front matter is inert, and inert output that renders is not changed.

func FrontMatterTitle added in v1.0.0

func FrontMatterTitle(title string) string

FrontMatterTitle returns the `title:` line of a mermaid front matter block.

The value is always a double quoted YAML scalar, because mermaid runs the front matter through a YAML parser before it draws anything and a bare scalar is not safe to build from arbitrary text. "Checkout: API" and "*ref" make that parser throw, which loses the whole diagram; "Checkout # API" is truncated at the comment, and "~", "# Checkout" and "&anchor" resolve to something that is not the title at all. Quoting removes every one of those readings.

func LineBreaksToBr added in v1.0.0

func LineBreaksToBr(s string) string

LineBreaksToBr returns s with every line ending written as "<br/>", which is the line break mermaid draws.

A raw line ending inside a label never survives: the diagram grammars are line oriented, so the text after the break is read as the start of the next statement. Measured across every diagram type, that loses the whole diagram in twelve of them and silently drops or mangles content in the rest, which is worse. "<br/>" is the one spelling of a line break that reaches the drawing, so a caller's "\r\n", "\n" and "\r" are each written as one.

func LineFeed

func LineFeed() string

LineFeed return line feed for current OS.

func StartsEntity added in v1.0.0

func StartsEntity(s string) bool

StartsEntity reports whether s opens with the rest of a "#name;" or "#123;" escape, that is whether a "#" written immediately before it would be read as one rather than as an ordinary character.

This is what keeps the escaping injective. A package that writes a quotation mark as "#quot;" has to escape a caller's literal "#quot;" as well, or the two would produce the same diagram. A "#" anywhere else is ordinary text and is left alone, which is what keeps output that already renders unchanged.

Types

This section is empty.

Directories

Path Synopsis
Package buildertest exercises the error handling that every builder in this module shares.
Package buildertest exercises the error handling that every builder in this module shares.
Package golden compares generated markdown against committed golden files.
Package golden compares generated markdown against committed golden files.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL