gomarkdown / markdown

markdown parser and HTML renderer for Go
Other
1.36k stars 171 forks source link

Support render markdown in the `<details>` #276

Open zyxkad opened 1 year ago

zyxkad commented 1 year ago

Source:

# This is a header

<p>
# This probably shouldn't be a header, it's good
</p>

<details>

# This should be a header, but it's not

</details>

<p>

# This should be a header too

</p>

Playground: https://go.dev/play/p/JlPMv2ebWw2 Github render: https://gist.github.com/zyxkad/7dfc84efa8ea9464481bd57b97646d9d

Half parser in babelmark support that. https://babelmark.github.io/?text=%0A%23+This+is+a+header%0A%0A%3Cp%3E%0A%23+This+probably+needn%27t+be+a+header%2C+it%27s+good%0A%3C%2Fp%3E%0A%0A%3Cdetails%3E%0A%0A%23+This+should+be+a+header%2C+but+it%27s+not%0A%0A%3C%2Fdetails%3E%0A%0A%3Cp%3E%0A%0A%23+This+should+be+a+header+too%0A%0A%3C%2Fp%3E%0A

kjk commented 1 year ago

I'm unlikely to have time to implement a big change like that.

There's no support for markdown-inside-html. ast.HTMLBlock is a leaf node which means there's no support for it to have children.

Not impossible but probably a big change and would require a parser flag as this is significantly different behavior.

mislavzanic commented 1 year ago

This can be implemented as a parser hook + renderer hook.

package renderer

import (
    "bytes"
    "io"

    "github.com/gomarkdown/markdown/ast"
)

type Details struct {
    ast.Container
}

var details = []byte("<details>")

func ParserHook(data []byte) (ast.Node, []byte, int) {
    if node, d, n := parseDetails(data); node != nil {
        return node, d, n
    }
    return nil, nil, 0
}

func parseDetails(data []byte) (ast.Node, []byte, int) {
    if !bytes.HasPrefix(data, sidenote) {
        return nil, nil, 0
    }
    i := bytes.Index(data, details)
    end := bytes.Index(data[i:], []byte("</details>"))
    if end < 0 {
        return nil, data, 0
    }
    end = end + i
    res := &Details{}
    return res, data[i:end], end + len([]byte("</details>"))
}

func renderDetails(w io.Writer, s *Details, entering bool) {
    if entering {
        io.WriteString(w, "<details>")
    } else {
        io.WriteString(w, "</details>")
    }
}

func RenderHook(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
    if leafNode, ok := node.(*Details); ok {
        renderDetails(w, leafNode, entering)
        return ast.GoToNext, true
    }
    return ast.GoToNext, false
}

There may be some mistakes here, but the idea should work. The keys is to abuse the order of parsing (the parser hook is evaluated before html blocks) and the second return value of parser hook (it is added as a child of the Details container and will be parsed as a block). This implies that you'll need to create a new struct for html blocks that need to be parsed like this.

weberc2 commented 1 month ago

Strangely, markdown is converted when the </details> is the very last character in the input, at least in some cases.

https://go.dev/play/p/-6D9vsqCPgT