~bigbes/confluence-md-utilities

ref: 0d1931030f4fbc30627156eb04663d7754552433 confluence-md-utilities/confluence/elements.go -rw-r--r-- 1.8 KiB
0d193103 — Eugene Blikh feat: add ability for local install 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
package confluence

// Confluence storage format macro helpers.

func CodeMacro(language string, body string) string {
	return CodeMacroWithID(language, body, "")
}

func CodeMacroWithID(language string, body string, macroID string) string {
	var lang string
	if language != "" {
		lang = `<ac:parameter ac:name="language">` + language + `</ac:parameter>`
	}
	tag := `<ac:structured-macro ac:name="code" ac:schema-version="1">`
	if macroID != "" {
		tag = `<ac:structured-macro ac:macro-id="` + macroID + `" ac:name="code" ac:schema-version="1">`
	}
	return tag +
		lang +
		`<ac:plain-text-body><![CDATA[` + escapeCDATA(body) + `]]></ac:plain-text-body>` +
		`</ac:structured-macro>`
}

func InfoPanel(body string) string {
	return `<ac:structured-macro ac:name="info" ac:schema-version="1">` +
		`<ac:rich-text-body>` + body + `</ac:rich-text-body>` +
		`</ac:structured-macro>`
}

func NotePanel(body string) string {
	return `<ac:structured-macro ac:name="note" ac:schema-version="1">` +
		`<ac:rich-text-body>` + body + `</ac:rich-text-body>` +
		`</ac:structured-macro>`
}

func WarningPanel(body string) string {
	return `<ac:structured-macro ac:name="warning" ac:schema-version="1">` +
		`<ac:rich-text-body>` + body + `</ac:rich-text-body>` +
		`</ac:structured-macro>`
}

func ImageExternal(url string) string {
	return `<ac:image><ri:url ri:value="` + url + `"/></ac:image>`
}

// escapeCDATA splits ]]> sequences so they don't break CDATA sections.
func escapeCDATA(s string) string {
	result := make([]byte, 0, len(s))
	for i := 0; i < len(s); i++ {
		if i+2 < len(s) && s[i] == ']' && s[i+1] == ']' && s[i+2] == '>' {
			result = append(result, ']', ']', '>', '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')
			i += 2
		} else {
			result = append(result, s[i])
		}
	}
	return string(result)
}