kata-containers/tests/cmd/check-markdown/hack.go
Aurélien Bombo e9f88757ba tests: Remove replace directives in go.mod
Same rationale as for runtime. With tests, the blackfriday replacement was
actually meaningful, so I refactored some imports.

Signed-off-by: Aurélien Bombo <abombo@microsoft.com>
2025-03-14 18:00:36 +00:00

70 lines
1.3 KiB
Go

//
// Copyright (c) 2019 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"strings"
bf "github.com/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
}