Skip to content

Commit

Permalink
add "prefer_zettel_id_links" option
Browse files Browse the repository at this point in the history
  • Loading branch information
pryley committed Jul 21, 2020
1 parent 0573a07 commit 33beb0e
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 8 deletions.
Binary file modified Alfred.pyc
Binary file not shown.
10 changes: 10 additions & 0 deletions Notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from Alfred import Tools
from collections import Counter, OrderedDict
from QuerySplitter import QuerySplitter
from unicodedata import normalize
import os
import re
Expand Down Expand Up @@ -47,6 +48,7 @@ def __init__(self):
self.exact_match = True if os.getenv('exact_match') == 'True' else False
self.path = Tools.getNotesPath()
self.prefer_filename_to_title = True if os.getenv('prefer_filename_to_title') == 'True' else False
self.prefer_zettel_id_links = True if os.getenv('prefer_zettel_id_links') == 'True' else False
self.search_content = True if os.getenv('search_content') == 'True' else False
self.search_yaml_tags_only = True if os.getenv('search_yaml_tags_only') == 'True' else False
self.template_tag = os.getenv('template_tag')
Expand Down Expand Up @@ -212,6 +214,14 @@ def getNoteFilename(self, file_path):
file_basename = os.path.basename(file_path)
return file_basename.rsplit('.', 1)[0]

def getNoteLinkTitle(self, path):
title = self.getNoteTitle(path)
if self.prefer_zettel_id_links:
qs = QuerySplitter(title)
if qs.zettel_id:
title = qs.zettel_id
return title

def getNoteTitle(self, path):
content = self._getFileContent(path)
title = self.getNoteFilename(path)
Expand Down
Binary file modified Notes.pyc
Binary file not shown.
Binary file modified Plist.pyc
Binary file not shown.
Binary file modified QuerySplitter.pyc
Binary file not shown.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ When set to `True`, the note filename will be used.
When set to `False`, the note title (if entered as `# Note Title`) will be used if it exists instead of the filename.
### Prefer Zettel ID Links (`prefer_zettel_id_links`)
> Default value: `False`
When set to `True`, the Zettel ID (if one exists) will be used as the link title in markdown and wiki links (i.e. `[[202006131200]]`).
When set to `False`, the full note title will be used in markdown and wiki links (i.e. `[[202006131200 The Note Title]]`).
### Search Content (`search_content`)
> Default value: `False`
Expand Down
7 changes: 7 additions & 0 deletions docs/prefer_zettel_id_links.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### Prefer Zettel ID Links (`prefer_zettel_id_links`)

> Default value: `False`
When set to `True`, the Zettel ID (if one exists) will be used as the link title in markdown and wiki links (i.e. `[[202006131200]]`).

When set to `False`, the full note title will be used in markdown and wiki links (i.e. `[[202006131200 The Note Title]]`).
2 changes: 1 addition & 1 deletion get_zettel_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sys

query = Tools.getArgv(1)
title = Search().getNoteTitle(query).strip()
title = Search().getNoteLinkTitle(query).strip()
output = '[[' + title + ']]'

