chess

package module
v0.0.0-...-92c1525 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2024 License: MIT Imports: 11 Imported by: 0

README

goldmark-chess

=========================

This is an extension to goldmark markdown that that renders interactive previews for chess PGN embedded in a fenced code blocks.

Usage

package main

import (
	"bytes"
	"fmt"
	"github.com/yuin/goldmark"
	chess "github.com/br1tney5pear5/goldmark-chess"
)

func main() {
	source := `~~~chess
[Event "Reykjavik Rapid"]
[Site "Reykjavik ISL"]
[Date "2004.03.18"]
[Result "1-0"]
[White "Magnus Carlsen"]
[Black "Garry Kasparov"]
1. e4 e5 2. Bc4 Nc6 3. Qh5 Nf6 4. Qxf7#
~~~
`
	// Simple usage
	md := goldmark.New(
		goldmark.WithExtensions(
			chess.Chess,
		),
        )
        var buf bytes.Buffer
        if err := md.Convert([]byte(source), &buf); err != nil {
          panic(err)
        }
        fmt.Print(buf.String())
}

Installation

go get github.com/br1tney5pear5/goldmark-chess

Future work

  • I think we don't sanitise pgn atm so it'll be just pasted verbatim in the embed renderer's div. This is an issue bc I think goldmark has a safe mode where it'll refuse to render any raw html and this could be used to bypass it.

  • Currently this extension renders pgn using caissa embeded viewer. I'd be cool to have many different providers to choose from.

Hugo Integration

diff --git a/markup/goldmark/convert.go b/markup/goldmark/convert.go
index 7c00433d5..037650545 100644
--- a/markup/goldmark/convert.go
+++ b/markup/goldmark/convert.go
@@ -39,6 +39,8 @@ import (
 
 	"github.com/gohugoio/hugo/markup/converter"
 	"github.com/gohugoio/hugo/markup/tableofcontents"
+
+	chess "github.com/br1tney5pear5/goldmark-chess"
 )
 
 const (
@@ -220,6 +222,10 @@ func newMarkdown(pcfg converter.ProviderConfig) goldmark.Markdown {
 		extensions = append(extensions, attributes.New())
 	}
 
+	if cfg.Extensions.Chess.Enable {
+		extensions = append(extensions, chess.Chess)
+        }
+
 	md := goldmark.New(
 		goldmark.WithExtensions(
 			extensions...,
diff --git a/markup/goldmark/goldmark_config/config.go b/markup/goldmark/goldmark_config/config.go
index 620475c48..e249f136c 100644
--- a/markup/goldmark/goldmark_config/config.go
+++ b/markup/goldmark/goldmark_config/config.go
@@ -70,6 +70,9 @@ var Default = Config{
 				Block:  [][]string{},
 			},
 		},
+		Chess: ChessConfig{
+			Enable: false,
+		},
 	},
 	Renderer: Renderer{
 		Unsafe: false,
@@ -128,6 +131,7 @@ type Extensions struct {
 	DefinitionList bool
 	Extras         Extras
 	Passthrough    Passthrough
+	Chess          ChessConfig
 
 	// GitHub flavored markdown
 	Table           bool
@@ -200,6 +204,11 @@ type Passthrough struct {
 	Delimiters DelimitersConfig
 }
 
+type ChessConfig struct {
+	Enable bool
+	// TODO: Add PGN display provider here
+}
+
 type DelimitersConfig struct {
 	// The delimiters to use for inline passthroughs. Each entry in the list
 	// is a size-2 list of strings, where the first string is the opening delimiter

Then set markup.goldmark.extensions.chess.enable to true in your config.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Chess = &chess{}
View Source
var KindChessBlock = ast.NewNodeKind("ChessBlock")

Functions

func NewChessHTMLRenderer

func NewChessHTMLRenderer(opts ...html.Option) renderer.NodeRenderer

func RandStringRunes

func RandStringRunes(n int) string

Types

type ChessBlock

type ChessBlock struct {
	ast.BaseBlock
}

func (*ChessBlock) Dump

func (b *ChessBlock) Dump(src []byte, level int)

Dump dumps the contents of this block to stdout.

func (*ChessBlock) IsRaw

func (*ChessBlock) IsRaw() bool

IsRaw reports that this block should be rendered as-is. TODO: Is it raw or not?

func (*ChessBlock) Kind

func (*ChessBlock) Kind() ast.NodeKind

Kind reports that this is a MermaidBlock.

type ChessHTMLRenderer

type ChessHTMLRenderer struct {
	html.Config
}

func (*ChessHTMLRenderer) RegisterFuncs

RegisterFuncs implements NodeRenderer.RegisterFuncs.

type Transformer

type Transformer struct {
}

func (*Transformer) Transform

func (t *Transformer) Transform(doc *ast.Document, reader text.Reader, _ parser.Context)

Transform transforms the provided Markdown AST.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