diagram

package module
v0.0.0-...-0ab7cd2 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: Apache-2.0 Imports: 17 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

NSGo Diagram

English | 简体中文

Native Mermaid diagrams for NSGo apps. limu reads and lays out every diagram type Pele draws — flowcharts, sequence, class, state and entity relationship diagrams, Gantt charts, pies, mind maps, timelines and more — and this module paints them on a canvas in the platform's font, light or dark as the app shows. Linked items open when pressed, tooltips show while the pointer rests on an item, and a wide flowchart turns to run down when its container is narrow.

Install

go get github.com/nsgo-dev/nsgo-diagram

Draw mermaid blocks in a TextView

Give a text view the plugin, and its mermaid code blocks draw as diagrams:

import (
	"github.com/nsgo-dev/nsgo/base/textview"

	diagram "github.com/nsgo-dev/nsgo-diagram"
)

textview.TextView{Markdown: source, Plugins: []textview.Plugin{diagram.Plugin()}}

The same Plugins field works on ui.TextView, and textview.Provide gives the plugin to every text view below a scope. A block still streaming in shows as code until it is complete, a copy takes its fenced source, and a diagram that fails to parse shows its source.

diagram.View shows a diagram anywhere else, laid out to its container's width. Its Icons maps Mermaid names such as "fa:fa-car" to *core.SVG documents, and Label overrides what assistive technology reads.

Follow the ui palette

The diagram package reads no ui colors itself. To draw diagrams in your palette, return a diagram.View with ThemeFunc from a plugin of your own. The function is read as the canvas draws, so a palette or appearance change recolors the diagram in place; colors left zero keep the defaults.

var themedDiagram = textview.Plugin{CodeLanguage: "mermaid", RenderCode: func(cx *core.Scope, code textview.CodeBlock) core.Node {
	colors := ui.ColorsOf(cx)
	return diagram.View{Source: code.Code, ErrorStyle: core.Style{FontFamily: core.FontMonospace}, ThemeFunc: func() *diagram.Theme {
		c := colors.Now()
		return &diagram.Theme{
			Background: c.Inset, Text: c.Text, Muted: c.Muted, Line: c.Border,
			Surface: c.Surface, SurfaceAlt: c.Raised, Border: c.Border,
			Accent: c.Accent, Critical: c.Danger,
			Series: [8]core.Color{c.Accent, c.Up, c.Warn, c.Danger, c.Info, c.Muted, c.AccentFill, c.Down},
			Radius: float32(ui.RadiusControl), StrokeWidth: 1,
		}
	}}
}}

ui.TextView{Markdown: source, Plugins: []textview.Plugin{themedDiagram}}

TSX

Import the tsx package for its effect in the Go host, next to NSGo's tsx/textview or tsx/ui. It registers Diagram in the TSX module nsgo-diagram, and go tool nsgo check writes its types to .nsgo/types/nsgo-diagram.d.ts.

import _ "github.com/nsgo-dev/nsgo-diagram/tsx"
import { TextView } from "nsgo/base";
import { Diagram } from "nsgo-diagram";

<TextView markdown={source}
  plugins={[{ codeLanguage: "mermaid", renderCode: (code) => <Diagram source={code.code} errorClass="font-mono" /> }]} />

Diagram takes source, fontSize, a partial theme (DiagramTheme), icons, accessibilityLabel and the usual styling props. A sandboxed bundle imports it only when its tsx.Sandbox lists "nsgo-diagram" in Modules.

Example

examples/diagrams shows a document whose diagrams follow the palette and a chat reply that streams a diagram in:

go tool nsgo dev examples/diagrams

Development

go tool nsgo check
go tool nsgo test

License

Apache License 2.0; see LICENSE and NOTICE, which credits limu (MIT).

Documentation

Overview

Package diagram draws Mermaid diagrams natively, with limu (github.com/firede/limu), which reads every diagram type Pele, the Obsidian team's Mermaid renderer, draws and lays it out: flowcharts, sequence, class, state and entity relationship diagrams, Gantt charts, pies, mind maps, timelines and more. It draws the laid-out scene on a canvas in the platform's font, which it measures the labels in too, so text fills the room the layout gave it. textview.TextView draws its mermaid code blocks so with Plugin.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Plugin