sys.stdout.write(output)
89 changes: 84 additions & 5 deletions info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,8 @@
<dict>
<key>argument</key>
<string></string>
<key>passthroughargument</key>
<false/>
<key>variables</key>
<dict>
<key>action_key_value</key>
Expand Down Expand Up @@ -1565,6 +1567,8 @@
<dict>
<key>argument</key>
<string>{var:split1}</string>
<key>passthroughargument</key>
<false/>
<key>variables</key>
<dict>
<key>clipboard</key>
Expand Down Expand Up @@ -2120,6 +2124,8 @@
<dict>
<key>argument</key>
<string>{var:action2}</string>
<key>passthroughargument</key>
<false/>
<key>variables</key>
<dict/>
</dict>
Expand Down Expand Up @@ -2347,6 +2353,8 @@ end alfred_script</string>
<dict>
<key>argument</key>
<string>{var:action2}</string>
<key>passthroughargument</key>
<false/>
<key>variables</key>
<dict/>
</dict>
Expand Down Expand Up @@ -2379,6 +2387,8 @@ end alfred_script</string>
<dict>
<key>argument</key>
<string></string>
<key>passthroughargument</key>
<false/>
<key>variables</key>
<dict/>
</dict>
Expand Down Expand Up @@ -2411,6 +2421,8 @@ end alfred_script</string>
<dict>
<key>argument</key>
<string>{var:action2}</string>
<key>passthroughargument</key>
<false/>
<key>variables</key>
<dict/>
</dict>
Expand All @@ -2426,6 +2438,8 @@ end alfred_script</string>
<dict>
<key>argument</key>
<string>{var:action2}</string>
<key>passthroughargument</key>
<false/>
<key>variables</key>
<dict/>
</dict>
Expand Down Expand Up @@ -2496,9 +2510,36 @@ end alfred_script</string>
<key>config</key>
<dict>
<key>applescript</key>
<string>use AppleScript version "2.4"use framework "AppKit"use framework "Foundation"use scripting additionson alfred_script(q) set oldClip to my fetchStorableClipboard() set thePasteboard to current application's NSPasteboard's generalPasteboard() set theCount to thePasteboard's changeCount() tell application "System Events" to keystroke "c" using {command down} repeat 20 times if thePasteboard's changeCount() is not theCount then exit repeat delay 0.1 end repeat
<string>use AppleScript version "2.4"
use framework "AppKit"
use framework "Foundation"
use scripting additions
on alfred_script(q)
set oldClip to my fetchStorableClipboard()
set thePasteboard to current application's NSPasteboard's generalPasteboard()
set theCount to thePasteboard's changeCount()
tell application "System Events" to keystroke "c" using {command down}
repeat 20 times
if thePasteboard's changeCount() is not theCount then exit repeat
delay 0.1
end repeat
set input to (the clipboard)
if ((q as string) is equal to "```") then set trimmedText to trimText(trimParagraphs(input, q), return) set newText to q &amp; return &amp; trimmedText &amp; return &amp; q &amp; return else set regex to "^([#]+)?(\\s+)?(.*)" set heading to regex_match from the input against regex given replacement:" $3" set newText to q &amp; trimText(heading, return) &amp; return end if set the clipboard to newText tell application "System Events" to keystroke "v" using {command down} delay 0.2 my putOnClipboard:oldClipend alfred_scripton fetchStorableClipboard()
if ((q as string) is equal to "```") then
set trimmedText to trimText(trimParagraphs(input, q), return)
set newText to q &amp; return &amp; trimmedText &amp; return &amp; q &amp; return
else
set regex to "^([#]+)?(\\s+)?(.*)"
set heading to regex_match from the input against regex given replacement:" $3"
set newText to q &amp; trimText(heading, return) &amp; return
end if
set the clipboard to newText
tell application "System Events" to keystroke "v" using {command down}
delay 0.2
my putOnClipboard:oldClip
end alfred_script
on fetchStorableClipboard()
set aMutableArray to current application's NSMutableArray's array()
set thePasteboard to current application's NSPasteboard's generalPasteboard()
repeat with anItem in thePasteboard's pasteboardItems()
Expand All @@ -2519,9 +2560,43 @@ on putOnClipboard:theArray
set thePasteboard to current application's NSPasteboard's generalPasteboard()
thePasteboard's clearContents()
thePasteboard's writeObjects:theArray
end putOnClipboard:on regex_match against pattern from str given replacement:fmt set regex to current application's NSRegularExpression's regularExpressionWithPattern:pattern options:(current application's NSRegularExpressionCaseInsensitive) |error|:(missing value) (regex's stringByReplacingMatchesInString:str options:0 range:{0, length of str} withTemplate:fmt) as textend regex_match
end putOnClipboard:
on regex_match against pattern from str given replacement:fmt
set regex to current application's NSRegularExpression's regularExpressionWithPattern:pattern options:(current application's NSRegularExpressionCaseInsensitive) |error|:(missing value)
(regex's stringByReplacingMatchesInString:str options:0 range:{0, length of str} withTemplate:fmt) as text
end regex_match
on trimParagraphs(theText, theCharactersToTrim) set theParagraphs to every paragraph of theText repeat with a from 1 to count of paragraphs of theText set theCurrentParagraph to item a of theParagraphs set item a of theParagraphs to trimText(theCurrentParagraph, theCharactersToTrim) end repeat set AppleScript's text item delimiters to return set theText to theParagraphs as string set AppleScript's text item delimiters to "" return theTextend trimParagraphson trimText(theText, theCharactersToTrim) set theTrimLength to length of theCharactersToTrim repeat while theText begins with theCharactersToTrim try set theText to characters (theTrimLength + 1) thru -1 of theText as string on error return "" end try end repeat repeat while theText ends with theCharactersToTrim try set theText to characters 1 thru -(theTrimLength + 1) of theText as string on error return "" end try end repeat return theTextend trimText</string>
on trimParagraphs(theText, theCharactersToTrim)
set theParagraphs to every paragraph of theText
repeat with a from 1 to count of paragraphs of theText
set theCurrentParagraph to item a of theParagraphs
set item a of theParagraphs to trimText(theCurrentParagraph, theCharactersToTrim)
end repeat
set AppleScript's text item delimiters to return
set theText to theParagraphs as string
set AppleScript's text item delimiters to ""
return theText
end trimParagraphs
on trimText(theText, theCharactersToTrim)
set theTrimLength to length of theCharactersToTrim
repeat while theText begins with theCharactersToTrim
try
set theText to characters (theTrimLength + 1) thru -1 of theText as string
on error
return ""
end try
end repeat
repeat while theText ends with theCharactersToTrim
try
set theText to characters 1 thru -(theTrimLength + 1) of theText as string
on error
return ""
end try
end repeat
return theText
end trimText</string>
<key>cachescript</key>
<true/>
</dict>
Expand Down Expand Up @@ -2558,6 +2633,8 @@ on trimParagraphs(theText, theCharactersToTrim) set theParagraphs to every para
<dict>
<key>argument</key>
<string>{var:action2}</string>
<key>passthroughargument</key>
<false/>
<key>variables</key>
<dict/>
</dict>
Expand Down Expand Up @@ -3360,6 +3437,8 @@ on trimParagraphs(theText, theCharactersToTrim) set theParagraphs to every para
<string>False</string>
<key>prefer_filename_to_title</key>
<string>True</string>
<key>prefer_zettel_id_links</key>
<string>False</string>
<key>search_content</key>
<string>False</string>
<key>search_yaml_tags_only</key>
Expand All @@ -3376,7 +3455,7 @@ on trimParagraphs(theText, theCharactersToTrim) set theParagraphs to every para
<key>variablesdontexport</key>
<array/>
<key>version</key>
<string>1.6.1</string>
<string>1.7.0</string>
<key>webaddress</key>
<string>https://github.com/pryley/alfred-the-archive</string>
</dict>
Expand Down
5 changes: 3 additions & 2 deletions search_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
search = Search()
note_path = Tools.getEnv("path_query1")
note_title = search.getNoteTitle(note_path)
note_link_title = search.getNoteLinkTitle(note_path)
filename = pathname2url(os.path.basename(note_path))
back_query = "<EMPTY>" if not query else query

Expand All @@ -22,13 +23,13 @@
"title": "Back",
},
{
"arg": u"markdown_link|>[{0}]({1})".format(note_title, filename),
"arg": u"markdown_link|>[{0}]({1})".format(note_link_title, filename),
"icon": "icons/link.png",
"subtitle": u"Copy a Markdown Link for \"{0}\" to the Clipboard".format(note_title),
"title": "Markdown Link",
},
{
"arg": u"wiki_link|>[[{0}]]".format(note_title),
"arg": u"wiki_link|>[[{0}]]".format(note_link_title),
"icon": "icons/link.png",
"subtitle": u"Copy a Wiki Link for \"{0}\" to the Clipboard".format(note_title),
"title": "Wiki Link",
Expand Down

0 comments on commit 33beb0e

Please sign in to comment.