forked from gitea/gitea
1
0
Fork 0
gitea/vendor/github.com/yuin/goldmark-meta
6543 30ce3731a1
Vendor Update Go Libs (#13444)
* denisenkom/go-mssqldb untagged -> v0.9.0

* github.com/editorconfig/editorconfig-core-go v2.3.7 -> v2.3.8

* github.com/go-testfixtures/testfixtures v3.4.0 -> v3.4.1

* github.com/mholt/archiver v3.3.2 -> v3.5.0

* github.com/olivere/elastic v7.0.20 -> v7.0.21

* github.com/urfave/cli v1.22.4 -> v1.22.5

* github.com/xanzy/go-gitlab v0.38.1 -> v0.39.0

* github.com/yuin/goldmark-meta untagged -> v1.0.0

* github.com/ethantkoenig/rupture 0a76f03a811a -> c3b3b810dc77

* github.com/jaytaylor/html2text 8fb95d837f7d -> 3577fbdbcff7

* github.com/kballard/go-shellquote cd60e84ee657 -> 95032a82bc51

* github.com/msteinert/pam 02ccfbfaf0cc -> 913b8f8cdf8b

* github.com/unknwon/paginater 7748a72e0141 -> 042474bd0eae

* CI.restart()

Co-authored-by: techknowlogick <techknowlogick@gitea.io>
2020-11-06 13:41:42 -05:00
..
.gitignore Handle yaml frontmatter (#11016) 2020-04-09 11:54:50 +01:00
LICENSE Handle yaml frontmatter (#11016) 2020-04-09 11:54:50 +01:00
README.md Vendor Update Go Libs (#13444) 2020-11-06 13:41:42 -05:00
go.mod Vendor Update Go Libs (#13444) 2020-11-06 13:41:42 -05:00
go.sum Vendor Update Go Libs (#13444) 2020-11-06 13:41:42 -05:00
meta.go Vendor Update Go Libs (#13444) 2020-11-06 13:41:42 -05:00

README.md

goldmark-meta

GoDev

goldmark-meta is an extension for the goldmark that allows you to define document metadata in YAML format.

Usage

Installation

go get github.com/yuin/goldmark-meta

Markdown syntax

YAML metadata block is a leaf block that can not have any markdown element as a child.

YAML metadata must start with a YAML metadata separator. This separator must be at first line of the document.

A YAML metadata separator is a line that only - is repeated.

YAML metadata must end with a YAML metadata separator.

You can define objects as a 1st level item. At deeper level, you can define any kind of YAML element.

Example:

---
Title: goldmark-meta
Summary: Add YAML metadata to the document
Tags:
    - markdown
    - goldmark
---

# Heading 1

Access the metadata

import (
    "bytes"
    "fmt"
    "github.com/yuin/goldmark"
    "github.com/yuin/goldmark/extension"
    "github.com/yuin/goldmark/parser"
    "github.com/yuin/goldmark-meta"
)

func main() {
    markdown := goldmark.New(
        goldmark.WithExtensions(
            meta.Meta,
        ),
    )
    source := `---
Title: goldmark-meta
Summary: Add YAML metadata to the document
Tags:
    - markdown
    - goldmark
---

# Hello goldmark-meta
`

    var buf bytes.Buffer
    context := parser.NewContext()
    if err := markdown.Convert([]byte(source), &buf, parser.WithContext(context)); err != nil {
        panic(err)
    }
    metaData := meta.Get(context)
    title := metaData["Title"]
    fmt.Print(title)
}

Render the metadata as a table

You need to add extension.TableHTMLRenderer or the Table extension to render metadata as a table.

import (
    "bytes"
    "fmt"
    "github.com/yuin/goldmark"
    "github.com/yuin/goldmark/extension"
    "github.com/yuin/goldmark/parser"
    "github.com/yuin/goldmark/renderer"
    "github.com/yuin/goldmark/util"
    "github.com/yuin/goldmark-meta"
)

func main() {
    markdown := goldmark.New(
        goldmark.WithExtensions(
            meta.New(meta.WithTable()),
        ),
        goldmark.WithRendererOptions(
            renderer.WithNodeRenderers(
                util.Prioritized(extension.NewTableHTMLRenderer(), 500),
            ),
        ),
    )
    // OR
    // markdown := goldmark.New(
    //     goldmark.WithExtensions(
    //         meta.New(meta.WithTable()),
    //         extension.Table,
    //     ),
    // )
    source := `---
Title: goldmark-meta
Summary: Add YAML metadata to the document
Tags:
    - markdown
    - goldmark
---

# Hello goldmark-meta
`

    var buf bytes.Buffer
    if err := markdown.Convert([]byte(source), &buf); err != nil {
        panic(err)
    }
    fmt.Print(buf.String())
}

License

MIT

Author

Yusuke Inuzuka