Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions src/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,18 +359,26 @@ func insertNavLink(str string, appNameArray string) string {
1)
}

// Global Navbar <= 1.2.45
globalNavBarMatch := utils.FindMatch(str, `(,[a-zA-Z_\$][\w\$]*===(?:[a-zA-Z_\$][\w\$]*\.){2}HOME_NEXT_TO_NAVIGATION&&.+?)\]`)
utils.ReplaceOnce(&str, `(,[a-zA-Z_\$][\w\$]*===(?:[a-zA-Z_\$][\w\$]*\.){2}HOME_NEXT_TO_NAVIGATION&&.+?)\]`, func(submatches ...string) string {
return fmt.Sprintf("%s,Spicetify._renderNavLinks([%s], true)]", submatches[1], appNameArray)
})

// Global Navbar >= 1.2.46
if len(globalNavBarMatch) == 0 {
utils.ReplaceOnce(&str, `("global-nav-bar".*?)(\(0,\s*[a-zA-Z_\$][\w\$]*\.jsx\))(\(\s*\w+,\s*\{\s*className:\w*\s*\}\s*\))`, func(submatches ...string) string {
return fmt.Sprintf("%s[%s%s,Spicetify._renderNavLinks([%s], true)].flat()", submatches[1], submatches[2], submatches[3], appNameArray)
})
}
utils.ReplaceOnceWithPriority(&str,
[]string{
// Global Navbar <= 1.2.45
`(,[a-zA-Z_\$][\w\$]*===(?:[a-zA-Z_\$][\w\$]*\.){2}HOME_NEXT_TO_NAVIGATION&&.+?)\]`,
// Global Navbar >= 1.2.60, greedy matching with enclosing brackets
`("global-nav-bar".*[[\w\$&|]*\(0,[a-zA-Z_\$][\w\$]*\.jsx\)\(\s*\w+,\s*\{\s*className:\w*\s*\}\s*\))\]`,
// Global Navbar >= 1.2.46, lazy matching
`("global-nav-bar".*?)(\(0,\s*[a-zA-Z_\$][\w\$]*\.jsx\))(\(\s*\w+,\s*\{\s*className:\w*\s*\}\s*\))`,
},
func(index int, submatches ...string) string {
switch index {
case 0:
case 1:
return fmt.Sprintf("%s,Spicetify._renderNavLinks([%s], true)]", submatches[1], appNameArray)
case 2:
return fmt.Sprintf("%s[%s%s,Spicetify._renderNavLinks([%s], true)].flat()", submatches[1], submatches[2], submatches[3], appNameArray)
}
return ""
},
)

return str
}
Expand Down
20 changes: 20 additions & 0 deletions src/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,26 @@ func ReplaceOnce(str *string, pattern string, repl func(submatches ...string) st
})
}

func ReplaceOnceWithPriority(str *string, patterns []string, repl func(index int, submatches ...string) string) {
for i, pattern := range patterns {
re := regexp.MustCompile(pattern)
firstMatch := true
*str = re.ReplaceAllStringFunc(*str, func(match string) string {
if firstMatch {
firstMatch = false
submatches := re.FindStringSubmatch(match)
if submatches != nil {
return repl(i, submatches...)
}
}
return match
})
if !firstMatch {
break
}
}
}

func FindMatch(input string, regexpTerm string) [][]string {
re := regexp.MustCompile(regexpTerm)
matches := re.FindAllStringSubmatch(input, -1)
Expand Down