~bigbes/confluence-md-utilities

ref: v0.1.0 confluence-md-utilities/template/embed.go -rw-r--r-- 909 bytes
e0e81bc6 — Eugene Blikh chore: rename module to go.bigb.es/confluence-md-utilities a month 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
}