lib/linksoup.js
function parseSpans(text) {
		var links = parseLinks(text);
		/*Represented as
		[{
		href: "http://yada",
		start: startIndex,
		end: endIndex
		}]*/
		var spans = [];
		var lastSpan = null;
		
		function isPrefixedLinkSpan(span)
		{
			return span && "href" in span && span.prefix && "proposed" in span.prefix;
		}
		
		function isTextSpan(span)
		{
			return span && !("href" in span);
		}
		
		function addTextSpan(text) {
			//if the last span was a candidate
			if (isPrefixedLinkSpan(lastSpan)) {
				//make sure there is a valid suffix
				if (regexen.matchMarkdownLinkSuffix.test(text))	{
					//is there any valid whitespace remaining?
					text = RegExp.$3;
					//use the title (if specified)
					if (RegExp.$2) {
						lastSpan.title = RegExp.$2;
					}
					//use the proposed link text for the verified link
					lastSpan.text = lastSpan.prefix.proposed.linkText;
					
					//if there was valid prefix text, use it
					if (lastSpan.prefix.proposed.text) {
						lastSpan.prefix.text = lastSpan.prefix.proposed.text;
						delete lastSpan.prefix["proposed"]; //clean up proposal	
					}
					else {
						spans.splice(spans.length - 2, 1); //remove prefix
						lastSpan = lastSpan.prefix;
					}
				}
				else {
					delete lastSpan.prefix["proposed"]; //clean up proposal...no match...no modification
				}
				delete lastSpan["prefix"]; //clean up prefix scratchwork
			}
			if (text) {
				lastSpan = {
					text : text
				};
				spans.push(lastSpan);
			}
		}
		
		function addLinkSpan(linkSpan) {
			var span = {
				href : linkSpan.href
			};

			if (isTextSpan(lastSpan) && regexen.matchMarkdownLinkPrefix.test(lastSpan.text))	{
				lastSpan.proposed = { text: RegExp.$1, linkText: RegExp.$2 };
				span.prefix = lastSpan;
			}
		
			lastSpan = span;
			spans.push(lastSpan);
		}
		
		//No links found, all text
		if (links.length === 0) {
			addTextSpan(text);
		} 
		//One or more links found
		else {
			var firstLink = links[0],
			lastLink = links[links.length - 1];
			
			//Was there text before the first link?
			if (firstLink.start > 0) {
				addTextSpan(text.substring(0, firstLink.start));
			}
			//Handle single link
			if (links.length === 1) {
				addLinkSpan(firstLink);
			} else {
				//push the firstLink
				addLinkSpan(firstLink);
				var prevLink = firstLink;
				
				//loop from the second
				for (var i = 1; i < links.length; i++) {
					//is there text between?
					if (links[i].start - prevLink.end >= 1) {
						addTextSpan(text.substring(prevLink.end, links[i].start));
					}
					//add link
					addLinkSpan(prevLink = links[i]);
				}
			}
			//Was there text after the links?
			if (lastLink.end < (text.length)) {
				addTextSpan(text.substring(lastLink.end));
			}
		}
		return spans;
	}
function parseSpans(text) {
		var links = parseLinks(text);
		/*Represented as
		[{
		href: "http://yada",
		start: startIndex,
		end: endIndex
		}]*/
		var spans = [];
		var lastSpan = null;
		
		function isPrefixedLinkSpan(span)
		{
			return span && "href" in span && span.prefix && "proposed" in span.prefix;
		}
		
		function isTextSpan(span)
		{
			return span && !("href" in span);
		}
		
		function addTextSpan(text) {
			//if the last span was a candidate
			if (isPrefixedLinkSpan(lastSpan)) {
				//make sure there is a valid suffix
				if (regexen.matchMarkdownLinkSuffix.test(text))	{
					//is there any valid whitespace remaining?
					text = RegExp.$3;
					//use the title (if specified)
					if (RegExp.$2) {
						lastSpan.title = RegExp.$2;
					}
					//use the proposed link text for the verified link
					lastSpan.text = lastSpan.prefix.proposed.linkText;
					
					//if there was valid prefix text, use it
					if (lastSpan.prefix.proposed.text) {
						lastSpan.prefix.text = lastSpan.prefix.proposed.text;
						delete lastSpan.prefix["proposed"]; //clean up proposal	
					}
					else {
						spans.splice(spans.length - 2, 1); //remove prefix
						lastSpan = lastSpan.prefix;
					}
				}
				else {
					delete lastSpan.prefix["proposed"]; //clean up proposal...no match...no modification
				}
				delete lastSpan["prefix"]; //clean up prefix scratchwork
			}
			if (text) {
				lastSpan = {
					text : text
				};
				spans.push(lastSpan);
			}
		}
		
		function addLinkSpan(linkSpan) {
			var span = {
				href : linkSpan.href
			};

			if (isTextSpan(lastSpan) && regexen.matchMarkdownLinkPrefix.test(lastSpan.text))	{
				lastSpan.proposed = { text: RegExp.$1, linkText: RegExp.$2 };
				span.prefix = lastSpan;
			}
		
			lastSpan = span;
			spans.push(lastSpan);
		}
		
		//No links found, all text
		if (links.length === 0) {
			addTextSpan(text);
		} 
		//One or more links found
		else {
			var firstLink = links[0],
			lastLink = links[links.length - 1];
			
			//Was there text before the first link?
			if (firstLink.start > 0) {
				addTextSpan(text.substring(0, firstLink.start));
			}
			//Handle single link
			if (links.length === 1) {
				addLinkSpan(firstLink);
			} else {
				//push the firstLink
				addLinkSpan(firstLink);
				var prevLink = firstLink;
				
				//loop from the second
				for (var i = 1; i < links.length; i++) {
					//is there text between?
					if (links[i].start - prevLink.end >= 1) {
						addTextSpan(text.substring(prevLink.end, links[i].start));
					}
					//add link
					addLinkSpan(prevLink = links[i]);
				}
			}
			//Was there text after the links?
			if (lastLink.end < (text.length)) {
				addTextSpan(text.substring(lastLink.end));
			}
		}
		return spans;
	}
