Skip to content

Commit

Permalink
Add List Back Links
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Ellis committed Oct 2, 2017
1 parent 62e8e92 commit f8985ae
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 7 deletions.
8 changes: 7 additions & 1 deletion Default (Linux).sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -661,9 +661,15 @@
{ "key": "selector", "operator": "equal", "operand": "meta.link.wiki.markdown", "match_all": true }
]
},
{"keys": ["alt+b"], "command": "list_back_links", "context":
[
{ "key": "setting.mde.keymap_disable.list_back_links", "operator": "not_equal", "operand": true },
{ "key": "selector", "operator": "equal", "operand": "text.html.markdown", "match_all": true }
]
},
{"keys": ["alt+j"], "command": "open_journal", "context":
[
{ "key": "setting.mde.keymap_disable.open_page", "operator": "not_equal", "operand": true },
{ "key": "setting.mde.keymap_disable.open_journal", "operator": "not_equal", "operand": true },
{ "key": "selector", "operator": "equal", "operand": "text.html.markdown", "match_all": true }
]
}
Expand Down
8 changes: 7 additions & 1 deletion Default (OSX).sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -666,9 +666,15 @@
{ "key": "selector", "operator": "equal", "operand": "meta.link.wiki.markdown", "match_all": true }
]
},
{"keys": ["alt+b"], "command": "list_back_links", "context":
[
{ "key": "setting.mde.keymap_disable.list_back_links", "operator": "not_equal", "operand": true },
{ "key": "selector", "operator": "equal", "operand": "text.html.markdown", "match_all": true }
]
},
{"keys": ["alt+j"], "command": "open_journal", "context":
[
{ "key": "setting.mde.keymap_disable.open_page", "operator": "not_equal", "operand": true },
{ "key": "setting.mde.keymap_disable.open_journal", "operator": "not_equal", "operand": true },
{ "key": "selector", "operator": "equal", "operand": "text.html.markdown", "match_all": true }
]
}
Expand Down
8 changes: 7 additions & 1 deletion Default (Windows).sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -661,9 +661,15 @@
{ "key": "selector", "operator": "equal", "operand": "meta.link.wiki.markdown", "match_all": true }
]
},
{"keys": ["alt+b"], "command": "list_back_links", "context":
[
{ "key": "setting.mde.keymap_disable.list_back_links", "operator": "not_equal", "operand": true },
{ "key": "selector", "operator": "equal", "operand": "text.html.markdown", "match_all": true }
]
},
{"keys": ["alt+j"], "command": "open_journal", "context":
[
{ "key": "setting.mde.keymap_disable.open_page", "operator": "not_equal", "operand": true },
{ "key": "setting.mde.keymap_disable.open_journal", "operator": "not_equal", "operand": true },
{ "key": "selector", "operator": "equal", "operand": "text.html.markdown", "match_all": true }
]
}
Expand Down
8 changes: 8 additions & 0 deletions Default.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,13 @@
{
"caption": "MarkdownWiki: Open Page",
"command": "open_page"
},
{
"caption": "MarkdownWiki: List Back Links",
"command": "list_back_links"
},
{
"caption": "MarkdownWiki: Open Journal",
"command": "open_journal"
}
]
4 changes: 3 additions & 1 deletion Markdown (Standard).sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,7 @@
// Default keys: (OSX)super+ctrl/shift+pageup (Linux/Win)ctrl+shift(+alt)+pageup
"mde.keymap_disable_goto_previous_heading": false,
// Open the page referenced
"mde.keymap_disable_open_page": false
"mde.keymap_disable.list_back_links": false,
// Open the page referenced
"mde.keymap_disable.open_page": false
}
56 changes: 56 additions & 0 deletions list_back_links.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import sublime, sublime_plugin
import os, string
import re
import mmap

class ListBackLinks(sublime_plugin.TextCommand):
def run(self, edit):
print("Listing backlinks")
self.backlinks = self.find_backlinks()
self.select_backlink()


def find_backlinks(self):
current_file = self.view.file_name()
current_dir, current_base = os.path.split(current_file)
current_name, current_extension = os.path.splitext(current_base)

results = []
for dirname, _, files in self.list_dir_tree(current_dir):
for file in files:
filename = os.path.join(dirname, file)
if self.contains_wikilink(filename, current_name):
results.append(filename)

return results


def contains_wikilink(self, filename, current_name):
link_text = "[[" + current_name + "]]"
print("Searching %s for: %s" % (filename, current_name))

try:
if link_text in open(filename).read():
return True
except:
pass

return False

def select_backlink(self):
self.view.window().show_quick_panel(self.backlinks, self.open_file)


def open_file(self, selected_index):
if selected_index != -1:
file = self.backlinks[selected_index]

print("Opening file '%s'" % (file))
self.view.window().open_file(file)


def list_dir_tree(self, directory):
for dir, dirnames, files in os.walk(directory):
dirnames[:] = [dirname for dirname in dirnames]
yield dir, dirnames, files

4 changes: 1 addition & 3 deletions messages/2.2.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ feedback you can use [GitHub issues][issues].
"mde.keymap_disable.goto_next_heading": false,
// Jump to the previous heading (any level/same or higher level)
// Default keys: (OSX)super+ctrl/shift+pageup (Linux/Win)ctrl+shift(+alt)+pageup
"mde.keymap_disable.goto_previous_heading": false,
// Open the page referenced
"mde.keymap_disable_open_page": false
"mde.keymap_disable.goto_previous_heading": false
}
```

Expand Down

0 comments on commit f8985ae

Please sign in to comment.