~bigbes/confluence-md-utilities

ref: fb67f6989dc2cabe60498e0e7b8f7a51e1e9826b confluence-md-utilities/template/embed.go -rw-r--r-- 909 bytes
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
package template

import (
	"fmt"
	"strings"
)

// Embed inserts content between marker comments in a template.
// The markers should be XML comments like <!-- MD_CONTENT_START --> and <!-- MD_CONTENT_END -->.
func Embed(templateXML, content, markerStart, markerEnd string) (string, error) {
	startIdx := strings.Index(templateXML, markerStart)
	if startIdx == -1 {
		return "", fmt.Errorf("start marker %q not found in template", markerStart)
	}
	endIdx := strings.Index(templateXML, markerEnd)
	if endIdx == -1 {
		return "", fmt.Errorf("end marker %q not found in template", markerEnd)
	}
	if endIdx < startIdx {
		return "", fmt.Errorf("end marker appears before start marker")
	}

	var buf strings.Builder
	buf.WriteString(templateXML[:startIdx+len(markerStart)])
	buf.WriteString("\n")
	buf.WriteString(content)
	buf.WriteString("\n")
	buf.WriteString(templateXML[endIdx:])

	return buf.String(), nil
}