func Plugin() textview.Plugin

Plugin draws a text view's mermaid code blocks, in any case, as Mermaid's own Markdown writes them, for textview.TextView{Plugins: []textview.Plugin{diagram.Plugin()}} or textview.Provide. Each lays out to its block's width, centered, keeping a sideways scroll for a diagram limu cannot fit, and a copy takes its source between ```mermaid fences. A diagram that fails shows its source in the monospaced font. An app that themes its diagrams or gives them icons sets the plugin's RenderCode to a View of its own.

Types

type Theme

type Theme struct {
	Background core.Color // behind the diagram, and behind labels on lines
	Text       core.Color
	Muted      core.Color // secondary text, as the titles of groups
	Line       core.Color // edges and other lines
	Surface    core.Color // the inside of shapes
	SurfaceAlt core.Color // the inside of groups, and of shapes set apart
	Border     core.Color // the outlines of shapes
	Accent     core.Color // what a diagram marks out
	Critical   core.Color // what a diagram marks as critical
	// Series are the colors of a chart's series, as a pie's slices, in turn; a series left zero keeps the appearance's.
	Series      [8]core.Color
	Radius      float32
	StrokeWidth float32
}

Theme is a diagram's colors, as limu's themes name them, and its corners' radius and lines' width, in points. What it leaves zero keeps the appearance's theme, so a theme may change only the colors it names.

type View

type View struct {
	// Source is the diagram's Mermaid text, as a mermaid code block holds it; SourceFunc follows what it reads.
	Source     string
	SourceFunc func() string
	// FontSize is the size of labels with no size of their own; 0 is 16.
	FontSize core.Points
	// Theme colors the diagram over the appearance's theme: Pele's light theme in light, and its gray counterpart in dark, with the same blue accent as NSGo's own look.
	Theme *Theme
	// ThemeFunc follows the colors it reads. Returning nil keeps the appearance's defaults. Theme and ThemeFunc are mutually exclusive.
	ThemeFunc func() *Theme
	// Icons supplies SVG documents by the names used in the source, such as "fa:fa-car". Missing or nil entries leave icons out. Render snapshots the map; SVGs may be shared with core.Icon.
	Icons map[string]*core.SVG
	// Label is what assistive technology reads in place of the diagram's title and description.
	Label     string
	Class     string
	Style     core.Style
	StyleFunc func() core.Style
	// ErrorClass and ErrorStyle style the source a diagram that fails shows instead.
	ErrorClass string
	ErrorStyle core.Style
	TestID     string
}

View draws a Mermaid diagram laid out to the width its container gives it, turning a wide flowchart to run down where it fits better. Labels use the platform's font at FontSize, scaled by the user's text size setting as text is. It colors itself with the appearance's theme, gray with a blue accent in light and in dark, and with what Theme sets over it. A diagram that does not parse, or cannot be laid out, shows its source as text in ErrorClass and ErrorStyle. Assistive technology reads it as an image, named by Label, else by the diagram's accTitle and accDescr, else as a diagram, in the language the app shows.

func (View) Render

func (v View) Render(cx *core.Scope) core.Node

Render implements core.Node.

Directories

Path Synopsis
examples
diagrams command
Command diagrams shows Mermaid diagrams in an NSGo app: a document whose diagrams follow ui's palette, and a chat reply that streams one in, which shows as code until its block is complete.
Command diagrams shows Mermaid diagrams in an NSGo app: a document whose diagrams follow ui's palette, and a chat reply that streams one in, which shows as code until its block is complete.
Package locales says what the diagram package's diagrams read as, to assistive technology, in the language the app shows.
Package locales says what the diagram package's diagrams read as, to assistive technology, in the language the app shows.
Package tsx gives TSX the module nsgo-diagram, whose Diagram is the diagram package's View, which links limu: an app whose TSX uses it imports this package for its effect.
Package tsx gives TSX the module nsgo-diagram, whose Diagram is the diagram package's View, which links limu: an app whose TSX uses it imports this package for its effect.

Jump to

Keyboard shortcuts

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