Skip to content

Commit

Permalink
Assign notebook with selector in bookmark.
Browse files Browse the repository at this point in the history
refs gh-348
  • Loading branch information
xwmx committed Dec 27, 2024
1 parent 23ec7bf commit 5c0e19c
Show file tree
Hide file tree
Showing 2 changed files with 305 additions and 0 deletions.
11 changes: 11 additions & 0 deletions nb
Original file line number Diff line number Diff line change
Expand Up @@ -13500,6 +13500,11 @@ _bookmark() {
elif [[ -z "${_subcommand:-}" ]] ||
[[ "${_subcommand}" =~ (^list$|^search$) ]]
then
if [[ "${1:-}" =~ (:|^/) ]] && [[ -z "${_selector:-}" ]]
then
_selector="${1:-}"
fi

_arguments+=("${1}")
fi
;;
Expand Down Expand Up @@ -13726,6 +13731,7 @@ _bookmark() {
fi

# `bookmark <url>`

if ((${#_secondary_urls[@]}))
then
local __url=
Expand All @@ -13747,6 +13753,11 @@ _bookmark() {
| sed 's/https:\/\/twitter.com/https:\/\/mobile.twitter.com/g'
)"

if [[ "${_selector}" =~ (:|^/) ]]
then
_notebooks select "${_selector}"
fi

local _relative_folder_path=
local _relative_path=

Expand Down
294 changes: 294 additions & 0 deletions test/bookmark.bats
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,74 @@ HEREDOC
[[ "${lines[0]}" =~ Added:\ .*[.*Example\ Notebook:1.*].*\ .*example.bookmark.md ]]
}


@test "'nb <notebook>: <url>' with no options creates new bookmark." {
{
"${_NB}" init

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

run "${_NB}" \
"Example Notebook:" \
"${_BOOKMARK_URL}"

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

# Returns status 0:

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

# Creates new bookmark file with content:

_files=($(ls "${NB_DIR}/Example Notebook/"))

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

[[ -f "${NB_DIR}/Example Notebook/${_files[0]}" ]]

diff \
<(cat "${NB_DIR}/Example Notebook/${_files[0]}") \
<(cat <<HEREDOC
# Example Domain
<file://${NB_TEST_BASE_PATH}/fixtures/example.com.html>
## Description
Example description.
## Content
$(cat "${NB_TEST_BASE_PATH}/fixtures/example.com.md")
HEREDOC
)

# Creates git commit:

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

# Adds to index:

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

diff \
<(ls "${NB_DIR}/Example Notebook") \
<(cat "${NB_DIR}/Example Notebook/.index")

# Prints output:

[[ "${lines[0]}" =~ Added:\ .*[.*Example\ Notebook:1.*].*\ .*.bookmark.md ]]
}

# `add bookmark` ##############################################################

@test "'add bookmark' with --tags, --filename, and --related options creates new bookmark." {
Expand Down Expand Up @@ -2021,6 +2089,232 @@ $(cat "${NB_TEST_BASE_PATH}/fixtures/example.com.md")"
git log | grep -q '\[nb\] Add'
}

# `bookmark <notebook>:` ###########################################################

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

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

run "${_NB}" \
bookmark \
"Example Notebook:" \
"${_BOOKMARK_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 file with content:

[[ -f "${NB_DIR}/Example Notebook/example.bookmark.md" ]]

diff \
<(cat "${NB_DIR}/Example Notebook/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
)

# Creates git commit:

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

# Adds to index:

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

diff \
<(ls "${NB_DIR}/Example Notebook") \
<(cat "${NB_DIR}/Example Notebook/.index")

# Prints output:

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


@test "'nb bookmark <notebook>: <url>' with no options creates new bookmark." {
{
"${_NB}" init

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

run "${_NB}" \
bookmark \
"Example Notebook:" \
"${_BOOKMARK_URL}"

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

# Returns status 0:

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

# Creates new bookmark file with content:

_files=($(ls "${NB_DIR}/Example Notebook/"))

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

[[ -f "${NB_DIR}/Example Notebook/${_files[0]}" ]]

diff \
<(cat "${NB_DIR}/Example Notebook/${_files[0]}") \
<(cat <<HEREDOC
# Example Domain
<file://${NB_TEST_BASE_PATH}/fixtures/example.com.html>
## Description
Example description.
## Content
$(cat "${NB_TEST_BASE_PATH}/fixtures/example.com.md")
HEREDOC
)

# Creates git commit:

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

# Adds to index:

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

diff \
<(ls "${NB_DIR}/Example Notebook") \
<(cat "${NB_DIR}/Example Notebook/.index")

# Prints output:

[[ "${lines[0]}" =~ Added:\ .*[.*Example\ Notebook:1.*].*\ .*.bookmark.md ]]
}

@test "'nb bookmark <notebook>:<folder>/ <url>' with no options creates new bookmark." {
{
"${_NB}" init

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

run "${_NB}" \
bookmark \
"Example Notebook:Example Folder/" \
"${_BOOKMARK_URL}" <<< "y${_NEWLINE}"

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

# Returns status 0:

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

# Creates new bookmark file with content:

_files=($(ls "${NB_DIR}/Example Notebook/Example Folder/"))

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

[[ -f "${NB_DIR}/Example Notebook/Example Folder/${_files[0]}" ]]

diff \
<(cat "${NB_DIR}/Example Notebook/Example Folder/${_files[0]}") \
<(cat <<HEREDOC
# Example Domain
<file://${NB_TEST_BASE_PATH}/fixtures/example.com.html>
## Description
Example description.
## Content
$(cat "${NB_TEST_BASE_PATH}/fixtures/example.com.md")
HEREDOC
)

# Creates git commit:

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

# Adds to index:

[[ -e "${NB_DIR}/Example Notebook/.index" ]]
[[ -e "${NB_DIR}/Example Notebook/Example Folder/.index" ]]

diff \
<(ls "${NB_DIR}/Example Notebook") \
<(cat "${NB_DIR}/Example Notebook/.index")

diff \
<(ls "${NB_DIR}/Example Notebook/Example Folder") \
<(cat "${NB_DIR}/Example Notebook/Example Folder/.index")

# Prints output:

[[ "${lines[0]}" =~ Creating\ new\ folder:\ .*Example\ Notebook:Example\ Folder/ ]]
[[ "${lines[1]}" =~ Added:\ .*[.*Example\ Notebook:Example\ Folder/1.*].*\ .*.bookmark.md ]]
}

# `bookmark delete` ###########################################################

@test "'bookmark delete' deletes properly without errors." {
Expand Down

0 comments on commit 5c0e19c

Please sign in to comment.