Giter VIP home page Giter VIP logo

Comments (2)

kjk avatar kjk commented on June 2, 2024

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.

from markdown.

mislavzanic avatar mislavzanic commented on June 2, 2024

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.

from markdown.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.