forked from kata-containers/tests
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhack.go
69 lines (52 loc) · 1.28 KB
/
hack.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// Copyright (c) 2019 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"strings"
bf "gopkg.in/russross/blackfriday.v2"
)
// forceCreateHeadings extracts "missed" headings from the specified node,
// returning a slice of the newly headings created (which need to be added by the
// caller).
//
// Alas, Black Friday isn't 100% reliable...
func (d *Doc) forceCreateHeadings(node *bf.Node) ([]Heading, error) {
if err := checkNode(node, bf.Text); err != nil {
return []Heading{}, err
}
chunk := string(node.Literal)
if chunk == "" {
// No text in this node
return []Heading{}, nil
}
lines := strings.Split(chunk, "\n")
if len(lines) <= 1 {
// No headings lurking in this text node
return []Heading{}, nil
}
var headings []Heading
for _, line := range lines {
if !strings.HasPrefix(line, anchorPrefix) {
continue
}
fields := strings.Split(line, anchorPrefix)
name := strings.Join(fields, "")
name = strings.TrimSpace(name)
count := strings.Count(line, anchorPrefix)
heading := Heading{
Name: name,
Level: count,
}
id, err := createHeadingID(heading.Name)
if err != nil {
return []Heading{}, err
}
heading.LinkName = id
headings = append(headings, heading)
extraHeadings++
}
return headings, nil
}