github.com/kata-containers/tests@v0.0.0-20240307153542-772105b56064/cmd/check-markdown/types.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 "github.com/sirupsen/logrus" 10 11 // LinkType represents the type of a link in a markdown document. 12 type LinkType int 13 14 const ( 15 unknownLink LinkType = iota 16 internalLink LinkType = iota 17 externalLink LinkType = iota // External ".md" file 18 externalFile LinkType = iota // External non-".md" file 19 urlLink LinkType = iota 20 mailLink LinkType = iota 21 LinkTypeCount LinkType = iota 22 ) 23 24 func (t LinkType) String() string { 25 var name string 26 27 switch t { 28 case unknownLink: 29 name = "unknown" 30 case internalLink: 31 name = "internal-link" 32 case externalLink: 33 name = "external-link" 34 case externalFile: 35 name = "external-file" 36 case urlLink: 37 name = "url-link" 38 case mailLink: 39 name = "mail-link" 40 } 41 42 return name 43 } 44 45 // Heading is a markdown heading, which might be the destination 46 // for a link. 47 // 48 // Example: A heading like this: 49 // 50 // ### This is a `verbatim` heading 51 // 52 // ... would be described as: 53 // 54 // ```go 55 // 56 // Heading{ 57 // Name: "This is a verbatim heading", 58 // MDName "This is a `verbatim` heading", 59 // LinkName: "this-is-a-verbatim-heading", 60 // Level: 3, 61 // } 62 // 63 // ``` 64 type Heading struct { 65 // Not strictly necessary since the name is used as a hash key. 66 // However, storing here too makes the code simpler ;) 67 Name string 68 69 // Name including any markdown syntax 70 MDName string 71 72 // The encoded value of Name. 73 LinkName string 74 75 // Heading level (1 for top level) 76 Level int 77 } 78 79 // Link is a reference to another part of this document 80 // (or another document). 81 // 82 // Example: A link like this: 83 // 84 // [internal link](#internal-section-name) 85 // 86 // ... would be described as: 87 // 88 // ```go 89 // 90 // Link{ 91 // Address: "internal-section-name", 92 // ResolvedPath: "", 93 // Description: "internal link", 94 // Type: internalLink, 95 // } 96 // 97 // And a link like this: 98 // 99 // [external link](/foo.md#section-name) 100 // 101 // ... would be described as: 102 // 103 // ```go 104 // 105 // Link{ 106 // Address: "foo.md#section-name", 107 // ResolvedPath: "/docroot/foo.md", 108 // Description: "external link", 109 // Type: externalLink, 110 // } 111 // 112 // ``` 113 type Link struct { 114 // Document this link refers to. 115 Doc *Doc 116 117 // Original address from document. 118 // 119 // Must be a valid Heading.LinkName. 120 // 121 // Not strictly necessary since the address is used as a hash key. 122 // However, storing here too makes the code simpler ;) 123 Address string 124 125 // The fully expanded address, without any anchor and heading suffix. 126 // 127 // Only applies to certain link types. 128 ResolvedPath string 129 130 // The text the user sees for the hyperlink address 131 Description string 132 133 Type LinkType 134 } 135 136 // Doc represents a markdown document. 137 type Doc struct { 138 Logger *logrus.Entry 139 140 // Key: heading name 141 // Value: Heading 142 Headings map[string]Heading 143 144 // Key: link address 145 // Value: *list* of links. Required since you can have multiple links with 146 // the same _address_, but of a different type. 147 Links map[string][]Link 148 149 // Filename 150 Name string 151 152 // true when this document has been fully parsed 153 Parsed bool 154 155 // if true, only show the Table Of Contents 156 ShowTOC bool 157 158 ListMode bool 159 }