~bigbes/confluence-md-utilities

ref: fb67f6989dc2cabe60498e0e7b8f7a51e1e9826b confluence-md-utilities/confluence/renderer.go -rw-r--r-- 13.2 KiB
fb67f698 — Eugene Blikh Initial commit: mdcx — Markdown to Confluence XML converter 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package confluence

import (
	"bytes"
	"fmt"
	"html"
	"strings"

	"github.com/yuin/goldmark/ast"
	east "github.com/yuin/goldmark/extension/ast"
	"github.com/yuin/goldmark/renderer"
	"github.com/yuin/goldmark/util"
)

// Renderer renders goldmark AST nodes into Confluence storage format XML.
type Renderer struct {
	taskIDCounter      int
	inTaskBody         bool
	inlineCommentDepth int
}

// NewRenderer creates a new Confluence storage format renderer.
func NewRenderer() renderer.NodeRenderer {
	return &Renderer{}
}

func (r *Renderer) nextTaskID() int {
	r.taskIDCounter++
	return r.taskIDCounter
}

// RegisterFuncs implements renderer.NodeRenderer.
func (r *Renderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
	// Block nodes
	reg.Register(ast.KindDocument, r.renderDocument)
	reg.Register(ast.KindHeading, r.renderHeading)
	reg.Register(ast.KindParagraph, r.renderParagraph)
	reg.Register(ast.KindTextBlock, r.renderTextBlock)
	reg.Register(ast.KindFencedCodeBlock, r.renderFencedCodeBlock)
	reg.Register(ast.KindCodeBlock, r.renderCodeBlock)
	reg.Register(ast.KindThematicBreak, r.renderThematicBreak)
	reg.Register(ast.KindBlockquote, r.renderBlockquote)
	reg.Register(ast.KindList, r.renderList)
	reg.Register(ast.KindListItem, r.renderListItem)
	reg.Register(ast.KindHTMLBlock, r.renderHTMLBlock)

	// Inline nodes
	reg.Register(ast.KindText, r.renderText)
	reg.Register(ast.KindString, r.renderString)
	reg.Register(ast.KindEmphasis, r.renderEmphasis)
	reg.Register(ast.KindCodeSpan, r.renderCodeSpan)
	reg.Register(ast.KindLink, r.renderLink)
	reg.Register(ast.KindAutoLink, r.renderAutoLink)
	reg.Register(ast.KindImage, r.renderImage)
	reg.Register(ast.KindRawHTML, r.renderRawHTML)

	// GFM extensions
	reg.Register(east.KindTable, r.renderTable)
	reg.Register(east.KindTableHeader, r.renderTableHeader)
	// Note: goldmark GFM has no KindTableBody
	reg.Register(east.KindTableRow, r.renderTableRow)
	reg.Register(east.KindTableCell, r.renderTableCell)
	reg.Register(east.KindStrikethrough, r.renderStrikethrough)
	reg.Register(east.KindTaskCheckBox, r.renderTaskCheckBox)
}

func (r *Renderer) renderDocument(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	return ast.WalkContinue, nil
}

func (r *Renderer) renderHeading(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	n := node.(*ast.Heading)
	tag := fmt.Sprintf("h%d", n.Level)
	if entering {
		fmt.Fprintf(w, "<%s>", tag)
	} else {
		fmt.Fprintf(w, "</%s>\n", tag)
	}
	return ast.WalkContinue, nil
}

func (r *Renderer) renderParagraph(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	// Inside task items, don't wrap in <p> - the task-body handles it
	if r.inTaskBody {
		if !entering {
			w.WriteString("</ac:task-body>\n")
			r.inTaskBody = false
		}
		return ast.WalkContinue, nil
	}
	if entering {
		w.WriteString("<p>")
	} else {
		w.WriteString("</p>\n")
	}
	return ast.WalkContinue, nil
}

func (r *Renderer) renderTextBlock(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	if !entering {
		if r.inTaskBody {
			w.WriteString("</ac:task-body>\n")
			r.inTaskBody = false
		} else {
			w.WriteString("\n")
		}
	}
	return ast.WalkContinue, nil
}

func (r *Renderer) renderFencedCodeBlock(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	if !entering {
		return ast.WalkContinue, nil
	}
	n := node.(*ast.FencedCodeBlock)
	language := ""
	if n.Info != nil {
		lang := n.Info.Segment.Value(source)
		// Take first word only (e.g., "go title=foo" -> "go")
		if idx := bytes.IndexByte(lang, ' '); idx > 0 {
			lang = lang[:idx]
		}
		language = string(lang)
	}

	var buf bytes.Buffer
	for i := 0; i < n.Lines().Len(); i++ {
		line := n.Lines().At(i)
		buf.Write(line.Value(source))
	}
	// Remove trailing newline from code content
	code := strings.TrimRight(buf.String(), "\n")

	w.WriteString(CodeMacro(language, code))
	w.WriteString("\n")
	return ast.WalkSkipChildren, nil
}

