COMMENT
Bear TOC v. 2.3
2024-07-15 at 10:42 EDT
Makes TOC in Bear Notes:
- Input identifier from Bear
- Adds result TOC to Bear note
- or Paste TOC manually
- Option to include note title in TOC
- Create as bullet or numbered lists
- Optional with Wiki links to headings
TEXT
### _Table of Contents_

Text
VARIABLES
Set variable
TocHeading
to
Text
NUMBER
4

Number
VARIABLES
Set variable
Depth
to
Number
TEXT
Include note title = true

Text
VARIABLES
Set variable
NoteTitleInToc
to
Text
VARIABLES
Set variable
note_id
to
Shortcut Input
TEXT
Match
^[0-9A-F\-]+$
in
note_id

Matches
SCRIPTING
Count
Items
in
Matches

Count
SCRIPTING
If
Count
is
0
SCRIPTING
Show alert
Informational Message
SCRIPTING
Exit shortcut with
Result
SCRIPTING
Otherwise
SCRIPTING
End If

If Result
BEAR
Under Construction
VARIABLES
Set variable
MdText
to
Note
VARIABLES
Set variable
noteTitle
to
Note
SCRIPTING
Choose from Menu

Menu Result
SCRIPTING
Bullet TOC

Menu Result
NUMBER

Number
VARIABLES
Set variable
MakeTocLinks
to
Number
TEXT
-

Text
SCRIPTING
Numbered TOC

Menu Result
NUMBER

Number
VARIABLES
Set variable
MakeTocLinks
to
Number
TEXT
1.

Text
SCRIPTING
Bullet TOC with links

Menu Result
NUMBER
1.0

Number
VARIABLES
Set variable
MakeTocLinks
to
Number
TEXT
-

Text
SCRIPTING
Numbered TOC with links

Menu Result
NUMBER
1.0

Number
VARIABLES
Set variable
MakeTocLinks
to
Number
TEXT
1.

Text
SCRIPTING
Quit Shortcut

Menu Result
SCRIPTING
Exit shortcut with
Result
SCRIPTING
End Menu

Menu Result
VARIABLES
Set variable
ListTag
to
Menu Result
COMMENT
First make plain TOC without links:
SCRIPTING
If
NoteTitleInToc
contains
Include note title = true
TEXT
Split
MdText
by
New Lines

Split Text
SCRIPTING
Get
First Item
from
Split Text

Item from List
VARIABLES
Set variable
TocList
to
Item from List
SCRIPTING
Otherwise
SCRIPTING
End If

If Result
TEXT
Match
\n(#{1,
Depth
} .+?)\n
in
MdText

Matches
TEXT
Get
Group At Index
1
in
Matches

Text
VARIABLES
Add
Text
to
TocList
NUMBER
6.0

Number
VARIABLES
Set variable
TopMost
to
Number
SCRIPTING
Repeat with each item in
TocList

Repeat Results
DOCUMENTS
Replace
^(#{1,6}) .+
with
$1
in
Repeat Item

Updated Text
SCRIPTING
Count
Characters
in
Updated Text

Count
SCRIPTING
If
Count
is less than or equal to
TopMost
VARIABLES
Set variable
TopMost
to
Count
SCRIPTING
Otherwise
SCRIPTING
End If

If Result
SCRIPTING
End Repeat

Repeat Results
SCRIPTING
Get
First Item
from
TocList

Item from List
DOCUMENTS
Replace
^(#+) .+
with
$1
in
Item from List

Updated Text
SCRIPTING
Count
Characters
in
Updated Text

Count
SCRIPTING
If
Count
is
TopMost
VARIABLES
Set variable
TocStartLevel
to
Count
SCRIPTING
Otherwise
NUMBER
1.0

Number
VARIABLES
Set variable
TocStartLevel
to
Number
SCRIPTING
End If

If Result
TEXT
Combine
TocList
with
New Lines

Combined Text
VARIABLES
Set variable
toc
to
Combined Text
SCRIPTING
If
MakeTocLinks
is
1
COMMENT
Add wiki style tags: [[/title]]
DOCUMENTS
Replace
(#+?) (.+)
with
$1 [[/$2|$2]]
in
toc

Updated Text
VARIABLES
Set variable
toc
to
Updated Text
SCRIPTING
Otherwise
SCRIPTING
End If

If Result

Updated Text
DOCUMENTS
Replace
#
with
in
Updated Text

Updated Text
DOCUMENTS
Replace
<!-- {"fold":true} -->
with
World
in
Updated Text

Updated Text
DOCUMENTS
Replace
[*~=`]+(\b.*?\b[,.?!]?)[*~=`]+|\_+(\S.+?)\_+
with
$1$2
in
Updated Text

Updated Text
TEXT
Updated Text
TocHeading

Text
VARIABLES
Set variable
toc
to
Text
SCRIPTING
Choose from Menu

Menu Result
SCRIPTING
Add it to beginning of Note - Only use this if Title is H1!

Menu Result
VARIABLES
Get
toc
BEAR
Add to Bear Note
Note Identifier
[{'class': 'magic', 'value': 'note_id', 'glyph': 'assets/cat/Variable.svg', 'UUID': ''}]

Bear Note
SCRIPTING
Paste TOC into note manually

Menu Result
SHARING
Copy
toc
to clipboard
BEAR
Open
Note Title
Note Identifier
[{'class': 'magic', 'value': 'note_id', 'glyph': 'assets/cat/Variable.svg', 'UUID': ''}]
NOTIFICATIONS
Show notification
Input
Options Under Construction
SCRIPTING
Quit Shortcut

Menu Result
SCRIPTING
Exit shortcut with
Result
SCRIPTING
End Menu

Menu Result
SCRIPTING
Exit shortcut with
Result