From 6bf3cda3acf7f919d93d5f0e483af01f3ea7ec41 Mon Sep 17 00:00:00 2001 From: Anas Mohamed <14171441+amohamed11@users.noreply.github.com> Date: Wed, 26 Jan 2022 00:23:15 -0700 Subject: [PATCH] Add top level 1 heading as bookmark title suggestion --- display/bookmarks.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/display/bookmarks.go b/display/bookmarks.go index 2cebd96a..3e85bb91 100644 --- a/display/bookmarks.go +++ b/display/bookmarks.go @@ -2,6 +2,8 @@ package display import ( "fmt" + "regexp" + "strings" "code.rocketnine.space/tslocum/cview" "github.com/gdamore/tcell/v2" @@ -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) @@ -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