-
Notifications
You must be signed in to change notification settings - Fork 175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support render markdown in the <details>
#276
Comments
I'm unlikely to have time to implement a big change like that. There's no support for markdown-inside-html. Not impossible but probably a big change and would require a parser flag as this is significantly different behavior. |
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 |
Strangely, markdown is converted when the |
Source:
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
The text was updated successfully, but these errors were encountered: