@@ -5,9 +5,9 @@ package markup
5
5
6
6
import (
7
7
"bytes"
8
+ "fmt"
8
9
"io"
9
10
"regexp"
10
- "slices"
11
11
"strings"
12
12
"sync"
13
13
@@ -133,75 +133,49 @@ func CustomLinkURLSchemes(schemes []string) {
133
133
common .GlobalVars ().LinkRegex , _ = xurls .StrictMatchingScheme (strings .Join (withAuth , "|" ))
134
134
}
135
135
136
- type postProcessError struct {
137
- context string
138
- err error
139
- }
140
-
141
- func (p * postProcessError ) Error () string {
142
- return "PostProcess: " + p .context + ", " + p .err .Error ()
143
- }
144
-
145
136
type processor func (ctx * RenderContext , node * html.Node )
146
137
147
- var defaultProcessors = []processor {
148
- fullIssuePatternProcessor ,
149
- comparePatternProcessor ,
150
- codePreviewPatternProcessor ,
151
- fullHashPatternProcessor ,
152
- shortLinkProcessor ,
153
- linkProcessor ,
154
- mentionProcessor ,
155
- issueIndexPatternProcessor ,
156
- commitCrossReferencePatternProcessor ,
157
- hashCurrentPatternProcessor ,
158
- emailAddressProcessor ,
159
- emojiProcessor ,
160
- emojiShortCodeProcessor ,
161
- }
162
-
163
- // PostProcess does the final required transformations to the passed raw HTML
138
+ // PostProcessDefault does the final required transformations to the passed raw HTML
164
139
// data, and ensures its validity. Transformations include: replacing links and
165
140
// emails with HTML links, parsing shortlinks in the format of [[Link]], like
166
141
// MediaWiki, linking issues in the format #ID, and mentions in the format
167
142
// @user, and others.
168
- func PostProcess (ctx * RenderContext , input io.Reader , output io.Writer ) error {
169
- return postProcess (ctx , defaultProcessors , input , output )
170
- }
171
-
172
- var commitMessageProcessors = []processor {
173
- fullIssuePatternProcessor ,
174
- comparePatternProcessor ,
175
- fullHashPatternProcessor ,
176
- linkProcessor ,
177
- mentionProcessor ,
178
- issueIndexPatternProcessor ,
179
- commitCrossReferencePatternProcessor ,
180
- hashCurrentPatternProcessor ,
181
- emailAddressProcessor ,
182
- emojiProcessor ,
183
- emojiShortCodeProcessor ,
143
+ func PostProcessDefault (ctx * RenderContext , input io.Reader , output io.Writer ) error {
144
+ procs := []processor {
145
+ fullIssuePatternProcessor ,
146
+ comparePatternProcessor ,
147
+ codePreviewPatternProcessor ,
148
+ fullHashPatternProcessor ,
149
+ shortLinkProcessor ,
150
+ linkProcessor ,
151
+ mentionProcessor ,
152
+ issueIndexPatternProcessor ,
153
+ commitCrossReferencePatternProcessor ,
154
+ hashCurrentPatternProcessor ,
155
+ emailAddressProcessor ,
156
+ emojiProcessor ,
157
+ emojiShortCodeProcessor ,
158
+ }
159
+ return postProcess (ctx , procs , input , output )
184
160
}
185
161
186
162
// RenderCommitMessage will use the same logic as PostProcess, but will disable
187
- // the shortLinkProcessor and will add a defaultLinkProcessor if defaultLink is
188
- // set, which changes every text node into a link to the passed default link.
163
+ // the shortLinkProcessor.
189
164
func RenderCommitMessage (ctx * RenderContext , content string ) (string , error ) {
190
- procs := commitMessageProcessors
191
- return renderProcessString (ctx , procs , content )
192
- }
193
-
194
- var commitMessageSubjectProcessors = []processor {
195
- fullIssuePatternProcessor ,
196
- comparePatternProcessor ,
197
- fullHashPatternProcessor ,
198
- linkProcessor ,
199
- mentionProcessor ,
200
- issueIndexPatternProcessor ,
201
- commitCrossReferencePatternProcessor ,
202
- hashCurrentPatternProcessor ,
203
- emojiShortCodeProcessor ,
204
- emojiProcessor ,
165
+ procs := []processor {
166
+ fullIssuePatternProcessor ,
167
+ comparePatternProcessor ,
168
+ fullHashPatternProcessor ,
169
+ linkProcessor ,
170
+ mentionProcessor ,
171
+ issueIndexPatternProcessor ,
172
+ commitCrossReferencePatternProcessor ,
173
+ hashCurrentPatternProcessor ,
174
+ emailAddressProcessor ,
175
+ emojiProcessor ,
176
+ emojiShortCodeProcessor ,
177
+ }
178
+ return postProcessString (ctx , procs , content )
205
179
}
206
180
207
181
var emojiProcessors = []processor {
@@ -214,7 +188,18 @@ var emojiProcessors = []processor{
214
188
// emailAddressProcessor, will add a defaultLinkProcessor if defaultLink is set,
215
189
// which changes every text node into a link to the passed default link.
216
190
func RenderCommitMessageSubject (ctx * RenderContext , defaultLink , content string ) (string , error ) {
217
- procs := slices .Clone (commitMessageSubjectProcessors )
191
+ procs := []processor {
192
+ fullIssuePatternProcessor ,
193
+ comparePatternProcessor ,
194
+ fullHashPatternProcessor ,
195
+ linkProcessor ,
196
+ mentionProcessor ,
197
+ issueIndexPatternProcessor ,
198
+ commitCrossReferencePatternProcessor ,
199
+ hashCurrentPatternProcessor ,
200
+ emojiShortCodeProcessor ,
201
+ emojiProcessor ,
202
+ }
218
203
procs = append (procs , func (ctx * RenderContext , node * html.Node ) {
219
204
ch := & html.Node {Parent : node , Type : html .TextNode , Data : node .Data }
220
205
node .Type = html .ElementNode
@@ -223,19 +208,19 @@ func RenderCommitMessageSubject(ctx *RenderContext, defaultLink, content string)
223
208
node .Attr = []html.Attribute {{Key : "href" , Val : defaultLink }, {Key : "class" , Val : "muted" }}
224
209
node .FirstChild , node .LastChild = ch , ch
225
210
})
226
- return renderProcessString (ctx , procs , content )
211
+ return postProcessString (ctx , procs , content )
227
212
}
228
213
229
214
// RenderIssueTitle to process title on individual issue/pull page
230
215
func RenderIssueTitle (ctx * RenderContext , title string ) (string , error ) {
231
216
// do not render other issue/commit links in an issue's title - which in most cases is already a link.
232
- return renderProcessString (ctx , []processor {
217
+ return postProcessString (ctx , []processor {
233
218
emojiShortCodeProcessor ,
234
219
emojiProcessor ,
235
220
}, title )
236
221
}
237
222
238
- func renderProcessString (ctx * RenderContext , procs []processor , content string ) (string , error ) {
223
+ func postProcessString (ctx * RenderContext , procs []processor , content string ) (string , error ) {
239
224
var buf strings.Builder
240
225
if err := postProcess (ctx , procs , strings .NewReader (content ), & buf ); err != nil {
241
226
return "" , err
@@ -246,7 +231,7 @@ func renderProcessString(ctx *RenderContext, procs []processor, content string)
246
231
// RenderDescriptionHTML will use similar logic as PostProcess, but will
247
232
// use a single special linkProcessor.
248
233
func RenderDescriptionHTML (ctx * RenderContext , content string ) (string , error ) {
249
- return renderProcessString (ctx , []processor {
234
+ return postProcessString (ctx , []processor {
250
235
descriptionLinkProcessor ,
251
236
emojiShortCodeProcessor ,
252
237
emojiProcessor ,
@@ -256,7 +241,7 @@ func RenderDescriptionHTML(ctx *RenderContext, content string) (string, error) {
256
241
// RenderEmoji for when we want to just process emoji and shortcodes
257
242
// in various places it isn't already run through the normal markdown processor
258
243
func RenderEmoji (ctx * RenderContext , content string ) (string , error ) {
259
- return renderProcessString (ctx , emojiProcessors , content )
244
+ return postProcessString (ctx , emojiProcessors , content )
260
245
}
261
246
262
247
func postProcess (ctx * RenderContext , procs []processor , input io.Reader , output io.Writer ) error {
@@ -276,7 +261,7 @@ func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output
276
261
strings .NewReader ("</body></html>" ),
277
262
))
278
263
if err != nil {
279
- return & postProcessError { " invalid HTML" , err }
264
+ return fmt . Errorf ( "markup.postProcess: invalid HTML: %w " , err )
280
265
}
281
266
282
267
if node .Type == html .DocumentNode {
@@ -308,7 +293,7 @@ func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output
308
293
// Render everything to buf.
309
294
for _ , node := range newNodes {
310
295
if err := html .Render (output , node ); err != nil {
311
- return & postProcessError { "error rendering processed HTML " , err }
296
+ return fmt . Errorf ( "markup.postProcess: html.Render: %w " , err )
312
297
}
313
298
}
314
299
return nil
0 commit comments