github.com/dmaizel/tests@v0.0.0-20210728163746-cae6a2d9cee8/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  // Heading{
    56  //   Name:     "This is a verbatim heading",
    57  //   MDName    "This is a `verbatim` heading",
    58  //   LinkName: "this-is-a-verbatim-heading",
    59  //   Level:    3,
    60  // }
    61  // ```
    62  type Heading struct {
    63  	// Not strictly necessary since the name is used as a hash key.
    64  	// However, storing here too makes the code simpler ;)
    65  	Name string
    66  
    67  	// Name including any markdown syntax
    68  	MDName string
    69  
    70  	// The encoded value of Name.
    71  	LinkName string
    72  
    73  	// Heading level (1 for top level)
    74  	Level int
    75  }
    76  
    77  // Link is a reference to another part of this document
    78  // (or another document).
    79  //
    80  // Example: A link like this:
    81  //
    82  //     [internal link](#internal-section-name)
    83  //
    84  // ... would be described as:
    85  //
    86  // ```go
    87  // Link{
    88  //   Address:      "internal-section-name",
    89  //   ResolvedPath: "",
    90  //   Description:  "internal link",
    91  //   Type:         internalLink,
    92  // }
    93  //
    94  // And a link like this:
    95  //
    96  //     [external link](/foo.md#section-name)
    97  //
    98  // ... would be described as:
    99  //
   100  // ```go
   101  // Link{
   102  //   Address:      "foo.md#section-name",
   103  //   ResolvedPath: "/docroot/foo.md",
   104  //   Description:  "external link",
   105  //   Type:         externalLink,
   106  // }
   107  // ```
   108  type Link struct {
   109  	// Document this link refers to.
   110  	Doc *Doc
   111  
   112  	// Original address from document.
   113  	//
   114  	// Must be a valid Heading.LinkName.
   115  	//
   116  	// Not strictly necessary since the address is used as a hash key.
   117  	// However, storing here too makes the code simpler ;)
   118  	Address string
   119  
   120  	// The fully expanded address, without any anchor and heading suffix.
   121  	//
   122  	// Only applies to certain link types.
   123  	ResolvedPath string
   124  
   125  	// The text the user sees for the hyperlink address
   126  	Description string
   127  
   128  	Type LinkType
   129  }
   130  
   131  // Doc represents a markdown document.
   132  type Doc struct {
   133  	Logger *logrus.Entry
   134  
   135  	// Key: heading name
   136  	// Value: Heading
   137  	Headings map[string]Heading
   138  
   139  	// Key: link address
   140  	// Value: *list* of links. Required since you can have multiple links with
   141  	// the same _address_, but of a different type.
   142  	Links map[string][]Link
   143  
   144  	// Filename
   145  	Name string
   146  
   147  	// true when this document has been fully parsed
   148  	Parsed bool
   149  
   150  	// if true, only show the Table Of Contents
   151  	ShowTOC bool
   152  
   153  	ListMode bool
   154  }