File: empty_code.go

package info (click to toggle)
golang-github-johanneskaufmann-html-to-markdown 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,084 kB
  • sloc: makefile: 3
file content (42 lines) | stat: -rw-r--r-- 768 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package domutils

import (
	"context"

	"github.com/JohannesKaufmann/dom"
	"golang.org/x/net/html"
)

func hasTextChildNodes(startNode *html.Node) bool {
	var found bool

	var finder func(*html.Node)
	finder = func(node *html.Node) {
		if node.Type == html.TextNode && node.Data != "" {
			found = true
			return
		}
		for child := node.FirstChild; child != nil; child = child.NextSibling {
			finder(child)
		}
	}
	finder(startNode)

	return found
}

func RemoveEmptyCode(ctx context.Context, doc *html.Node) {
	node := doc
	for node != nil {
		if dom.NodeName(node) == "code" && !hasTextChildNodes(node) {
			next := dom.GetNextNeighborNodeExcludingOwnChild(node)

			dom.RemoveNode(node)

			node = next
			continue
		}

		node = dom.GetNextNeighborNode(node)
	}
}