github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/markup/mdstripper/mdstripper_test.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package mdstripper
     7  
     8  import (
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestMarkdownStripper(t *testing.T) {
    16  	type testItem struct {
    17  		markdown      string
    18  		expectedText  []string
    19  		expectedLinks []string
    20  	}
    21  
    22  	list := []testItem{
    23  		{
    24  			`
    25  ## This is a title
    26  
    27  This is [one](link) to paradise.
    28  This **is emphasized**.
    29  This: should coalesce.
    30  
    31  ` + "```" + `
    32  This is a code block.
    33  This should not appear in the output at all.
    34  ` + "```" + `
    35  
    36  * Bullet 1
    37  * Bullet 2
    38  
    39  A HIDDEN ` + "`" + `GHOST` + "`" + ` IN THIS LINE.
    40  		`,
    41  			[]string{
    42  				"This is a title",
    43  				"This is",
    44  				"to paradise.",
    45  				"This",
    46  				"is emphasized",
    47  				".",
    48  				"This: should coalesce.",
    49  				"Bullet 1",
    50  				"Bullet 2",
    51  				"A HIDDEN",
    52  				"IN THIS LINE.",
    53  			},
    54  			[]string{
    55  				"link",
    56  			},
    57  		},
    58  		{
    59  			"Simply closes: #29 yes",
    60  			[]string{
    61  				"Simply closes: #29 yes",
    62  			},
    63  			[]string{},
    64  		},
    65  		{
    66  			"Simply closes: !29 yes",
    67  			[]string{
    68  				"Simply closes: !29 yes",
    69  			},
    70  			[]string{},
    71  		},
    72  	}
    73  
    74  	for _, test := range list {
    75  		text, links := StripMarkdown([]byte(test.markdown))
    76  		rawlines := strings.Split(text, "\n")
    77  		lines := make([]string, 0, len(rawlines))
    78  		for _, line := range rawlines {
    79  			line := strings.TrimSpace(line)
    80  			if line != "" {
    81  				lines = append(lines, line)
    82  			}
    83  		}
    84  		assert.EqualValues(t, test.expectedText, lines)
    85  		assert.EqualValues(t, test.expectedLinks, links)
    86  	}
    87  }