Skip to content

💬 How to apply rules like in v1 for v2? #122

Open
@hbinduni

Description

@hbinduni

i create rules for v1 like below:

converter := htmltomarkdown.NewConverter("", true, nil)

// Add a custom rule for the <img> tag
converter.AddRules(
htmltomarkdown.Rule{
	Filter: []string{"img"},
	Replacement: func(content string, node *goquery.Selection, options *htmltomarkdown.Options) *string {
		// Get attributes from the <img> tag
		src, _ := node.Attr("src")
		alt, _ := node.Attr("alt")
		title, _ := node.Attr("title")

		// Return a custom Markdown format
		result := fmt.Sprintf("![Custom Format | Alt: %s | Title: %s](%s)", alt, title, src)
		return &result
	},
},
)

how to apply rules for v2?
is that correct to apply rules like v1 using custom render in v2?
something like this:

conv := converter.NewConverter(
		converter.WithPlugins(
			base.NewBasePlugin(),
			commonmark.NewCommonmarkPlugin(),
		),
	)
	
conv.Register.RendererFor("img", converter.TagTypeInline, func(ctx converter.Context, w converter.Writer, node *html.Node) converter.RenderStatus {
		src := getAttribute(node, "src")
		alt := getAttribute(node, "alt")
		title := getAttribute(node, "title")

		// Custom Markdown for <img> tags
		w.WriteString(fmt.Sprintf("![Custom Format | Alt: %s | Title: %s](%s)", alt, title, src))
		return converter.RenderSuccess
	}, converter.PriorityEarly)

thank you

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions