github.com/kata-containers/tests@v0.0.0-20240307153542-772105b56064/cmd/check-markdown/add.go (about) 1 // 2 // Copyright (c) 2019 Intel Corporation 3 // 4 // SPDX-License-Identifier: Apache-2.0 5 // 6 7 package main 8 9 import ( 10 "errors" 11 "fmt" 12 "path/filepath" 13 "strings" 14 15 "github.com/sirupsen/logrus" 16 ) 17 18 // linkAddrToPath converts a link address into a path name. 19 func (d *Doc) linkAddrToPath(address string) (string, error) { 20 if address == "" { 21 return "", errors.New("need address") 22 } 23 24 dir := filepath.Dir(d.Name) 25 26 var file string 27 28 // An "absolute link path" like this has been specified: 29 // 30 // [Foo](/absolute-link.md) 31 if strings.HasPrefix(address, absoluteLinkPrefix) { 32 if !fileExists(docRoot) { 33 return "", fmt.Errorf("document root %q does not exist", docRoot) 34 } 35 36 file = filepath.Join(docRoot, address) 37 } else { 38 file = filepath.Join(dir, address) 39 } 40 41 return file, nil 42 } 43 44 // addHeading adds the specified heading to the document. 45 // 46 // Note that headings must be unique. 47 func (d *Doc) addHeading(heading Heading) error { 48 name := heading.Name 49 50 if name == "" { 51 return d.Errorf("heading name cannot be blank: %+v", heading) 52 } 53 54 if heading.LinkName == "" { 55 return d.Errorf("heading link name cannot be blank: %q (%+v)", 56 name, heading) 57 } 58 59 if heading.Level <= 0 { 60 return d.Errorf("heading level must be atleast 1: %q (%+v)", 61 name, heading) 62 } 63 64 if _, ok := d.Headings[name]; ok { 65 return d.Errorf("duplicate heading: %q (heading: %+v)", 66 name, heading) 67 } 68 69 // Potentially change the ID to handle strange characters 70 // supported in links by GitHub. 71 id, err := createHeadingID(heading.Name) 72 if err != nil { 73 return err 74 } 75 76 heading.LinkName = id 77 78 d.Logger.WithField("heading", fmt.Sprintf("%+v", heading)).Debug("adding heading") 79 80 d.Headings[name] = heading 81 82 return nil 83 } 84 85 // addLink potentially adds the specified link to the document. 86 // 87 // Note that links do not need to be unique: a document can contain 88 // multiple links with: 89 // 90 // - the same description and the same address. 91 // - the same description but with different addresses. 92 // - different descriptions but with the same address. 93 func (d *Doc) addLink(link Link) error { 94 addr := link.Address 95 96 if link.ResolvedPath != "" { 97 addr = link.ResolvedPath 98 } 99 100 if addr == "" { 101 return d.Errorf("link address cannot be blank: %+v", link) 102 } 103 104 if link.Type == unknownLink { 105 return d.Errorf("BUG: link type invalid: %+v", link) 106 } 107 108 // Not checked by default as magic "build status" / go report / godoc 109 // links don't have a description - they have a image only. 110 if strict && link.Description == "" { 111 return d.Errorf("link description cannot be blank: %q (%+v)", 112 addr, link) 113 } 114 115 fields := logrus.Fields{ 116 "link": fmt.Sprintf("%+v", link), 117 } 118 119 links := d.Links[addr] 120 121 for _, l := range links { 122 if l.Type == link.Type { 123 d.Logger.WithFields(fields).Debug("not adding duplicate link") 124 125 return nil 126 } 127 } 128 129 d.Logger.WithFields(fields).Debug("adding link") 130 131 links = append(links, link) 132 d.Links[addr] = links 133 134 return nil 135 }