goldmark

package
v0.10.2 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2025 License: MIT Imports: 8 Imported by: 0

README

GoSVGChart Goldmark Extension

This extension for the Goldmark markdown parser allows you to embed charts directly in your markdown documents using the familiar GoSVGChart syntax.

Installation

go get github.com/riclib/gosvgchart

Usage

Add the GoSVGChart extension to your Goldmark parser:

import (
    "github.com/riclib/gosvgchart/goldmark"
    gm "github.com/yuin/goldmark"
)

// Create a new Goldmark instance with the gosvgchart extension
markdown := gm.New(
    gm.WithExtensions(
        goldmark.New(),
    ),
)

// Convert markdown to HTML
var output []byte
if err := markdown.Convert([]byte(markdownContent), &output); err != nil {
    // Handle error
}

Syntax

Use a fenced code block with the language identifier gosvgchart and include the chart definition:

```gosvgchart
barchart
title: Quarterly Revenue
width: 600
height: 400
colors: #2ecc71, #e74c3c, #f39c12, #9b59b6

data:
Q1 | 850
Q2 | 940
Q3 | 1100
Q4 | 1200
```

The extension will replace this code block with the rendered SVG chart.

Chart Types

The extension supports all chart types provided by GoSVGChart:

  1. Line Chart: Use linechart as the first line in your code block
  2. Bar Chart: Use barchart as the first line in your code block
  3. Pie Chart: Use piechart as the first line in your code block

Complete Example

Here's a full example of a markdown document with various chart types:

# Sales Report

## Quarterly Revenue

```gosvgchart
barchart
title: Quarterly Revenue
width: 600
height: 400
colors: #2ecc71, #e74c3c, #f39c12, #9b59b6

data:
Q1 | 850
Q2 | 940
Q3 | 1100
Q4 | 1200
```

## Monthly Sales Trend

```gosvgchart
linechart
title: Monthly Sales
width: 600
height: 400
colors: #3498db

data:
Jan | 120
Feb | 250
Mar | 180
Apr | 310
May | 270
Jun | 390
```

## Market Share

```gosvgchart
piechart
title: Market Share
width: 500
height: 500
colors: #3498db, #2ecc71, #e74c3c, #f39c12, #9b59b6

data:
Product A | 35
Product B | 25
Product C | 20
Product D | 15
Others | 5
```

Side-by-Side Charts

When you place multiple chart code blocks directly next to each other without any text in between, the extension will automatically render them side-by-side in a responsive layout:

## Revenue Comparison

```gosvgchart
barchart
title: 2023 Revenue
width: 500
height: 300
colors: #3498db, #2ecc71

data:
Q1 | 850
Q2 | 940
Q3 | 1100
Q4 | 1200
```
```gosvgchart
barchart
title: 2024 Revenue
width: 500
height: 300
colors: #e74c3c, #f39c12

data:
Q1 | 950
Q2 | 1040
Q3 | 1200
Q4 | 1400
```

You can also place three or more charts side by side, and they will automatically wrap to new lines on smaller screens:

## Regional Market Share

```gosvgchart
piechart
title: North Region
width: 300
height: 300
colors: #3498db, #2ecc71, #e74c3c

data:
Product A | 45
Product B | 35
Product C | 20
```
```gosvgchart
piechart
title: South Region
width: 300
height: 300
colors: #3498db, #2ecc71, #e74c3c

data:
Product A | 30
Product B | 45
Product C | 25
```
```gosvgchart
piechart
title: East Region
width: 300
height: 300
colors: #3498db, #2ecc71, #e74c3c

data:
Product A | 25
Product B | 25
Product C | 50
```

Error Handling

If there's an error in your chart definition, the extension will output an HTML comment with the error message instead of the SVG chart.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ChartNodeKind = ast.NewNodeKind("ChartNode")

Define a custom node kind for charts

Functions

func New

func New() goldmark.Extender

New returns a new ChartCodeBlockHandler extension

func NewChartRenderer

func NewChartRenderer() renderer.NodeRenderer

NewChartRenderer returns a new ChartRenderer

func NewChartTransformer

func NewChartTransformer() parser.ASTTransformer

NewChartTransformer returns a new chart transformer

Types

type ChartCodeBlockHandler

type ChartCodeBlockHandler struct{}

ChartCodeBlockHandler is a Goldmark extension for handling gosvgchart code blocks

func (*ChartCodeBlockHandler) Extend

func (c *ChartCodeBlockHandler) Extend(md goldmark.Markdown)

Extend adds the gosvgchart code block handling to the Goldmark parser

type ChartNode

type ChartNode struct {
	ast.BaseBlock
	// contains filtered or unexported fields
}

ChartNode represents a chart in the AST

func NewChartNode

func NewChartNode() *ChartNode

NewChartNode returns a new ChartNode

func (*ChartNode) Dump

func (n *ChartNode) Dump(source []byte, level int)

Dump implements Node.Dump

func (*ChartNode) Kind

func (n *ChartNode) Kind() ast.NodeKind

Kind implements Node.Kind

func (*ChartNode) Markdown

func (n *ChartNode) Markdown() string

Markdown returns the markdown content of the chart

func (*ChartNode) SetMarkdown

func (n *ChartNode) SetMarkdown(markdown string)

SetMarkdown sets the markdown content of the chart

type ChartRenderer

type ChartRenderer struct{}

ChartRenderer renders ChartNode

func (*ChartRenderer) RegisterFuncs

func (r *ChartRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer)

RegisterFuncs registers the render functions for ChartNode

type ChartTransformer

type ChartTransformer struct{}

ChartTransformer transforms fenced code blocks with "gosvgchart" language to ChartNode

func (*ChartTransformer) Transform

func (c *ChartTransformer) Transform(doc *ast.Document, reader text.Reader, pc parser.Context)

Transform looks for code blocks with "gosvgchart" language and transforms them to ChartNode

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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