Sublime Forum

List Markdown Reference Links -- Updating old plugins?

#1

I hope that this is the right place for this question.

I tried using two old (ST2-era) packages so that I could generate a list of Markdown link references to insert into a text document.

Here they are:

http://www.macdrifter.com/2012/08/making-a-sublime-text-plugin-markdown-reference-viewer.html

Neither seem to work. I’ve placed both plugins into my Packages folder in their own directories and implemented the keybinds, but to no avail.

Any hopes of modernizing these plugins for use with ST3?

Thanks!

0 Likes

#2

image

That gist works for me even in ST 4. Did you manually trigger the autocompletion between the second []? Or, maybe you have to elaborate what’s “not working”.


For people who interest in this with ST 4, here is a modified version to better fit ST 4’s autocompletion system (remember to place the .python-version file).

image

2 Likes

#3

Thank you for your contribution. Please bare with me, I posted this message with haste.

Using the original gist I wasn’t able to get a response after entering a second set of brackets, no pop-up, nothing.

If it helps, I have the gist in a folder titled “Markdownreferences” with the .py file inside of it. The folder is within Packages.

0 Likes

#4

Did you try to manually trigger the autocompletion between the second [] ?

0 Likes

#5

Okay. Remember I asked you to please bare with me…

How do I manually trigger it?

0 Likes

#6

I didn’t blame. It’s part of the “debug” procedure. If I don’t ask, then our talk ends.

by default, ctrl+space.

1 Like

#7

Got it. Thank you. By the way, thank you for those little orange squares. Only plugin I discovered that makes adding DEVONThink links a breeze.

If you can’t tell I’m very new to Sublime.

0 Likes

#8

https://packagecontrol.io/packages/OpenUri

0 Likes

#9

Or you can try this version (for ST 3), which will auto invoke auto_complete when you type the second [].

import re
import sublime
import sublime_plugin


# Utility function for shortening text.
# From: http://www.leancrew.com/all-this/2012/08/more-markdown-reference-links-in-bbedit/
def shorten(str, n):
    """ Truncate str to no more than n chars """
    return str if len(str) <= n else str[: n - 1] + "…"


# Provide completions that match just after typing an opening square bracket
class MarkdownReferenceCompletions(sublime_plugin.EventListener):
    working_scope = (
        "text.html.markdown meta.link.reference"
        "    (constant.other.reference.link.markdown | punctuation.definition.constant.end.markdown)"
    )
    
    def on_post_text_command(self, view, command_name, args):
        cursor = view.sel()[0].b

        # auto invoke auto_complete
        if view.match_selector(cursor, self.working_scope):
            view.run_command("auto_complete")

    def on_query_completions(self, view, prefix, locations):
        cursor = locations[0]

        # Only trigger within Markdown reference-style link
        if not view.match_selector(cursor, self.working_scope):
            return []

        # Make sure we're inside the reference name portion
        pt = cursor - len(prefix)
        if view.substr(sublime.Region(pt - 2, pt)) != "][":
            return []

        reflinks = view.find_all(r"^\[([^^\]]+)\]:[ \t]+(https?://)?(.+)$")

        refs = []
        for r in reflinks:
            m = re.match(r"^\[([^^\]]+)\]:[ \t]+(https?://)?(.+)$", view.substr(r))
            if m:
                refs.append(
                    (m.group(1) + "\t" + shorten(m.group(3), 30 - len(m.group(1))), m.group(1))
                )

        return (refs, sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)
0 Likes