Skip to content

Commit f7aac5a

Browse files
committed
refactor: replace magic numbers/strings with CommentOpen/Close constants
Add CommentOpen ("<!--") and CommentClose ("-->") constants to config and use len() instead of hardcoded 4 and 3 in insertAfterHeader. Signed-off-by: Jose Alekhinne <alekhinejose@gmail.com>
1 parent 984a1a2 commit f7aac5a

2 files changed

Lines changed: 6 additions & 3 deletions

File tree

internal/cli/add/append.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,12 @@ func insertAfterHeader(content, entry, header string) []byte {
145145
for insertPoint < len(content) {
146146
if content[insertPoint] == '\n' {
147147
insertPoint++
148-
} else if insertPoint+4 < len(content) && content[insertPoint:insertPoint+4] == "<!--" {
148+
} else if insertPoint+len(config.CommentOpen) <= len(content) &&
149+
content[insertPoint:insertPoint+len(config.CommentOpen)] == config.CommentOpen {
149150
// Skip HTML comment
150-
endComment := strings.Index(content[insertPoint:], "-->")
151+
endComment := strings.Index(content[insertPoint:], config.CommentClose)
151152
if endComment != -1 {
152-
insertPoint += endComment + 3
153+
insertPoint += endComment + len(config.CommentClose)
153154
// Skip trailing whitespace after comment
154155
for insertPoint < len(content) && (content[insertPoint] == '\n' || content[insertPoint] == ' ') {
155156
insertPoint++

internal/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package config
22

33
const (
4+
CommentClose = "-->"
5+
CommentOpen = "<!--"
46
CtxMarkerEnd = "<!-- ctx:end -->"
57
CtxMarkerStart = "<!-- ctx:context -->"
68
DirArchive = "archive"

0 commit comments

Comments
 (0)