function parseSpans ( text ) { var links = parseLinks ( text ) ; /*Represented as
		[{
		href: "http://yada",
		start: startIndex,
		end: endIndex
		}]*/ var spans = [ ] ; var lastSpan = null ; function isPrefixedLinkSpan ( span ) { return span && "href" in span && span . prefix && "proposed" in span . prefix ; } function isTextSpan ( span ) { return span && ! ( "href" in span ) ; } function addTextSpan ( text ) { //if the last span was a candidate
 if ( isPrefixedLinkSpan ( lastSpan ) ) { //make sure there is a valid suffix
 if ( regexen . matchMarkdownLinkSuffix . test ( text ) ) { //is there any valid whitespace remaining?
 text = RegExp . $3 ; //use the title (if specified)
 if ( RegExp . $2 ) { lastSpan . title = RegExp . $2 ; } //use the proposed link text for the verified link
 lastSpan . text = lastSpan . prefix . proposed . linkText ; //if there was valid prefix text, use it
 if ( lastSpan . prefix . proposed . text ) { lastSpan . prefix . text = lastSpan . prefix . proposed . text ; delete lastSpan . prefix [ "proposed" ] ; //clean up proposal	
 } else { spans . splice ( spans . length - 2 , 1 ) ; //remove prefix
 lastSpan = lastSpan . prefix ; } } else { delete lastSpan . prefix [ "proposed" ] ; //clean up proposal...no match...no modification
 } delete lastSpan [ "prefix" ] ; //clean up prefix scratchwork
 } if ( text ) { lastSpan = { text : text } ; spans . push ( lastSpan ) ; } } function addLinkSpan ( linkSpan ) { var span = { href : linkSpan . href } ; if ( isTextSpan ( lastSpan ) && regexen . matchMarkdownLinkPrefix . test ( lastSpan . text ) ) { lastSpan . proposed = { text : RegExp . $1 , linkText : RegExp . $2 } ; span . prefix = lastSpan ; } lastSpan = span ; spans . push ( lastSpan ) ; } //No links found, all text
 if ( links . length === 0 ) { addTextSpan ( text ) ; } //One or more links found
 else { var firstLink = links [ 0 ] , lastLink = links [ links . length - 1 ] ; //Was there text before the first link?
 if ( firstLink . start > 0 ) { addTextSpan ( text . substring ( 0 , firstLink . start ) ) ; } //Handle single link
 if ( links . length === 1 ) { addLinkSpan ( firstLink ) ; } else { //push the firstLink
 addLinkSpan ( firstLink ) ; var prevLink = firstLink ; //loop from the second
 for ( var i = 1 ; i < links . length ; i ++ ) { //is there text between?
 if ( links [ i ] . start - prevLink . end >= 1 ) { addTextSpan ( text . substring ( prevLink . end , links [ i ] . start ) ) ; } //add link
 addLinkSpan ( prevLink = links [ i ] ) ; } } //Was there text after the links?
 if ( lastLink . end < ( text . length ) ) { addTextSpan ( text . substring ( lastLink . end ) ) ; } } return spans ; }
Parses the text into an array of spans
Parses the text into an array of spans