func (r *Renderer) renderCodeBlock(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	if !entering {
		return ast.WalkContinue, nil
	}
	n := node.(*ast.CodeBlock)
	var buf bytes.Buffer
	for i := 0; i < n.Lines().Len(); i++ {
		line := n.Lines().At(i)
		buf.Write(line.Value(source))
	}
	code := strings.TrimRight(buf.String(), "\n")
	w.WriteString(CodeMacro("", code))
	w.WriteString("\n")
	return ast.WalkSkipChildren, nil
}

func (r *Renderer) renderThematicBreak(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	if entering {
		w.WriteString("<hr/>\n")
	}
	return ast.WalkContinue, nil
}

func (r *Renderer) renderBlockquote(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	if entering {
		w.WriteString(`<ac:structured-macro ac:name="info" ac:schema-version="1"><ac:rich-text-body>`)
	} else {
		w.WriteString(`</ac:rich-text-body></ac:structured-macro>` + "\n")
	}
	return ast.WalkContinue, nil
}

func (r *Renderer) renderList(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	n := node.(*ast.List)
	// Check if this is a task list (first item has a TaskCheckBox child)
	if isTaskList(n) {
		if entering {
			w.WriteString("<ac:task-list>\n")
		} else {
			w.WriteString("</ac:task-list>\n")
		}
		return ast.WalkContinue, nil
	}
	tag := "ul"
	if n.IsOrdered() {
		tag = "ol"
	}
	if entering {
		fmt.Fprintf(w, "<%s>\n", tag)
	} else {
		fmt.Fprintf(w, "</%s>\n", tag)
	}
	return ast.WalkContinue, nil
}

// isTaskList checks if a list node contains task checkbox items.
func isTaskList(n *ast.List) bool {
	for child := n.FirstChild(); child != nil; child = child.NextSibling() {
		if li, ok := child.(*ast.ListItem); ok {
			for c := li.FirstChild(); c != nil; c = c.NextSibling() {
				if hasCheckbox(c) {
					return true
				}
			}
		}
	}
	return false
}

func hasCheckbox(n ast.Node) bool {
	for c := n.FirstChild(); c != nil; c = c.NextSibling() {
		if c.Kind() == east.KindTaskCheckBox {
			return true
		}
	}
	return false
}

func (r *Renderer) renderListItem(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	// Check if parent is a task list
	if parent, ok := node.Parent().(*ast.List); ok && isTaskList(parent) {
		if entering {
			w.WriteString("<ac:task>\n")
			fmt.Fprintf(w, "<ac:task-id>%d</ac:task-id>\n", r.nextTaskID())
		} else {
			w.WriteString("</ac:task>\n")
		}
		return ast.WalkContinue, nil
	}
	if entering {
		w.WriteString("<li>")
	} else {
		w.WriteString("</li>\n")
	}
	return ast.WalkContinue, nil
}

func (r *Renderer) renderHTMLBlock(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	if !entering {
		return ast.WalkContinue, nil
	}
	n := node.(*ast.HTMLBlock)
	for i := 0; i < n.Lines().Len(); i++ {
		line := n.Lines().At(i)
		raw := strings.TrimRight(string(line.Value(source)), "\n")
		w.WriteString(r.convertRawSpan(raw))
		w.WriteString("\n")
	}
	return ast.WalkSkipChildren, nil
}

func (r *Renderer) renderText(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	if !entering {
		return ast.WalkContinue, nil
	}
	n := node.(*ast.Text)
	w.WriteString(html.EscapeString(string(n.Segment.Value(source))))
	if n.HardLineBreak() {
		w.WriteString("<br/>")
	} else if n.SoftLineBreak() {
		w.WriteString("\n")
	}
	return ast.WalkContinue, nil
}

func (r *Renderer) renderString(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	if !entering {
		return ast.WalkContinue, nil
	}
	n := node.(*ast.String)
	w.Write(n.Value)
	return ast.WalkContinue, nil
}

func (r *Renderer) renderEmphasis(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	n := node.(*ast.Emphasis)
	tag := "em"
	if n.Level == 2 {
		tag = "strong"
	}
	if entering {
		fmt.Fprintf(w, "<%s>", tag)
	} else {
		fmt.Fprintf(w, "</%s>", tag)
	}
	return ast.WalkContinue, nil
}

func (r *Renderer) renderCodeSpan(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	if entering {
		w.WriteString("<code>")
	} else {
		w.WriteString("</code>")
	}
	return ast.WalkContinue, nil
}

func (r *Renderer) renderLink(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	n := node.(*ast.Link)
	if entering {
		fmt.Fprintf(w, `<a href="%s">`, html.EscapeString(string(n.Destination)))
	} else {
		w.WriteString("</a>")
	}
	return ast.WalkContinue, nil
}

func (r *Renderer) renderAutoLink(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	n := node.(*ast.AutoLink)
	if entering {
		url := string(n.URL(source))
		fmt.Fprintf(w, `<a href="%s">%s</a>`, html.EscapeString(url), html.EscapeString(url))
	}
	return ast.WalkContinue, nil
}

