Skip to content

Extract specific Heading from document if exists #254

Answered by mmiranda
mmiranda asked this question in Q&A
Discussion options

You must be logged in to vote

What I got so far was this:

        doc := gm.Parser().Parse(text.NewReader(source))
	ast.Walk(doc, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
		s := ast.WalkStatus(ast.WalkContinue)
		var err error
		if n.Kind().String() == "Heading" && string(n.Text(source)) == "Title I'm filtering" {
		        // Apply any logic here

                         // gets the title
			fmt.Println(string(n.Text(source)))
                         // get block content
			fmt.Println(string(n.NextSibling().Text(source)))

                         // Stop walking
			s = ast.WalkStatus(ast.WalkStop)
		}
		return s, err
	})

Is this the right way to do it?

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by mmiranda
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants