Convert markdown links from inline to reference

后端 未结 2 871
暖寄归人
暖寄归人 2020-12-16 02:44

I have a changelog file formatted using Github\'s markdown.

Initially I used inline links for every link I needed to add, that is:

This is s         


        
相关标签:
2条回答
  • 2020-12-16 03:22

    That's a great requirement!

    I've just created a new Node.js program (I know it's not a GUI but seems something more people would like the capability of) to do this on GitHub.

    Here's also the code:

    // node main.js test.md result.md
    
    var fs = require('fs')
    fs.readFile(process.argv[2], 'utf8', function (err, markdown) {
        if (err) {
            return console.log(err);
        }
        var counter = 1;
        var matches = {};
        var matcher = /\[.*?\]\((.*?)\)/g;
        while (match = matcher.exec(markdown)) {
            if (!matches[match[1]]) matches[match[1]] = counter++;
        }
        console.log(matches);
        Object.keys(matches).forEach(function(url) {
            var r = new RegExp("(\\[.*?\\])\\(" + url + "\\)", "g");
            markdown = markdown.replace(r, "$1[" + matches[url] + "]");
            markdown += "\n[" + matches[url] + "]: " + url;
        });
    
        fs.writeFile(process.argv[3], markdown, 'utf8', function (err) {
            if (err) return console.log(err);
        });
    
    });
    
    0 讨论(0)
  • 2020-12-16 03:28

    Save this as mdrelink.py in your Packages folder, and you can then run it with

    view.run_command('mdrelink');
    

    from within the command console.

    I think I got the order thingy right – reversing is necessary because otherwise it would mess up the already cached indexes of next items. It should also automatically skip already used link numbers. My first Python and my first Sublime plugin, so please be gentle with me.

    import sublime, sublime_plugin
    
    class mdrelinkCommand(sublime_plugin.TextCommand):
        def run(self, edit):
            oldlinks = []
            self.view.find_all("^\s*(\[\d+\]):", sublime.IGNORECASE, "\\1", oldlinks)
            newlinkpos = self.view.find_all("\[.+?\](\(.+?\))")
            orgtext = []
            self.view.find_all("(\[.+?\])\(.+?\)", sublime.IGNORECASE, "\\1", orgtext)
            orglink = []
            self.view.find_all("\[.+?\]\((.+?)\)", sublime.IGNORECASE, "\\1", orglink)
            orglink.reverse()
            self.view.insert(edit, self.view.size(), '\n\n')
            counter = 1
            newnumbers = []
            for r in newlinkpos:
                while '['+str(counter)+']' in oldlinks:
                     counter += 1
                oldlinks.append('['+str(counter)+']')
                line = '[' + str(counter)+']: '+ orglink.pop() + '\n'
                newnumbers.append('  ['+str(counter)+']')
                self.view.insert(edit, self.view.size(), line)
            for r in reversed(newlinkpos):
                self.view.replace(edit, r, orgtext.pop()+newnumbers.pop())
    
    0 讨论(0)
提交回复
热议问题