-
-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathbacklink.nb-plugin
82 lines (69 loc) · 2.1 KB
/
backlink.nb-plugin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env bash
###############################################################################
# backlink.nb-plugin
#
# Add backlinks to notes.
#
# Depends on note-link-janitor:
# https://github.com/andymatuschak/note-link-janitor
#
# Install with:
# nb plugin install https://github.com/xwmx/nb/blob/master/plugins/backlink.nb-plugin
#
# https://github.com/xwmx/nb
###############################################################################
# Add the new subcommand name with `_subcommands add <name>`.
_subcommands add "backlink"
# Define help and usage text with `_subcommands describe <subcommand> <usage>`.
_subcommands describe "backlink" <<HEREDOC
Usage:
nb backlink [--force]
Description:
Add backlinks to notes. Crawl notes in a notebook for [[wiki-style links]]
and append a "Backlinks" section to each linked file that lists passages
referencing the note.
To link to a note from within another note, surround the title of the
target note in double square brackets:
Example with link to [[Target Note Title]] in content.
Depends on note-link-janitor:
https://github.com/andymatuschak/note-link-janitor
Requirement: every note in the notebook must have a title.
HEREDOC
# Define the subcommand as a function, named with a leading underscore.
_backlink() {
if ! hash "note-link-janitor" 2>/dev/null
then
printf "\
note-link-janitor required:
https://github.com/andymatuschak/note-link-janitor\\n" 1>&2
exit 1
fi
if ! [[ "${1:-}" == "--force" ]]
then
printf "Adding backlinks to notes. This could update multiple files.\\n"
while true
do
local __yn=
IFS='' read -r -e -d $'\n' -p "Proceed? [y/N] " __yn
case ${__yn} in
[Yy]*)
break
;;
*)
printf "Exiting...\\n"
exit 0
;;
esac
done
fi
local _notebook_path=
_notebook_path="$(_notebooks current --path)"
note-link-janitor "${_notebook_path}"
if [[ -n "$(_git status --porcelain)" ]]
then
_git checkpoint "[nb] Backlinked"
printf "Backlinked!\\n"
else
printf "No new links found.\\n"
fi
}