@@ -3,8 +3,8 @@ package ansi
3
3
import (
4
4
"bytes"
5
5
"fmt"
6
+ "io"
6
7
"net/url"
7
- "slices"
8
8
9
9
xansi "github.com/charmbracelet/x/ansi"
10
10
"github.com/charmbracelet/x/exp/slice"
@@ -13,9 +13,10 @@ import (
13
13
)
14
14
15
15
type tableLink struct {
16
- href string
17
- title string
18
- content string
16
+ href string
17
+ title string
18
+ content string
19
+ linkType linkType
19
20
}
20
21
21
22
type groupedTableLinks map [string ][]tableLink
@@ -37,36 +38,35 @@ func (e *TableElement) printTableLinks(ctx RenderContext) {
37
38
w := ctx .blockStack .Current ().Block
38
39
termWidth := int (ctx .blockStack .Width (ctx )) //nolint: gosec
39
40
40
- renderLinkText := func (link tableLink , linkType linkType ) string {
41
+ renderLinkText := func (link tableLink , position int ) string {
42
+ var token string
41
43
style := ctx .options .Styles .LinkText
42
44
43
- var token string
44
- switch linkType {
45
- case linkTypeAuto :
46
- token = linkWithSuffix (link , ctx .table .groupedAutoLinks )
45
+ switch link .linkType {
46
+ case linkTypeAuto , linkTypeRegular :
47
+ token = fmt .Sprintf ("[%d]: %s" , position , link .content )
47
48
case linkTypeImage :
48
- token = linkWithSuffix ( link , ctx . table . groupedImages )
49
+ token = link . content
49
50
style = ctx .options .Styles .ImageText
50
- case linkTypeRegular :
51
- token = linkWithSuffix (link , ctx .table .groupedLinks )
51
+ style .Prefix = fmt .Sprintf ("[%d]: %s" , position , style .Prefix )
52
52
}
53
53
54
+ var b bytes.Buffer
54
55
el := & BaseElement {Token : token , Style : style }
55
- _ = el .Render (w , ctx )
56
-
57
- return token
56
+ _ = el .Render (io .MultiWriter (w , & b ), ctx )
57
+ return b .String ()
58
58
}
59
59
60
- renderLinkHref := func (link tableLink , linkType linkType , linkText string ) {
60
+ renderLinkHref := func (link tableLink , linkText string ) {
61
61
style := ctx .options .Styles .Link
62
- if linkType == linkTypeImage {
62
+ if link . linkType == linkTypeImage {
63
63
style = ctx .options .Styles .Image
64
64
}
65
65
66
66
// XXX(@andreynering): Once #411 is merged, use the hyperlink
67
67
// protocol to make the link work for the full URL even if we
68
68
// show it truncated.
69
- linkMaxWidth := max (termWidth - len (linkText )- 1 , 0 )
69
+ linkMaxWidth := max (termWidth - xansi . StringWidth (linkText )- 1 , 0 )
70
70
token := xansi .Truncate (link .href , linkMaxWidth , "…" )
71
71
72
72
el := & BaseElement {Token : token , Style : style }
@@ -77,45 +77,38 @@ func (e *TableElement) printTableLinks(ctx RenderContext) {
77
77
renderText (w , ctx .options .ColorProfile , ctx .blockStack .Current ().Style .StylePrimitive , str )
78
78
}
79
79
80
- if len (ctx .table .tableAutoLinks ) > 0 || len ( ctx . table . tableLinks ) > 0 {
80
+ if len (ctx .table .tableLinks ) > 0 {
81
81
renderString ("\n " )
82
82
}
83
- for _ , link := range ctx .table .tableAutoLinks {
83
+ for i , link := range ctx .table .tableLinks {
84
84
renderString ("\n " )
85
- linkText := renderLinkText (link , linkTypeAuto )
85
+ linkText := renderLinkText (link , i + 1 )
86
86
renderString (" " )
87
- renderLinkHref (link , linkTypeAuto , linkText )
88
- }
89
- for _ , link := range ctx .table .tableLinks {
90
- renderString ("\n " )
91
- linkText := renderLinkText (link , linkTypeRegular )
92
- renderString (" " )
93
- renderLinkHref (link , linkTypeRegular , linkText )
87
+ renderLinkHref (link , linkText )
94
88
}
95
89
96
90
if len (ctx .table .tableImages ) > 0 {
97
91
renderString ("\n " )
98
92
}
99
- for _ , image := range ctx .table .tableImages {
93
+ for i , image := range ctx .table .tableImages {
100
94
renderString ("\n " )
101
- linkText := renderLinkText (image , linkTypeImage )
95
+ linkText := renderLinkText (image , i + 1 )
102
96
renderString (" " )
103
- renderLinkHref (image , linkTypeImage , linkText )
97
+ renderLinkHref (image , linkText )
104
98
}
105
99
}
106
100
107
101
func (e * TableElement ) shouldPrintTableLinks (ctx RenderContext ) bool {
108
102
if ctx .options .InlineTableLinks {
109
103
return false
110
104
}
111
- if len (ctx .table .tableAutoLinks ) == 0 && len ( ctx . table . tableLinks ) == 0 && len (ctx .table .tableImages ) == 0 {
105
+ if len (ctx .table .tableLinks ) == 0 && len (ctx .table .tableImages ) == 0 {
112
106
return false
113
107
}
114
108
return true
115
109
}
116
110
117
111
func (e * TableElement ) collectLinksAndImages (ctx RenderContext ) error {
118
- autoLinks := make ([]tableLink , 0 )
119
112
images := make ([]tableLink , 0 )
120
113
links := make ([]tableLink , 0 )
121
114
@@ -128,19 +121,21 @@ func (e *TableElement) collectLinksAndImages(ctx RenderContext) error {
128
121
case * ast.AutoLink :
129
122
uri := string (n .URL (e .source ))
130
123
autoLink := tableLink {
131
- href : uri ,
132
- content : linkDomain (uri ),
124
+ href : uri ,
125
+ content : linkDomain (uri ),
126
+ linkType : linkTypeAuto ,
133
127
}
134
- autoLinks = append (autoLinks , autoLink )
128
+ links = append (links , autoLink )
135
129
case * ast.Image :
136
130
content , err := nodeContent (node , e .source )
137
131
if err != nil {
138
132
return ast .WalkStop , err
139
133
}
140
134
image := tableLink {
141
- href : string (n .Destination ),
142
- title : string (n .Title ),
143
- content : string (content ),
135
+ href : string (n .Destination ),
136
+ title : string (n .Title ),
137
+ content : string (content ),
138
+ linkType : linkTypeImage ,
144
139
}
145
140
if image .content == "" {
146
141
image .content = linkDomain (image .href )
@@ -152,9 +147,10 @@ func (e *TableElement) collectLinksAndImages(ctx RenderContext) error {
152
147
return ast .WalkStop , err
153
148
}
154
149
link := tableLink {
155
- href : string (n .Destination ),
156
- title : string (n .Title ),
157
- content : string (content ),
150
+ href : string (n .Destination ),
151
+ title : string (n .Title ),
152
+ content : string (content ),
153
+ linkType : linkTypeRegular ,
158
154
}
159
155
links = append (links , link )
160
156
}
@@ -165,7 +161,6 @@ func (e *TableElement) collectLinksAndImages(ctx RenderContext) error {
165
161
return fmt .Errorf ("glamour: error collecting links: %w" , err )
166
162
}
167
163
168
- ctx .table .tableAutoLinks = autoLinks
169
164
ctx .table .tableImages = images
170
165
ctx .table .tableLinks = links
171
166
return nil
@@ -174,10 +169,6 @@ func (e *TableElement) collectLinksAndImages(ctx RenderContext) error {
174
169
func (e * TableElement ) uniqAndGroupLinks (ctx RenderContext ) {
175
170
groupByContentFunc := func (l tableLink ) string { return l .content }
176
171
177
- // auto links
178
- ctx .table .tableAutoLinks = slice .Uniq (ctx .table .tableAutoLinks )
179
- ctx .table .groupedAutoLinks = slice .GroupBy (ctx .table .tableAutoLinks , groupByContentFunc )
180
-
181
172
// images
182
173
ctx .table .tableImages = slice .Uniq (ctx .table .tableImages )
183
174
ctx .table .groupedImages = slice .GroupBy (ctx .table .tableImages , groupByContentFunc )
@@ -232,12 +223,3 @@ func linkDomain(href string) string {
232
223
}
233
224
return "link"
234
225
}
235
-
236
- func linkWithSuffix (tl tableLink , grouped groupedTableLinks ) string {
237
- token := tl .content
238
- if len (grouped [token ]) < 2 {
239
- return token
240
- }
241
- index := slices .Index (grouped [token ], tl )
242
- return fmt .Sprintf ("%s[%d]" , token , index + 1 )
243
- }
0 commit comments