Skip to content

Commit

Permalink
Add top level 1 heading as bookmark title suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
amohamed11 committed Jan 26, 2022
1 parent e6ac6d8 commit 6bf3cda
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions display/bookmarks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package display

import (
"fmt"
"regexp"
"strings"

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

// Regex for extracting top level 1 heading. The title will extracted from the 1st submatch.
var topHeadingRegex = regexp.MustCompile(`(?m)^#\s?[\w\s,;'":\.]+$`)

func bkmkInit() {
panels.AddPanel(PanelBookmarks, bkmkModal, false, false)

Expand Down Expand Up @@ -159,7 +164,17 @@ func addBookmark() {
return
}
name, exists := bookmarks.Get(p.URL)

// Retrieve & use top level 1 heading for name if bookmark does not already exist.
if !exists {
match := topHeadingRegex.FindString(p.Raw)
if match != "" {
name = strings.TrimSpace(match[1:])
}
}

// Open a bookmark modal with the current name of the bookmark, if it exists
// otherwise use the top level 1 heading as a suggested name
newName, action := openBkmkModal(name, exists)

//nolint:exhaustive
Expand Down

0 comments on commit 6bf3cda

Please sign in to comment.