Convert markup links from string to link

I have a change file formatted using Github Loss .

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

This is some [example](http://www.stackoverflow.com) line of text. 

Over time, when the file increased in size, it became a little dirty, mainly due to this method of inserting links.

I would like to convert all links from a string to a link (see description of each ) that converts the above string to this:

 This is some [example][1] line of text. [1]: http://www.stackoverflow.com 

Since the file is quite large and contains many built-in links, I was wondering if there was any automated way to do this. I use Sublime Text 3 for editing, but I could not find a suitable package for this task. Perhaps some kind of clever regex?

+5
source share
2 answers

This is a big requirement!

I just created a new Node.js program (I know this is not a GUI, but it seems that more people would like to be able to) do it on GitHub .

Here is 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); }); }); 
+5
source

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

 view.run_command('mdrelink'); 

from the command console.

I think that I understood the order correctly - you need to reverse it, because otherwise it would ruin the already cached indexes of the following items. It should also automatically skip already used link numbers. My first Python and my first Sublime plugin, so please be careful 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()) 
+4
source

All Articles