in extension.ts [142:182]
function getLinks(document: TextDocument): Promise<Link[]> {
// Return a promise, since this might take a while for large documents
return new Promise<Link[]>((resolve, reject) => {
// Create arrays to hold links as we parse them out
let linksToReturn = new Array<Link>();
// Get lines in the document
let lineCount = document.lineCount;
//Loop over the lines in a document
for(let lineNumber = 0; lineNumber < lineCount; lineNumber++) {
// Get the text for the current line
let lineText = document.lineAt(lineNumber);
// Are there links?
let links = lineText.text.match(/\[[^\[]+\]\(([^\)]+(\)[a-zA-Z0-9-]*.\w*\)|\)))|\[[a-zA-z0-9_-]+\]:\s*(\S+)/g);
if(links) {
// Iterate over the links found on this line
for(let i = 0; i< links.length; i++) {
// Get the URL from each individual link
// ([^\)]+) captures inline style link address
// (\S+) captures reference style link address
var link = links[i].match(/\[[^\[]+\]\(([^\)]+(\)[a-zA-Z0-9-]*.\w+\)|\)))|\[[a-zA-z0-9_-]+\]:\s*(\S+)/);
// Figure out which capture contains the address; inline style or reference
let address = (link[3] == null) ? link[1].slice(0, -1) : link[3];
//Push it to the array
linksToReturn.push({
text: link[0],
address: address,
lineText: lineText
});
}
}
}
if(linksToReturn.length > 0) {
//Return the populated array, which completes the promise.
resolve(linksToReturn);
} else {
//Reject, because we found no links
reject;
}
}).catch();
}