1
0
Fork 0

Add top level 1 heading as bookmark title suggestion (#293)

This commit is contained in:
Anas Mohamed 2022-01-28 10:18:28 -07:00 committed by GitHub
parent e6ac6d8ebf
commit 50044ddce5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 0 deletions

View File

@ -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)^#[^#][\t ]*[^\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