A markdown parser written in Go. Easy to extend, standard(CommonMark) compliant, well structured.

goldmark

https://pkg.go.dev/github.com/yuin/goldmark https://github.com/yuin/goldmark/actions?query=workflow:test https://coveralls.io/github/yuin/goldmark https://goreportcard.com/report/github.com/yuin/goldmark

A Markdown parser written in Go. Easy to extend, standards-compliant, well-structured.

goldmark is compliant with CommonMark 0.29.

Motivation

I needed a Markdown parser for Go that satisfies the following requirements:

  • Easy to extend.
    • Markdown is poor in document expressions compared to other light markup languages such as reStructuredText.
    • We have extensions to the Markdown syntax, e.g. PHP Markdown Extra, GitHub Flavored Markdown.
  • Standards-compliant.
    • Markdown has many dialects.
    • GitHub-Flavored Markdown is widely used and is based upon CommonMark, effectively mooting the question of whether or not CommonMark is an ideal specification.
      • CommonMark is complicated and hard to implement.
  • Well-structured.
    • AST-based; preserves source position of nodes.
  • Written in pure Go.

golang-commonmark may be a good choice, but it seems to be a copy of markdown-it.

blackfriday.v2 is a fast and widely-used implementation, but is not CommonMark-compliant and cannot be extended from outside of the package, since its AST uses structs instead of interfaces.

Furthermore, its behavior differs from other implementations in some cases, especially regarding lists: Deep nested lists don't output correctly #329, List block cannot have a second line #244, etc.

This behavior sometimes causes problems. If you migrate your Markdown text from GitHub to blackfriday-based wikis, many lists will immediately be broken.

As mentioned above, CommonMark is complicated and hard to implement, so Markdown parsers based on CommonMark are few and far between.

Features

  • Standards-compliant. goldmark is fully compliant with the latest CommonMark specification.
  • Extensible. Do you want to add a @username mention syntax to Markdown? You can easily do so in goldmark. You can add your AST nodes, parsers for block-level elements, parsers for inline-level elements, transformers for paragraphs, transformers for the whole AST structure, and renderers.
  • Performance. goldmark's performance is on par with that of cmark, the CommonMark reference implementation written in C.
  • Robust. goldmark is tested with go-fuzz, a fuzz testing tool.
  • Built-in extensions. goldmark ships with common extensions like tables, strikethrough, task lists, and definition lists.
  • Depends only on standard libraries.

Installation

$ go get github.com/yuin/goldmark

Usage

Import packages:

import (
    "bytes"
    "github.com/yuin/goldmark"
)

Convert Markdown documents with the CommonMark-compliant mode:

var buf bytes.Buffer
if err := goldmark.Convert(source, &buf); err != nil {
  panic(err)
}

With options

var buf bytes.Buffer
if err := goldmark.Convert(source, &buf, parser.WithContext(ctx)); err != nil {
  panic(err)
}
Functional option Type Description
parser.WithContext A parser.Context Context for the parsing phase.

Context options

Functional option Type Description
parser.WithIDs A parser.IDs IDs allows you to change logics that are related to element id(ex: Auto heading id generation).

Custom parser and renderer

import (
    "bytes"
    "github.com/yuin/goldmark"
    "github.com/yuin/goldmark/extension"
    "github.com/yuin/goldmark/parser"
    "github.com/yuin/goldmark/renderer/html"
)

md := goldmark.New(
          goldmark.WithExtensions(extension.GFM),
          goldmark.WithParserOptions(
              parser.WithAutoHeadingID(),
          ),
          goldmark.WithRendererOptions(
              html.WithHardWraps(),
              html.WithXHTML(),
          ),
      )
var buf bytes.Buffer
if err := md.Convert(source, &buf); err != nil {
    panic(err)
}
Functional option Type Description
goldmark.WithParser parser.Parser This option must be passed before goldmark.WithParserOptions and goldmark.WithExtensions
goldmark.WithRenderer renderer.Renderer This option must be passed before goldmark.WithRendererOptions and goldmark.WithExtensions
goldmark.WithParserOptions ...parser.Option
goldmark.WithRendererOptions ...renderer.Option
goldmark.WithExtensions ...goldmark.Extender

Parser and Renderer options

Parser options

Functional option Type Description
parser.WithBlockParsers A util.PrioritizedSlice whose elements are parser.BlockParser Parsers for parsing block level elements.
parser.WithInlineParsers A util.PrioritizedSlice whose elements are parser.InlineParser Parsers for parsing inline level elements.
parser.WithParagraphTransformers A util.PrioritizedSlice whose elements are parser.ParagraphTransformer Transformers for transforming paragraph nodes.
parser.WithASTTransformers A util.PrioritizedSlice whose elements are parser.ASTTransformer Transformers for transforming an AST.
parser.WithAutoHeadingID - Enables auto heading ids.
parser.WithAttribute - Enables custom attributes. Currently only headings supports attributes.

