Permalink
Browse files

InsertLink command no longer duplicates URLs

If you try to insert a reference to a URL that already exists, it will no longer create a duplicate but will instead reference the index.
  • Loading branch information...
Alcha committed Aug 21, 2018
1 parent 66eb63d commit c1d5c504a854036eea134464a21e1bcf8ae4af5a
Showing with 38 additions and 44 deletions.
  1. +35 −41 cmds/InsertLink.js
  2. +3 −3 tools/LinkTools.js
@@ -6,26 +6,8 @@ const {

const vscode = require('vscode')
const Window = vscode.window
const Selection = vscode.window.activeTextEditor.selection
const Range = vscode.Range

const { showMessage } = require('../tools/StdTools')

/**
* Verifies the provided selection isn't a blank selection.
*
* @param {Selection} selection
*
* @returns {boolean} True or false, is the selection a valid selection?
*/
const verifySelection = (selection, Window) => {
if (selection.start.isEqual(selection.end)) {
showMessage(Window, 'Please make a selection before executing this command.')
.catch(err => Window.showErrorMessage(err.message))
return false
} else return true
}

/**
* The function/command for inserting a link into a Markdown file.
*/
@@ -39,37 +21,49 @@ module.exports = () => {
getLinkUrl(Window).then(url => {
if (url === undefined || url.length === 0) return

let newLink
let existingLinks = parseExistingLinks(doc)
let maxIndex = getMaxIndex(existingLinks)

if (verifySelection(selection, Window)) {
try {
let link = {
index: maxIndex,
url: url,
lineNum: doc.lineCount
}
existingLinks.forEach(link => { if (link.url === url) newLink = link })

if (newLink === undefined) {
let maxIndex = getMaxIndex(existingLinks)
newLink = {
index: maxIndex,
url: url,
lineNum: doc.lineCount
}

editor.edit(builder => {
// The OG text to begin with
let ogText = doc.getText(new Range(selection.start, selection.end))

// The New School text to replace it with
let nsText = `[${ogText}][${newLink.index}]`

editor.edit(builder => {
// The OG text to begin with
let ogText = doc.getText(new Range(selection.start, selection.end))
// Perform the actual replacement
builder.replace(selection, nsText)

// The New School text to replace it with
let nsText = `[${ogText}][${link.index}]`
// The position for the actual url to go
let position = new vscode.Position(newLink.lineNum, 0)

// Perform the actual replacement
builder.replace(selection, nsText)
// The formatted string containing the index and url
let content = `\n[${newLink.index}]: ${newLink.url}`

// The position for the actual url to go
let position = new vscode.Position(link.lineNum, 0)
// Insert the url reference at the bottom of the file
builder.insert(position, content)
})
} else {
editor.edit(builder => {
// The OG text to begin with
let ogText = doc.getText(new Range(selection.start, selection.end))

// The formatted string containing the index and url
let content = `\n[${link.index}]: ${link.url}`
// The New School text to replace it with
let nsText = `[${ogText}][${newLink.index}]`

// Insert the url reference at the bottom of the file
builder.insert(position, content)
})
} catch (err) { Window.showErrorMessage(err.message) }
// Perform the actual replacement
builder.replace(selection, nsText)
})
}
}).catch(err => Window.showErrorMessage(err.message))
} catch (error) {
@@ -11,7 +11,7 @@ const urlStarterRegex = /\[\d+\]: /
*
* @param {Doc} doc
*
* @returns {Links} A collection of links found in the document
* @returns {Link[]} A collection of links found in the document
*/
const parseExistingLinks = doc => {
let links = []
@@ -63,7 +63,7 @@ const getLinkIndex = text => {

/**
*
* @param {Links[]} links
* @param {Link[]} links
*
* @returns {number}
*/
@@ -105,7 +105,7 @@ const getLinkUrlFromUser = window => {
module.exports.getLinkUrl = getLinkUrlFromUser

/**
* @typedef {Object} Links
* @typedef {Object} Link
* @prop {number} index The reference number used for the inline link
* @prop {string} url The url the link points to
* @prop {number} lineNum The line number the link resides on

0 comments on commit c1d5c50

Please sign in to comment.