~bigbes/confluence-md-utilities

ref: 5fabfe018459f627441085dd7f87f1f9e4e97af8 confluence-md-utilities/template/embed.go -rw-r--r-- 909 bytes
5fabfe01 — Eugene Blikh feat: add verify command, improve round-trip fidelity 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
}