HTML Renderer options

Functional option Type Description
html.WithWriter html.Writer html.Writer for writing contents to an io.Writer.
html.WithHardWraps - Render newlines as
.
html.WithXHTML - Render as XHTML.
html.WithUnsafe - By default, goldmark does not render raw HTML or potentially dangerous links. With this option, goldmark renders such content as written.

Built-in extensions

Attributes

The parser.WithAttribute option allows you to define attributes on some elements.

Currently only headings support attributes.

Attributes are being discussed in the CommonMark forum. This syntax may possibly change in the future.

Headings

## heading ## {#id .className attrName=attrValue class="class1 class2"}

## heading {#id .className attrName=attrValue class="class1 class2"}
heading {#id .className attrName=attrValue}
============

Table extension

The Table extension implements Table(extension), as defined in GitHub Flavored Markdown Spec.

Specs are defined for XHTML, so specs use some deprecated attributes for HTML5.

You can override alignment rendering method via options.

Functional option Type Description
extension.WithTableCellAlignMethod extension.TableCellAlignMethod Option indicates how are table cells aligned.

Typographer extension

The Typographer extension translates plain ASCII punctuation characters into typographic-punctuation HTML entities.

Default substitutions are:

Punctuation Default entity
' ,
" ,
--
---
...
<< «
>> »

You can override the default substitutions via extensions.WithTypographicSubstitutions:

markdown := goldmark.New(
    goldmark.WithExtensions(
        extension.NewTypographer(
            extension.WithTypographicSubstitutions(extension.TypographicSubstitutions{
                extension.LeftSingleQuote:  []byte("‚"),
                extension.RightSingleQuote: nil, // nil disables a substitution
            }),
        ),
    ),
)

Linkify extension

The Linkify extension implements Autolinks(extension), as defined in GitHub Flavored Markdown Spec.

Since the spec does not define details about URLs, there are numerous ambiguous cases.

You can override autolinking patterns via options.

Functional option Type Description
extension.WithLinkifyAllowedProtocols [][]byte List of allowed protocols such as [][]byte{ []byte("http:") }
extension.WithLinkifyURLRegexp *regexp.Regexp Regexp that defines URLs, including protocols
extension.WithLinkifyWWWRegexp *regexp.Regexp Regexp that defines URL starting with www.. This pattern corresponds to the extended www autolink
extension.WithLinkifyEmailRegexp *regexp.Regexp Regexp that defines email addresses`

Example, using xurls:

import "mvdan.cc/xurls/v2"

markdown := goldmark.New(
    goldmark.WithRendererOptions(
        html.WithXHTML(),
        html.WithUnsafe(),
    ),
    goldmark.WithExtensions(
        extension.NewLinkify(
            extension.WithLinkifyAllowedProtocols([][]byte{
                []byte("http:"),
                []byte("https:"),
            }),
            extension.WithLinkifyURLRegexp(
                xurls.Strict,
            ),
        ),
    ),
)

Footnotes extension

The Footnote extension implements PHP Markdown Extra: Footnotes.

This extension has some options:

Functional option Type Description
extension.WithFootnoteIDPrefix []byte a prefix for the id attributes.
extension.WithFootnoteIDPrefixFunction func(gast.Node) []byte a function that determines the id attribute for given Node.
extension.WithFootnoteLinkTitle []byte an optional title attribute for footnote links.
extension.WithFootnoteBacklinkTitle []byte an optional title attribute for footnote backlinks.
extension.WithFootnoteLinkClass []byte a class for footnote links. This defaults to footnote-ref.
extension.WithFootnoteBacklinkClass []byte a class for footnote backlinks. This defaults to footnote-backref.
extension.WithFootnoteBacklinkHTML []byte a class for footnote backlinks. This defaults to ↩︎.

Some options can have special substitutions. Occurances of “^^” in the string will be replaced by the corresponding footnote number in the HTML output. Occurances of “%%” will be replaced by a number for the reference (footnotes can have multiple references).

extension.WithFootnoteIDPrefix and extension.WithFootnoteIDPrefixFunction are useful if you have multiple Markdown documents displayed inside one HTML document to avoid footnote ids to clash each other.

extension.WithFootnoteIDPrefix sets fixed id prefix, so you may write codes like the following:

for _, path := range files {
    source := readAll(path)
    prefix := getPrefix(path)

    markdown := goldmark.New(
        goldmark.WithExtensions(
            NewFootnote(
                WithFootnoteIDPrefix([]byte(path)),
            ),
        ),
    )
    var b bytes.Buffer
    err := markdown.Convert(source, &b)
    if err != nil {
        t.Error(err.Error())
    }
}

extension.WithFootnoteIDPrefixFunction determines an id prefix by calling given function, so you may write codes like the following:

markdown := goldmark.New(
    goldmark.WithExtensions(
        NewFootnote(
                WithFootnoteIDPrefixFunction(func(n gast.Node) []byte {
                    v, ok := n.OwnerDocument().Meta()["footnote-prefix"]
                    if ok {
                        return util.StringToReadOnlyBytes(v.(string))
                    }
                    return nil
                }),
        ),
    ),
)

for _, path := range files {
    source := readAll(path)
    var b bytes.Buffer

    doc := markdown.Parser().Parse(text.NewReader(source))
    doc.Meta()["footnote-prefix"] = getPrefix(path)
    err := markdown.Renderer().Render(&b, source, doc)
}

You can use goldmark-meta to define a id prefix in the markdown document:

---
title: document title
slug: article1
footnote-prefix: article1
---

# My article

Security

By default, goldmark does not render raw HTML or potentially-dangerous URLs. If you need to gain more control over untrusted contents, it is recommended that you use an HTML sanitizer such as bluemonday.

Benchmark

You can run this benchmark in the _benchmark directory.

against other golang libraries

blackfriday v2 seems to be the fastest, but as it is not CommonMark compliant, its performance cannot be directly compared to that of the CommonMark-compliant libraries.

goldmark, meanwhile, builds a clean, extensible AST structure, achieves full compliance with CommonMark, and consumes less memory, all while being reasonably fast.

goos: darwin
goarch: amd64
BenchmarkMarkdown/Blackfriday-v2-12                  326           3465240 ns/op         3298861 B/op      20047 allocs/op
BenchmarkMarkdown/GoldMark-12                        303           3927494 ns/op         2574809 B/op      13853 allocs/op
BenchmarkMarkdown/CommonMark-12                      244           4900853 ns/op         2753851 B/op      20527 allocs/op
BenchmarkMarkdown/Lute-12                            130           9195245 ns/op         9175030 B/op     123534 allocs/op
BenchmarkMarkdown/GoMarkdown-12                        9         113541994 ns/op         2187472 B/op      22173 allocs/op

against cmark (CommonMark reference implementation written in C)

----------- cmark -----------
file: _data.md
iteration: 50
average: 0.0037760639 sec
go run ./goldmark_benchmark.go
------- goldmark -------
file: _data.md
iteration: 50
average: 0.0040964230 sec

As you can see, goldmark's performance is on par with cmark's.

Extensions

goldmark internal(for extension developers)

Overview

goldmark's Markdown processing is outlined in the diagram below.

            
                           |
                           V
            +-------- parser.Parser ---------------------------
            | 1. Parse block elements into AST
            |   1. If a parsed block is a paragraph, apply 
            |      ast.ParagraphTransformer
            | 2. Traverse AST and parse blocks.
            |   1. Process delimiters(emphasis) at the end of
            |      block parsing
            | 3. Apply parser.ASTTransformers to AST
                           |
                           V
                      
                           |
                           V
            +------- renderer.Renderer ------------------------
            | 1. Traverse AST and apply renderer.NodeRenderer
            |    corespond to the node type

                           |
                           V
                        

Parsing

Markdown documents are read through text.Reader interface.

AST nodes do not have concrete text. AST nodes have segment information of the documents, represented by text.Segment .

text.Segment has 3 attributes: Start, End, Padding .

(TBC)

TODO

See extension directory for examples of extensions.

Summary:

  1. Define AST Node as a struct in which ast.BaseBlock or ast.BaseInline is embedded.
  2. Write a parser that implements parser.BlockParser or parser.InlineParser.
  3. Write a renderer that implements renderer.NodeRenderer.
  4. Define your goldmark extension that implements goldmark.Extender.

Donation

BTC: 1NEDSyUmo4SMTDP83JJQSWi1MvQUGGNMZB

License

MIT

Author

Yusuke Inuzuka

Owner
Yusuke Inuzuka
Software developer from Japan. Explicit is better than implicit.
Yusuke Inuzuka
Comments
  • Plural possessives (Typographer extension)

    Plural possessives (Typographer extension)

    This issue occurs with the Typographer extension in Hugo — including the most recent release, 0.80.0 — if goldmark is the selected parser. It doesn’t occur if Blackfriday is the selected parser and “smart” punctuation is activated. I have complied with the @yuin requirement for Hugo users to bring up such issues in the Hugo repo before doing so here (https://github.com/gohugoio/hugo/issues/8099).

    Consider the following text in a Markdown file:

    John's dog is named Sam. The Smiths' dog is named Rover.
    

    Expected result: Each apostrophe should be a “smart”/curly apostrophe (&rsquo;), given the default behavior of goldmark’s Typographer extension.

    Actual result: Only the singular possessive (John's dog) has the “smart”/curly apostrophe, while the plural possessive (Smiths' dog) has the “dumb”/straight apostrophe (&apos;).

    Thanks in advance for any help or consideration this issue may receive.

  • Parser does not preserve whitespaces when parsing nested code blocks.

    Parser does not preserve whitespaces when parsing nested code blocks.

    Thank you for the amazing project! 🤗

    I think I found a small bug, which is a bit annoying in our markdown formatting project. Particular problem is showcased in this draft PR

    1. What version of goldmark are you using? Checked v1.1.24 and latest 6c741ae251abd461bb7b5ce28e7df7a9306bd005
    2. What version of Go are you using? go version go1.15 linux/amd64
    3. What operating system and processor architecture are you using? go version go1.15 linux/amd64
    4. What did you do?

    Parsed nested code block (valid markdown):

    * Some items with nested code with strict whitespaces.
      ```Makefile
      include .bingo/Variables.mk
      
      run:
      	@$(GOIMPORTS) <args>
      ```
    

    (Note strict whitespace in above md, especially line <space><space>\t@$(GOIMPORTS) <args>)

    1. What did you expect to see?

    goldmark renderer.Renderer.Render(...) method's n ast.Node has correct structure. However lines in ast.FencedCodeBlock has wrong whitespace (somehow codeblock being fenced affects things).

    Particularly: Lines in the node should have exactly the same bytes, so for example line 3 should be <space><space>\t@$(GOIMPORTS) <args>

    See repro test below.

    1. What did you see instead?

    Line 1 and 3 has some semi-random spaces instead what provided in parsed markdown.

    Particularly line 3 has <space><space>@$(GOIMPORTS) <args>. See repro test below.

    1. Did you confirm your output is different from CommonMark online demo or other official renderer correspond with an extension?: YES image

    Repro go test:

    package markdown
    
    import (
    	"bytes"
    	"fmt"
    	"io"
    	"testing"
    
    	"github.com/yuin/goldmark"
    	"github.com/yuin/goldmark/ast"
    	"github.com/yuin/goldmark/renderer"
    )
    
    type testRenderer struct {
    	t *testing.T
    }
    
    func (t testRenderer) AddOptions(...renderer.Option) { return }
    func (t testRenderer) Render(_ io.Writer, source []byte, n ast.Node) error {
    	fencedCodeBlock := n.FirstChild().FirstChild().FirstChild().NextSibling().(*ast.FencedCodeBlock)
    
    	line := fencedCodeBlock.Lines().At(0)
    	if val := line.Value(source); !bytes.Equal([]byte("include .bingo/Variables.mk\n"), val) {
    		t.t.Errorf("not what we expected, got %q", string(val))
    	}
    	line = fencedCodeBlock.Lines().At(1)
    	if val := line.Value(source); !bytes.Equal([]byte("\n"), val) {
    		t.t.Errorf("not what we expected, got %q", string(val)) // BUG1: bug_test.go:28: not what we expected, got "  \n"
    	}
    	line = fencedCodeBlock.Lines().At(2)
    	if val := line.Value(source); !bytes.Equal([]byte("run:\n"), val) {
    		t.t.Errorf("not what we expected, got %q", string(val))
    	}
    	line = fencedCodeBlock.Lines().At(3)
    	if val := line.Value(source); !bytes.Equal([]byte("\t@$(GOIMPORTS) <args>\n"), val) {
    		t.t.Errorf("not what we expected, got %q", string(val)) // BUG 2: bug_test.go:36: not what we expected, got "  @$(GOIMPORTS) <args>\n"	}
    	}
    	return nil
    }
    
    func TestGoldmarkCodeBlockWhitespaces(t *testing.T) {
    	var codeBlock = "```"
    	mdContent := []byte(fmt.Sprintf(`* Some item with nested code with strict whitespaces.
      %sMakefile
      include .bingo/Variables.mk
      
      run:
      	@$(GOIMPORTS) <args>
      %s`, codeBlock, codeBlock))
    
    	var buf bytes.Buffer
    	if err := goldmark.New(goldmark.WithRenderer(&testRenderer{t: t})).Convert(mdContent, &buf); err != nil {
    		t.Fatal(err)
    	}
    }
    

    I am pretty sure it's somehow easy to fix (:

  • Use non-numeric footnote anchor in id and href

    Use non-numeric footnote anchor in id and href

    Please answer the following before submitting your issue:

    1. What version of goldmark are you using? : v1.1.21
    2. What version of Go are you using? : go1.13.6
    3. What operating system and processor architecture are you using? : linux/amd64
    4. What did you do? : Use footnotes like blah[^name] ... [^name]: more
    5. What did you expect to see? : HTML with id and href mentioning name, retain backwards compatibility with existing website using blackfriday.
    6. What did you see instead? : fn:1
    7. Did you confirm your output is different from CommonMark online demo or other official renderer correspond with an extension?: Footnotes are an extension so CommonMark does not apply. The linked to php-markdown "spec" doesn't even allow named footnotes. blackfriday did exactly what I wanted.
    8. (Feature request only): Why you can not implement it as an extension?: I can, but it seems like I'd end up copy-pasting at least FootnoteHTMLRenderer, and the way it uses private methods and registration it doesn't seem like I could easily wrap & extend it.

    It seems like this would be a simple enhancement to current footnote extension. If n.Ref is not numeric, use it instead of n.Index.

  • Consider adding a context (data holder) to Render

    Consider adding a context (data holder) to Render

    This is a follow up to #37

    So, setting state on the nodes in the AST and then use that while rendering works, but ...

    • It makes for some fairly clumsy and verbose code
    • It breaks the separation of concerns (adding rendering code to the parser)

    What I'm now doing instead is something ala:

            w := renderContext{
    		BufWriter: bufio.NewWriter(buf),
    		renderContextData: renderContextDataHolder{
    			rctx: ctx,
    			dctx: c.ctx,
    		},
    	}
    
    	if err := c.md.Renderer().Render(w, ctx.Src, doc); err != nil {
    		return nil, err
    	}
    

    This works great , and I don't mind doing it like this (this is entirely internal), but the down side is that it may stop working in the future if you decide to wrap the writer or something.

  • Rendering

    Rendering "class" attribute

    Hi @yuin I'm trying to append class="..." to all img tags and wondering if something like this would make sense to add (of course it's simply a hard-coded example for "class" attribute only) :

    https://github.com/zzwx-forks/goldmark/commit/11441f5283f66d71c6986b2bd5828dfbb0aebf19

    This way users wouldn't have to completely rewrite render function in case something simple as adding a class is needed and they don't want to possibly break the code when the library gets updated.

    This is my use case:

    case *ast.Image:
      if entering {
        n.SetAttributeString("class", "img-fluid")
      }
    
  • Apostrophes in contractions are not converted to right single quote

    Apostrophes in contractions are not converted to right single quote

    For the following text:

    I'm going to see my mother. She's very nice.
    

    Currently, ' is not converted to &rsquo; for contractions when the typography extension is enabled, but smartypants does. I would expect the output to be:

    I&rsquo;m going to see my mother. She&rsquo;s very nice.
    
  • typographer extension should ignore code element content

    typographer extension should ignore code element content

    main.go
    package main
    
    import (
    	"bytes"
    	"fmt"
    
    	"github.com/yuin/goldmark"
    	"github.com/yuin/goldmark/extension"
    	"github.com/yuin/goldmark/renderer/html"
    )
    
    func main() {
    	md := goldmark.New(
    		goldmark.WithExtensions(
    			extension.Typographer,
    		),
    		goldmark.WithRendererOptions(
    			html.WithUnsafe(),
    		),
    	)
    
    	input := `<code>"foo"</code>`
    
    	var buf bytes.Buffer
    	if err := md.Convert([]byte(input), &buf); err != nil {
    		panic(err)
    	}
    
    	fmt.Println(buf.String())
    
    }
    

    Desired output:

    <p><code>"foo"</code></p>
    

    Actual output:

    <p><code>&ldquo;foo&rdquo;</code></p>
    
  • How to apply microtypographic rules to Markdown?

    How to apply microtypographic rules to Markdown?

    1. What version of goldmark are you using? : v1.11.1 (Hugo 0.60.1)
    2. What version of Go are you using? : go1.13.4
    3. What operating system and processor architecture are you using? darwin/amd64 (macOS 10.15.1)
    4. What did you do? : Write Markdown in french language
    5. What did you expect to see? : french typographic rules applied (like inserting a non-breakable space before a question mark)
    6. What did you see instead? : no french typographic rules
    7. (Feature request only): Why you can not implement it as an extension?: Not a Go programmer

    How should be french typographic rules applied, through an extension, or is it something that is dependendant of the Go language itself? Or another Go Package? SmartyPants but with more rules specific to a language.

    For instance, languages like PHP have libs to handle this https://github.com/jolicode/JoliTypo

    Some of the french typographic rules are liste by Grammalecte Firefox extension:

  • Typographic elements in heading are excluded from the automatically generated heading IDs

    Typographic elements in heading are excluded from the automatically generated heading IDs

    Hello,

    For background and related discussion, please see the following post in Hugo forum.

    https://discourse.gohugo.io/t/difference-in-auto-generated-heading-anchor-names-between-previous-versions-and-v0-60-x/22076

    Please answer the following before submitting your issue:

    1. What version of goldmark are you using? : 1.1.8 (included in Hugo 0.60.1)
    2. What version of Go are you using? : 1.11.2 (but shouldn't matter as the test is done with pre-built Hugo)
    3. What operating system and processor architecture are you using? : macOS 10.13.6, Intel Core i5
    4. What did you do? : Upgrade Hugo from 0.54.0 to 0.60.1 to check the basic functionality
    5. What did you expect to see? : Non-alphanumeric typhographic elements (hyphen, period, underscore, etc.) in heading are transformed into hyphen in the auto heading IDs (e.g. for heading "Command-Gen-Instance" and "v1.0.0 (Apr 21, 2019)", the results are command-gen-instance and v1-0-0-apr-21-2019)
    6. What did you see instead? : Non-alphanumeric typhographic elements in heading are excluded from the auto heading IDs (e.g. for the example above, the results are commandgeninstance and v100-april-21-2019)

    Many thanks for your work with Goldmark.

  • Header attributes

    Header attributes

    It seems that parser.WithAttribute() pocessinп does not always work well.

    https://github.com/mironovalexey/gm-test/tree/master/hattrs

    https://github.com/mironovalexey/gm-test/blob/master/hattrs/test.md

  • Footnote After Link Parsing Error

    Footnote After Link Parsing Error

    The link parser seems a little confused by a footnote immediately following a brackets-only link. Sample Markdown:

    Visit [Foo][^1].
    
      [Foo]: https://example.com/
      [^1]: But not really
    

    Expected HTML:

    <p>Visit <a href="https://example.com/">Foo</a><sup id="fnref:1"><a href="#fn:1" class="footnote-ref" role="doc-noteref">1</a></sup>.</p>
    

    Actual HTML:

    <p>Visit [Foo]<sup id="fnref:1"><a href="#fn:1" class="footnote-ref" role="doc-noteref">1</a></sup>.</p>
    
  • Fix #335

    Fix #335

    I think this fixes the issue described in #335 when using both linkify and typographer. I intend my contribution to be similar to the behaviour of https://github.com/github/cmark-gfm/blob/6a6e335709ef68cf2c616eeaf61b09ed4c654669/extensions/autolink.c#L58

    However, when using just linkify, it doesn't trigger on quotes so there will be no link created. This seems inconsistent to me but it was not part of the original issue so I'm just mentioning it.

  • extra_test.go: Add test timeout multiplier environment variable

    extra_test.go: Add test timeout multiplier environment variable

    Hi,

    Currently some performance tests in extra_test.go are failing in low-performance environments (QEMU user mode emulation in my case).

    === RUN   TestDeepNestedLabelPerformance
        extra_test.go:112: Parsing deep nested labels took more 5 secs
    --- FAIL: TestDeepNestedLabelPerformance (25.33s)
    === RUN   TestManyProcessingInstructionPerformance
        extra_test.go:132: Parsing processing instructions took more 5 secs
    --- FAIL: TestManyProcessingInstructionPerformance (14.11s)
    === RUN   TestManyCDATAPerformance
        extra_test.go:152: Parsing processing instructions took more 5 secs
    --- FAIL: TestManyCDATAPerformance (33.70s)
    === RUN   TestManyDeclPerformance
        extra_test.go:172: Parsing processing instructions took more 5 secs
    --- FAIL: TestManyDeclPerformance (16.28s)
    === RUN   TestManyCommentPerformance
        extra_test.go:192: Parsing processing instructions took more 5 secs
    --- FAIL: TestManyCommentPerformance (40.88s)
    

    I think reading a timeout multiplier from environment variable is a reasonable way to test goldmark in those low-performance environments without affecting the tests in normal environments.

    Thank you for looking into this PR!

  • testutil: Export ParseTestCaseFile

    testutil: Export ParseTestCaseFile

    This PR extracts functionality from DoTestCaseFile responsible for parsing test case files into a ParseTestCases function.

    The function is able to read from any io.Reader, instead of being limited to just files. A ParseTestCaseFile function is included for convenience.

    The functions do not panic on errors--instead, they return the error.

    While there, this updates DoTestCaseFile so that it also does not panic on error, and instead aborts the entire test instead.

    Minor note about tests: The tests for the parser use reflect.DeepEqual and %#v. It would make for better error messages to use go-cmp but I elected to not use that because so far, goldmark has no third-party dependencies. If you're open to the new dependency (only for test util), I can make a separate PR for that in the future.

  • Bad href attribute when linkify and typographer extensions are enabled

    Bad href attribute when linkify and typographer extensions are enabled

    What version of goldmark are you using? : 1.5.2 What version of Go are you using? : 1.19.3 What operating system and processor architecture are you using? : linux/amd64

    code
    package main
    
    import (
    	"bytes"
    	"fmt"
    
    	"github.com/yuin/goldmark"
    	"github.com/yuin/goldmark/extension"
    )
    
    func main() {
    	md := goldmark.New(
    		goldmark.WithExtensions(
    			extension.Typographer,
    			extension.Linkify,
    		),
    	)
    
    	input := `"https://example.org/"`
    
    	var buf bytes.Buffer
    	if err := md.Convert([]byte(input), &buf); err != nil {
    		panic(err)
    	}
    
    	fmt.Println(buf.String())
    
    }
    

    Expected output:

    <p>&ldquo;<a href="https://example.org/">https://example.org/</a>&rdquo;</p>
    

    Actual output:

    <p>&ldquo;<a href="https://example.org/%22">https://example.org/&quot;</a></p>
    

    The behavior is triggered by the trailing slash in the URL.

  • renderer: fix panic on unregistered node kind

    renderer: fix panic on unregistered node kind

    Renderers can ignore nodes they aren't interested in. However when a renderer doesn't register the highest node kind traversing a Markdown document results in the following panic:

    panic: runtime error: index out of range [1] with length 1
    
    goroutine 1 [running]:
    github.com/yuin/goldmark/renderer.(*renderer).Render.func2({0x9d8630, 0xc0001237a0}, 0x40?)
    	/home/simon/go/pkg/mod/github.com/yuin/[email protected]/renderer/renderer.go:164 +0xd9
    github.com/yuin/goldmark/ast.walkHelper({0x9d8630, 0xc0001237a0}, 0xc000521a48)
    	/home/simon/go/pkg/mod/github.com/yuin/[email protected]/ast/ast.go:492 +0x34
    github.com/yuin/goldmark/ast.Walk(...)
    	/home/simon/go/pkg/mod/github.com/yuin/[email protected]/ast/ast.go:487
    github.com/yuin/goldmark/renderer.(*renderer).Render(0xc0002d80a0?, {0x9d3b60?, 0xc0000744a0?}, {0xc0002f4580?, 0x281?, 0x2c0?}, {0x9d8630?, 0xc0001237a0?})
    	/home/simon/go/pkg/mod/github.com/yuin/[email protected]/renderer/renderer.go:161 +0x225
    github.com/yuin/goldmark.(*markdown).Convert(0xc0001e1d40, {0xc0002f4580, 0x281, 0x2c0}, {0x9d3b60, 0xc0000744a0}, {0x0, 0x0, 0x0})
    	/home/simon/go/pkg/mod/github.com/yuin/[email protected]/markdown.go:117 +0x10b
    main.renderMarkdown({0xc0002f42c0, 0x281})
    	/home/simon/src/hut/markdown.go:16 +0x97
    main.newListsListCommand.func1(0xc000270280?, {0xc778b8, 0x0, 0x0?})
    	/home/simon/src/hut/lists.go:109 +0x2ee
    github.com/spf13/cobra.(*Command).execute(0xc000270280, {0xc778b8, 0x0, 0x0})
    	/home/simon/go/pkg/mod/github.com/spf13/[email protected]/command.go:860 +0x663
    github.com/spf13/cobra.(*Command).ExecuteC(0xc000242c80)
    	/home/simon/go/pkg/mod/github.com/spf13/[email protected]/command.go:974 +0x3b4
    github.com/spf13/cobra.(*Command).Execute(...)
    	/home/simon/go/pkg/mod/github.com/spf13/[email protected]/command.go:902
    github.com/spf13/cobra.(*Command).ExecuteContext(...)
    	/home/simon/go/pkg/mod/github.com/spf13/[email protected]/command.go:895
    main.main()
    	/home/simon/src/hut/main.go:49 +0x30b
    

    Make sure r.nodeRendererFuncs is large enough before trying to access it.

    Workaround without this patch:

    reg.Register(ast.NodeKind(128), nil)
    
🚩 TOC, zero configuration table of content generator for Markdown files, create table of contents from any Markdown file with ease.
🚩 TOC, zero configuration table of content generator for Markdown files, create table of contents from any Markdown file with ease.

toc toc TOC, table of content generator for Markdown files Table of Contents Table of Contents Usage Installation Packages Arch Linux Homebrew Docker

Dec 29, 2022
Markdown - Markdown converter for golang

markdown ?? Talks ?? Join ?? Youtube ❤️ Sponsor Install via nami nami install ma

Jun 2, 2022
A PDF renderer for the goldmark markdown parser.
A PDF renderer for the goldmark markdown parser.

goldmark-pdf goldmark-pdf is a renderer for goldmark that allows rendering to PDF. Reference See https://pkg.go.dev/github.com/stephenafamo/goldmark-p

Jan 7, 2023
An extension to the Goldmark Markdown Parser

Goldmark-Highlight An extension to the Goldmark Markdown Parser which adds parsing / rendering capabilities for rendering highlighted text. Highlighte

May 25, 2022
An (almost) compliant XPath 1.0 library.

xsel xsel is a library that (almost) implements the XPath 1.0 specification. The non-compliant bits are: xsel does not implement the id function. The

Dec 21, 2022
A collection of well-known string hash functions, implemented in Go

This library is a collection of "well-known" 32-bit string hashes, implemented in Go. It includes: Java string hash ELF-32 Jenkins' One-A

Mar 3, 2022
A CLI markdown converter written in Go.

MDConv is a markdown converter written in Go. It is able to create PDF and HTML files from Markdown without using LaTeX. Instead MDConv u

Dec 20, 2022
Simple Markdown-Driven Scaffolding tool written by Go
Simple Markdown-Driven Scaffolding tool written by Go

Manaita Simple Markdown-Driven Scaffolding tool written by Go Write your scaffolding code on SCAFFOLD.md and generate files using the scaffold. Scaffo

Jun 25, 2022
Extract structured data from web sites. Web sites scraping.
Extract structured data from web sites. Web sites scraping.

Dataflow kit Dataflow kit ("DFK") is a Web Scraping framework for Gophers. It extracts data from web pages, following the specified CSS Selectors. You

Jan 7, 2023
Blackfriday: a markdown processor for Go

Blackfriday Blackfriday is a Markdown processor implemented in Go. It is paranoid about its input (so you can safely feed it user-supplied data), it i

Jan 8, 2023
⚙️ Convert HTML to Markdown. Even works with entire websites and can be extended through rules.
⚙️ Convert HTML to Markdown. Even works with entire websites and can be extended through rules.

html-to-markdown Convert HTML into Markdown with Go. It is using an HTML Parser to avoid the use of regexp as much as possible. That should prevent so

Jan 6, 2023
Produces a set of tags from given source. Source can be either an HTML page, Markdown document or a plain text. Supports English, Russian, Chinese, Hindi, Spanish, Arabic, Japanese, German, Hebrew, French and Korean languages.
Produces a set of tags from given source. Source can be either an HTML page, Markdown document or a plain text. Supports English, Russian, Chinese, Hindi, Spanish, Arabic, Japanese, German, Hebrew, French and Korean languages.

Tagify Gets STDIN, file or HTTP address as an input and returns a list of most popular words ordered by popularity as an output. More info about what

Dec 19, 2022
csvplus extends the standard Go encoding/csv package with fluent interface, lazy stream operations, indices and joins.

csvplus Package csvplus extends the standard Go encoding/csv package with fluent interface, lazy stream processing operations, indices and joins. The

Apr 9, 2022
Upskirt markdown library bindings for Go

Goskirt Package goskirt provides Go-bindings for the excellent Sundown Markdown parser. (F/K/A Upskirt). To use goskirt, create a new Goskirt-value wi

Oct 23, 2022
A markdown renderer package for the terminal
A markdown renderer package for the terminal

go-term-markdown go-term-markdown is a go package implementing a Markdown renderer for the terminal. Note: Markdown being originally designed to rende

Nov 25, 2022
:triangular_ruler:gofmtmd formats go source code block in Markdown. detects fenced code & formats code using gofmt.
:triangular_ruler:gofmtmd formats go source code block in Markdown. detects fenced code & formats code using gofmt.

gofmtmd gofmtmd formats go source code block in Markdown. detects fenced code & formats code using gofmt. Installation $ go get github.com/po3rin/gofm

Oct 31, 2022
Convert Microsoft Word Document to Markdown
Convert Microsoft Word Document to Markdown

docx2md Convert Microsoft Word Document to Markdown Usage $ docx2md NewDocument.docx Installation $ go get github.com/mattn/docx2md Supported Styles

Jan 4, 2023
Stylesheet-based markdown rendering for your CLI apps 💇🏻‍♀️
Stylesheet-based markdown rendering for your CLI apps 💇🏻‍♀️

Glamour Write handsome command-line tools with Glamour. glamour lets you render markdown documents & templates on ANSI compatible terminals. You can c

Jan 1, 2023
go-md2man - 转换 Markdown 为 man 手册内容

go-md2man Converts markdown into roff (man pages). Uses blackfriday to process markdown into man pages. Usage ./md2man -in /path/to/markdownfile.md -o

Dec 22, 2022