Skip to content
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

Open
zyxkad opened this issue Mar 11, 2023 · 3 comments
Open

Support render markdown in the <details> #276

zyxkad opened this issue Mar 11, 2023 · 3 comments

Comments

@zyxkad
Copy link

zyxkad commented Mar 11, 2023

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
Copy link
Contributor

kjk commented Mar 11, 2023

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
Copy link

mislavzanic commented Jul 5, 2023

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
Copy link

weberc2 commented Jul 1, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants