Created
August 26, 2023 06:54
-
-
Save OdatNurd/cb79549c65872f58790abf9780c28c99 to your computer and use it in GitHub Desktop.
Simple plugin that lets you easily copy temporary passwords out of markdown files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sublime | |
import sublime_plugin | |
from urllib.parse import quote, unquote | |
class SetUpButtonsCommand(sublime_plugin.TextCommand): | |
""" | |
When executed, this command checks the current file for all items that | |
match the regular expression and adds a phantom that alows for copying | |
text. | |
The regex looks for: | |
Some Random Text: password goes here [copy] | |
The password goes here part will be copied when the clipboard icon next to | |
the word [copy] is clicked. | |
This is triggered automatically by the event listener when a markdown file | |
is edited; however you could also trigger this manually, say via a key | |
binding or the command palette, if you add in new passwords after the file | |
has been opened. | |
""" | |
def run(self, edit): | |
# Create a new phantom set and attach it to the vierw | |
self.buttons = sublime.PhantomSet(self.view) | |
# Find all of the instances of the regex; the format string will pull | |
# the password out (based on the capture group in the regex) and put it | |
# into the password list; btns is the text regions of each match, | |
passwords = [] | |
btns = self.view.find_all(r'^[^:]+: (.*) \[copy\]',0, '$1', passwords) | |
# Set up a phantom for every match; this zips together the regions and | |
# the passwords they contain for easy handling. | |
phantoms = [] | |
for btn,password in zip(btns, passwords): | |
# Add in a phantom; the -5 moves the phantom 4 characters back from | |
# the end of the matched region, so that it ends up in the square | |
# brackets. | |
phantoms.append(sublime.Phantom( | |
sublime.Region(btn.b - 5), | |
f'<a href="copy#{quote(password)}">📋</a>', | |
sublime.LAYOUT_INLINE, | |
self.click | |
)) | |
# Add the regions to the buffer | |
self.buttons.update(phantoms) | |
def click(self, href): | |
""" | |
This gets invoked with the href attribute of one of clicked link in a | |
phantom; we pull the passwor dout and put it onto the clipboard. | |
""" | |
password = href.split('#')[-1] | |
sublime.set_clipboard(unquote(password)) | |
self.view.window().status_message('secret password copied to the clipboard') | |
class MarkdownPasswordCopyListener(sublime_plugin.ViewEventListener): | |
""" | |
Simple listener that watches for a markdown file to open and then triggers | |
the command that looks for passwords. | |
""" | |
@classmethod | |
def is_applicable(cls, settings): | |
return "/Markdown/" in settings.get("syntax") | |
def on_load(self): | |
self.view.run_command("set_up_buttons") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a simple plugin created at the request of user Roilisi in my Twitch Stream.
When you open a markdown file with a sequence of text like:
The plugin will inject a clipboard button inside of the square brackets which, when clicked, will copy the password to the clipboard.
If you're not sure how to use plugins in Sublime Text, this video shows how to set that up.