Skip to content

Commit

Permalink
Accept multiple <url> arguments in bookmark.
Browse files Browse the repository at this point in the history
refs gh-346
  • Loading branch information
xwmx committed Dec 2, 2024
1 parent 73dea89 commit d81c97a
Show file tree
Hide file tree
Showing 2 changed files with 296 additions and 2 deletions.
35 changes: 33 additions & 2 deletions nb
Original file line number Diff line number Diff line change
Expand Up @@ -13349,12 +13349,14 @@ _bookmark() {
local _comment=
local _edit_before_commit=0
local _encrypt=0
local _non_url_arguments=()
local _password=
local _quote=
local _readability_enabled=1
local _related_identifiers=()
local _request_page=1
local _save_source=0
local _secondary_urls=()
local _selector=
local _skip_content=0
local _skip_folder_prompt=0
Expand All @@ -13364,6 +13366,20 @@ _bookmark() {
local _title=
local _url=

local _previous_arg=

local __arg=
for __arg in "${@:-}"
do
if [[ "${_previous_arg:-}" =~ ^-- ]] ||
! _string_is_url "${__arg:-}"
then
_non_url_arguments+=("${__arg:-}")
fi

_previous_arg="${__arg:-}"
done

while ((${#}))
do
case "${1:-}" in
Expand Down Expand Up @@ -13473,10 +13489,14 @@ _bookmark() {
;;
*)
if [[ ! "${_subcommand:-}" =~ (^list$|^search$) ]] &&
[[ -z "${_url:-}" ]] &&
_string_is_url "${1}"
then
_url="${1}"
if [[ -z "${_url:-}" ]]
then
_url="${1}"
else
_secondary_urls+=("${1}")
fi
elif [[ -z "${_subcommand:-}" ]] ||
[[ "${_subcommand}" =~ (^list$|^search$) ]]
then
Expand Down Expand Up @@ -13706,6 +13726,17 @@ _bookmark() {
fi

# `bookmark <url>`
if ((${#_secondary_urls[@]}))
then
local __url=
for __url in "${_url}" "${_secondary_urls[@]}"
do
# shellcheck disable=SC2046
_bookmark "${__url}" $(printf "%s\\n" "${_non_url_arguments[@]}")
done

return 0
fi

local _temp_file=
_temp_file="$(_temp file)"
Expand Down
263 changes: 263 additions & 0 deletions test/bookmark.bats
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,269 @@ Added:\ .*[.*2.*].*\ 🔖\ .*two.bookmark.md.*\ \"Example\ Domain\" ]]

# `nb <url>` ##################################################################

@test "'nb <url1> <url2>' with --tags, --filename, and --related options creates 2 new bookmarks." {
{
"${_NB}" init

"${_NB}" notebooks add "demo"
"${_NB}" notebooks add "sample"
}

run "${_NB}" \
"${_BOOKMARK_URL}" \
"${_BOOKMARK_OG_URL}" \
--tags tag1,tag2 \
--filename "example" \
--related http://example.org \
--related sample:123 \
--related "[[demo:456]]" \
--related "Example Title"

printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"

# Returns status 0:

[[ "${status}" -eq 0 ]]

# Creates new bookmark files with content:

_files=($(ls "${NB_DIR}/home/"))

[[ "${#_files[@]}" -eq 2 ]]

[[ -f "${NB_DIR}/home/example.bookmark.md" ]]
[[ -f "${NB_DIR}/home/example-1.bookmark.md" ]]

diff \
<(cat "${NB_DIR}/home/example.bookmark.md") \
<(cat <<HEREDOC
# Example Domain
<file://${NB_TEST_BASE_PATH}/fixtures/example.com.html>
## Description
Example description.
## Related
- <http://example.org>
- [[sample:123]]
- [[demo:456]]
- [[Example Title]]
## Tags
#tag1 #tag2
## Content
$(cat "${NB_TEST_BASE_PATH}/fixtures/example.com.md")
HEREDOC
)

diff \
<(cat "${NB_DIR}/home/example-1.bookmark.md") \
<(cat <<HEREDOC
# Example OG Title
<file://${NB_TEST_BASE_PATH}/fixtures/example.com-og.html>
## Description
Example OG description.
## Related
- <http://example.org>
- [[sample:123]]
- [[demo:456]]
- [[Example Title]]
## Tags
#tag1 #tag2
## Content
$(cat "${NB_TEST_BASE_PATH}/fixtures/example.com.md")
HEREDOC
)

# Creates git commit:

cd "${NB_DIR}/home" || return 1
while [[ -n "$(git status --porcelain)" ]]
do
sleep 1
done
git log | grep -q '\[nb\] Add'

# Adds to index:

[[ -e "${NB_DIR}/home/.index" ]]

printf "ls \"\${NB_DIR}/home\": '%s'" "$(ls "${NB_DIR}/home")"
printf "cat \"\${NB_DIR}/home/.index\": '%s'" "$(cat "${NB_DIR}/home/.index")"

diff \
<(ls -r "${NB_DIR}/home") \
<(cat "${NB_DIR}/home/.index")

# Prints output:

[[ "${lines[0]}" =~ Added:\ .*[.*1.*].*\ .*example.bookmark.md ]]
[[ "${lines[1]}" =~ Added:\ .*[.*2.*].*\ .*example-1.bookmark.md ]]
}

@test "'nb <url1> <url2> <url3>' with --tags, --filename, and --related options creates 3 new bookmarks." {
{
"${_NB}" init

"${_NB}" notebooks add "demo"
"${_NB}" notebooks add "sample"
}

run "${_NB}" \
"${_BOOKMARK_URL}" \
"${_BOOKMARK_OG_URL}" \
"${_BOOKMARK_TITLES_URL}" \
--tags tag1,tag2 \
--filename "example" \
--related http://example.org \
--related sample:123 \
--related "[[demo:456]]" \
--related "Example Title"

printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"

# Returns status 0:

[[ "${status}" -eq 0 ]]

# Creates new bookmark files with content:

_files=($(ls "${NB_DIR}/home/"))

[[ "${#_files[@]}" -eq 3 ]]

[[ -f "${NB_DIR}/home/example.bookmark.md" ]]
[[ -f "${NB_DIR}/home/example-1.bookmark.md" ]]
[[ -f "${NB_DIR}/home/example-2.bookmark.md" ]]

diff \
<(cat "${NB_DIR}/home/example.bookmark.md") \
<(cat <<HEREDOC
# Example Domain
<file://${NB_TEST_BASE_PATH}/fixtures/example.com.html>
## Description
Example description.
## Related
- <http://example.org>
- [[sample:123]]
- [[demo:456]]
- [[Example Title]]
## Tags
#tag1 #tag2
## Content
$(cat "${NB_TEST_BASE_PATH}/fixtures/example.com.md")
HEREDOC
)

diff \
<(cat "${NB_DIR}/home/example-1.bookmark.md") \
<(cat <<HEREDOC
# Example OG Title
<file://${NB_TEST_BASE_PATH}/fixtures/example.com-og.html>
## Description
Example OG description.
## Related
- <http://example.org>
- [[sample:123]]
- [[demo:456]]
- [[Example Title]]
## Tags
#tag1 #tag2
## Content
$(cat "${NB_TEST_BASE_PATH}/fixtures/example.com.md")
HEREDOC
)

diff \
<(cat "${NB_DIR}/home/example-2.bookmark.md") \
<(cat <<HEREDOC
# Title One
<file://${NB_TEST_BASE_PATH}/fixtures/example.com-titles.html>
## Description
Example description.
## Related
- <http://example.org>
- [[sample:123]]
- [[demo:456]]
- [[Example Title]]
## Tags
#tag1 #tag2
## Content
$(cat "${NB_TEST_BASE_PATH}/fixtures/example.com.md")
HEREDOC
)

# Creates git commit:

cd "${NB_DIR}/home" || return 1
while [[ -n "$(git status --porcelain)" ]]
do
sleep 1
done
git log | grep -q '\[nb\] Add'

# Adds to index:

[[ -e "${NB_DIR}/home/.index" ]]

printf "ls -t -r \"\${NB_DIR}/home\": '%s'" "$(ls -t -r "${NB_DIR}/home")"
printf "cat \"\${NB_DIR}/home/.index\": '%s'" "$(cat "${NB_DIR}/home/.index")"

diff \
<(ls -t -r "${NB_DIR}/home") \
<(cat "${NB_DIR}/home/.index")

# Prints output:

[[ "${lines[0]}" =~ Added:\ .*[.*1.*].*\ .*example.bookmark.md ]]
[[ "${lines[1]}" =~ Added:\ .*[.*2.*].*\ .*example-1.bookmark.md ]]
[[ "${lines[2]}" =~ Added:\ .*[.*3.*].*\ .*example-2.bookmark.md ]]
}

@test "'nb <url>' with --tags, --filename, and --related options creates new bookmark." {
{
"${_NB}" init
Expand Down

0 comments on commit d81c97a

Please sign in to comment.