Skip to content

Commit 6bf3cda

Browse files
committed
Add top level 1 heading as bookmark title suggestion
1 parent e6ac6d8 commit 6bf3cda

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

display/bookmarks.go

+15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package display
22

33
import (
44
"fmt"
5+
"regexp"
6+
"strings"
57

68
"code.rocketnine.space/tslocum/cview"
79
"github.com/gdamore/tcell/v2"
@@ -28,6 +30,9 @@ const (
2830
var bkmkCh = make(chan bkmkAction)
2931
var bkmkModalText string // The current text of the input field in the modal
3032

33+
// Regex for extracting top level 1 heading. The title will extracted from the 1st submatch.
34+
var topHeadingRegex = regexp.MustCompile(`(?m)^#\s?[\w\s,;'":\.]+$`)
35+
3136
func bkmkInit() {
3237
panels.AddPanel(PanelBookmarks, bkmkModal, false, false)
3338

@@ -159,7 +164,17 @@ func addBookmark() {
159164
return
160165
}
161166
name, exists := bookmarks.Get(p.URL)
167+
168+
// Retrieve & use top level 1 heading for name if bookmark does not already exist.
169+
if !exists {
170+
match := topHeadingRegex.FindString(p.Raw)
171+
if match != "" {
172+
name = strings.TrimSpace(match[1:])
173+
}
174+
}
175+
162176
// Open a bookmark modal with the current name of the bookmark, if it exists
177+
// otherwise use the top level 1 heading as a suggested name
163178
newName, action := openBkmkModal(name, exists)
164179

165180
//nolint:exhaustive

0 commit comments

Comments
 (0)