Skip to content

Add top level 1 heading as bookmark title suggestion #293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 28, 2022
Merged
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
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)^#[^#][\t ]*[^\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