func (r *Renderer) renderImage(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	if !entering {
		return ast.WalkContinue, nil
	}
	n := node.(*ast.Image)
	url := string(n.Destination)
	alt := nodeText(n, source)
	if alt != "" {
		fmt.Fprintf(w, `<ac:image ac:alt="%s"><ri:url ri:value="%s"/></ac:image>`,
			html.EscapeString(alt), html.EscapeString(url))
	} else {
		fmt.Fprintf(w, `<ac:image><ri:url ri:value="%s"/></ac:image>`, html.EscapeString(url))
	}
	return ast.WalkSkipChildren, nil
}

func (r *Renderer) renderRawHTML(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	if !entering {
		return ast.WalkContinue, nil
	}
	n := node.(*ast.RawHTML)
	for i := 0; i < n.Segments.Len(); i++ {
		seg := n.Segments.At(i)
		raw := string(seg.Value(source))
		w.WriteString(r.convertRawSpan(raw))
	}
	return ast.WalkContinue, nil
}

// convertRawSpan converts round-trip spans back to Confluence XML.
func (r *Renderer) convertRawSpan(raw string) string {
	if !strings.HasPrefix(raw, "<span ") {
		// Handle closing tag — convert back if we're inside an inline comment
		if raw == "</span>" && r.inlineCommentDepth > 0 {
			r.inlineCommentDepth--
			return "</ac:inline-comment-marker>"
		}
		return raw
	}

	// <span data-inline-comment="ref">
	if strings.Contains(raw, "data-inline-comment=") {
		ref := extractAttrValue(raw, "data-inline-comment")
		if ref != "" {
			r.inlineCommentDepth++
			return `<ac:inline-comment-marker ac:ref="` + ref + `">`
		}
	}

	// <span data-user-key="key"/>
	if strings.Contains(raw, "data-user-key=") {
		key := extractAttrValue(raw, "data-user-key")
		if key != "" {
			return `<ac:link><ri:user ri:userkey="` + key + `"/></ac:link>`
		}
	}

	// <span data-attachment="filename"/>
	if strings.Contains(raw, "data-attachment=") {
		filename := extractAttrValue(raw, "data-attachment")
		if filename != "" {
			alt := extractAttrValue(raw, "data-alt")
			if alt != "" {
				return `<ac:image ac:alt="` + alt + `"><ri:attachment ri:filename="` + filename + `"/></ac:image>`
			}
			return `<ac:image><ri:attachment ri:filename="` + filename + `"/></ac:image>`
		}
	}

	return raw
}

func extractAttrValue(tag, attr string) string {
	key := attr + `="`
	idx := strings.Index(tag, key)
	if idx == -1 {
		return ""
	}
	start := idx + len(key)
	end := strings.Index(tag[start:], `"`)
	if end == -1 {
		return ""
	}
	return tag[start : start+end]
}

// GFM Table support

func (r *Renderer) renderTable(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	if entering {
		w.WriteString("<table>\n<tbody>\n")
	} else {
		w.WriteString("</tbody>\n</table>\n")
	}
	return ast.WalkContinue, nil
}

func (r *Renderer) renderTableHeader(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	if entering {
		w.WriteString("<tr>\n")
	} else {
		w.WriteString("</tr>\n")
	}
	return ast.WalkContinue, nil
}

func (r *Renderer) renderTableRow(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	if entering {
		w.WriteString("<tr>\n")
	} else {
		w.WriteString("</tr>\n")
	}
	return ast.WalkContinue, nil
}

func (r *Renderer) renderTableCell(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	n := node.(*east.TableCell)
	tag := "td"
	if n.Parent().Kind() == east.KindTableHeader {
		tag = "th"
	}
	if entering {
		fmt.Fprintf(w, "<%s><p>", tag)
	} else {
		fmt.Fprintf(w, "</p></%s>\n", tag)
	}
	return ast.WalkContinue, nil
}

func (r *Renderer) renderStrikethrough(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	if entering {
		w.WriteString("<del>")
	} else {
		w.WriteString("</del>")
	}
	return ast.WalkContinue, nil
}

func (r *Renderer) renderTaskCheckBox(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	if !entering {
		return ast.WalkContinue, nil
	}
	n := node.(*east.TaskCheckBox)
	if n.IsChecked {
		w.WriteString("<ac:task-status>complete</ac:task-status>\n")
	} else {
		w.WriteString("<ac:task-status>incomplete</ac:task-status>\n")
	}
	w.WriteString("<ac:task-body>")
	r.inTaskBody = true
	return ast.WalkContinue, nil
}

// nodeText extracts plain text from a node tree.
func nodeText(n ast.Node, source []byte) string {
	var buf bytes.Buffer
	for c := n.FirstChild(); c != nil; c = c.NextSibling() {
		if t, ok := c.(*ast.Text); ok {
			buf.Write(t.Segment.Value(source))
		}
	}
	return buf.String()
}