{"tiddlers":{"$:/plugins/flibbles/relink-markdown/fieldtypes/markdown.js":{"title":"$:/plugins/flibbles/relink-markdown/fieldtypes/markdown.js","text":"/*\\\nmodule-type: relinkfieldtype\ntitle: $:/plugins/flibbles/relink-markdown/fieldtypes/markdown.js\ntype: application/javascript\n\nThis relinks tiddlers which contain markdown. It tries to be agnostic to\nwhichever markdown plugin you're using.\n\n\\*/\n\nvar Rebuilder = require(\"$:/plugins/flibbles/relink/js/utils/rebuilder.js\");\nvar utils = require(\"$:/plugins/flibbles/relink-markdown/utils/markdown.js\");\nvar WikiParser = require(\"$:/core/modules/parsers/wikiparser/wikiparser.js\")['text/vnd.tiddlywiki'];\n\nfunction MarkdownWalker(text, options) {\n\tthis.wiki = options.wiki;\n\tthis.options = options;\n\tif(!this.mdInlineRuleClasses) {\n\t\tMarkdownWalker.prototype.mdInlineRuleClasses = $tw.modules.createClassesFromModules(\"relinkmarkdownrule\",\"inline\",$tw.MarkdownRuleBase);\n\t}\n\tif(!this.mdBlockRuleClasses) {\n\t\tMarkdownWalker.prototype.mdBlockRuleClasses = $tw.modules.createClassesFromModules(\"relinkmarkdownrule\",\"block\",$tw.MarkdownRuleBase);\n\t}\n\tthis.source = text || \"\";\n\tthis.sourceLength = this.source.length;\n\t// Set current parse position\n\tthis.pos = 0;\n\t// Instantiate the parser block and inline rules\n\tthis.blockRules = this.instantiateRules(this.mdBlockRuleClasses,\"block\",0);\n\tthis.inlineRules = this.instantiateRules(this.mdInlineRuleClasses,\"inline\",0);\n\t// instantiateRules first with indent==undefined so we can match regardless\n\t// of tabdepth. Now we need to be strict about it.\n\tthis.indent = 0;\n\tthis.parseBlocks();\n};\n\nMarkdownWalker.prototype = Object.create(WikiParser.prototype);\n\nmodule.exports\n\nMarkdownWalker.prototype.parseBlock = function(terminatorRegExpString) {\n\tvar terminatorRegExp = /([^\\S\\n]*\\r?\\n\\r?\\n)/mg;\n\tthis.skipEmptyLines();\n\tif(this.pos >= this.sourceLength) {\n\t\treturn [];\n\t}\n\t// Look for a block rule that applies at the current position\n\tvar nextMatch = this.findNextMatch(this.blockRules, this.pos);\n\tif(nextMatch && nextMatch.matchIndex === this.pos) {\n\t\treturn this.handleRule(nextMatch);\n\t}\n\treturn this.parseInlineRun(terminatorRegExp);\n};\n\nMarkdownWalker.prototype.parseInlineRunTerminated = function(terminatorRegExp,options) {\n\toptions = options || {};\n\tvar tree = [];\n\t// Find the next occurrence of the terminator\n\tterminatorRegExp.lastIndex = this.pos;\n\tvar terminatorMatch = terminatorRegExp.exec(this.source);\n\t// Find the next occurrence of a inlinerule\n\tvar inlineRuleMatch = this.findNextMatch(this.inlineRules,this.pos);\n\t// Loop around until we've reached the end of the text\n\twhile(this.pos < this.sourceLength && (terminatorMatch || inlineRuleMatch)) {\n\t\t// Return if we've found the terminator, and it precedes any inline rule match\n\t\tif(terminatorMatch) {\n\t\t\tif(!inlineRuleMatch || inlineRuleMatch.matchIndex >= terminatorMatch.index) {\n\t\t\t\tthis.handleWikitext(this.pos, terminatorMatch.index);\n\t\t\t\t//if(options.eatTerminator) {\n\t\t\t\t\tthis.pos += terminatorMatch[0].length;\n\t\t\t\t//}\n\t\t\t\treturn tree;\n\t\t\t}\n\t\t}\n\t\t// Process any inline rule, along with the text preceding it\n\t\tif(inlineRuleMatch) {\n\t\t\t// Preceding text\n\t\t\tthis.handleWikitext(this.pos, inlineRuleMatch.matchIndex);\n\t\t\tthis.handleRule(inlineRuleMatch);\n\t\t\t// Look for the next inline rule\n\t\t\tinlineRuleMatch = this.findNextMatch(this.inlineRules,this.pos);\n\t\t\t// Look for the next terminator match\n\t\t\tterminatorRegExp.lastIndex = this.pos;\n\t\t\tterminatorMatch = terminatorRegExp.exec(this.source);\n\t\t}\n\t}\n\t// Process the remaining text\n\tthis.handleWikitext(this.pos, this.sourceLength);\n\treturn tree;\n};\n\nMarkdownWalker.prototype.skipEmptyLines = function() {\n\tvar emptyRegExp = /(?:[^\\S\\n]*\\n)+/mg;\n\temptyRegExp.lastIndex = this.pos;\n\tvar emptyMatch = emptyRegExp.exec(this.source);\n\tif(emptyMatch && emptyMatch.index === this.pos) {\n\t\tthis.pos = emptyRegExp.lastIndex;\n\t}\n};\n\nfunction MarkdownReporter(text, callback, options) {\n\tthis.callback = callback;\n\tMarkdownWalker.call(this, text, options);\n};\n\nMarkdownReporter.prototype = Object.create(MarkdownWalker.prototype);\n\nMarkdownReporter.prototype.handleRule = function(ruleInfo) {\n\tif (ruleInfo.rule.report) {\n\t\truleInfo.rule.report(this.source, this.callback, this.options);\n\t} else {\n\t\tif (ruleInfo.rule.matchRegExp !== undefined) {\n\t\t\tthis.pos = ruleInfo.rule.matchRegExp.lastIndex;\n\t\t} else {\n\t\t\t// We can't easily determine the end of this\n\t\t\t// rule match. We'll \"parse\" it so that\n\t\t\t// parser.pos gets updated, but we throw away\n\t\t\t// the results.\n\t\t\truleInfo.rule.parse();\n\t\t}\n\t}\n};\n\nMarkdownReporter.prototype.handleWikitext = function(startPos, end) {\n\tif (startPos < end) {\n\t\tvar config = utils.getSettings(this.wiki);\n\t\tif (config.wikitext) {\n\t\t\tvar substr = this.source.substring(this.pos, end);\n\n\t\t\tvar pragma = config.wikitextPragma;\n\t\t\tvar wikitextHandler = utils.getType('wikitext');\n\t\t\tvar wikiEntry = wikitextHandler.report(pragma + substr, this.callback, this.options);\n\t\t}\n\t}\n\tthis.pos = end;\n};\n\nexports.report = function(markdowntext, callback, options) {\n\tnew MarkdownReporter(markdowntext, callback, options);\n};\n\nfunction MarkdownRelinker(text, fromTitle, toTitle, options) {\n\tthis.fromTitle = fromTitle;\n\tthis.toTitle = toTitle;\n\tthis.builder = new Rebuilder(text);\n\tMarkdownWalker.call(this, text, options);\n};\n\nMarkdownRelinker.prototype = Object.create(MarkdownWalker.prototype);\n\nMarkdownRelinker.prototype.handleRule = function(ruleInfo) {\n\tvar newEntry = ruleInfo.rule.relink(this.source, this.fromTitle, this.toTitle, this.options);\n\tif (newEntry !== undefined) {\n\t\tif (newEntry.impossible) {\n\t\t\tthis.impossible = true;\n\t\t}\n\t\tif (newEntry.output) {\n\t\t\tthis.builder.add(newEntry.output, ruleInfo.matchIndex, this.pos);\n\t\t}\n\t}\n};\n\nMarkdownRelinker.prototype.handleWikitext = function(startPos, end) {\n\tif (startPos < end) {\n\t\tvar config = utils.getSettings(this.wiki);\n\t\tif (config.wikitext) {\n\t\t\tvar substr = this.source.substring(this.pos, end);\n\n\t\t\tvar pragma = config.wikitextPragma;\n\t\t\tvar wikitextHandler = utils.getType('wikitext');\n\t\t\tvar wikiEntry = wikitextHandler.relink(pragma + substr, this.fromTitle, this.toTitle, this.options);\n\t\t\tif (wikiEntry != undefined) {\n\t\t\t\tif (wikiEntry.impossible) {\n\t\t\t\t\tthis.impossible = true;\n\t\t\t\t}\n\t\t\t\tif (wikiEntry.output) {\n\t\t\t\t\tthis.builder.add(wikiEntry.output.slice(pragma.length), startPos, end);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tthis.pos = end;\n};\n\nexports.name = \"markdown\";\n\nexports.relink = function(markdowntext, fromTitle, toTitle, options) {\n\tvar relinker = new MarkdownRelinker(markdowntext, fromTitle, toTitle, options),\n\t\tentry;\n\tif (relinker.builder.changed() || relinker.impossible) {\n\t\tentry = {\n\t\t\toutput: relinker.builder.results(),\n\t\t\timpossible: relinker.impossible };\n\t}\n\treturn entry;\n};\n","module-type":"relinkfieldtype","type":"application/javascript"},"$:/plugins/flibbles/relink-markdown/text/markdowntext/codeblock.js":{"title":"$:/plugins/flibbles/relink-markdown/text/markdowntext/codeblock.js","text":"/*\\\nmodule-type: relinkmarkdownrule\ntitle: $:/plugins/flibbles/relink-markdown/text/markdowntext/codeblock.js\ntype: application/javascript\n\n```javascript\ncode\n```\n\n\\*/\n\nvar utils = require(\"$:/plugins/flibbles/relink-markdown/utils/markdown\");\n\nexports.name = \"codeblock\";\nexports.types = {inline: true};\n\nexports.init = function(parser) {\n\tthis.parser = parser;\n\tthis.matchRegExp = /(```+)[^\\n`]*(?:\\n|$)/mg;\n\tthis.maxIndent = 3;\n};\n\nexports.relink = function(text, fromTitle, toTitle, options) {\n\tvar endRegExp = new RegExp(\"^ {0,3}\" + this.match[1] + \"+[^\\\\S\\\\n]*\\\\n\", \"mg\");\n\tendRegExp.lastIndex = this.matchRegExp.lastIndex;\n\tvar endMatch = endRegExp.exec(this.parser.source);\n\tif (endMatch) {\n\t\tthis.parser.pos = endRegExp.lastIndex;\n\t} else {\n\t\tthis.parser.pos = this.parser.sourceLength;\n\t}\n\treturn undefined;\n};\n\nexports.report = exports.relink;\n","module-type":"relinkmarkdownrule","type":"application/javascript"},"$:/plugins/flibbles/relink-markdown/text/markdowntext/codeinline.js":{"title":"$:/plugins/flibbles/relink-markdown/text/markdowntext/codeinline.js","text":"/*\\\nmodule-type: relinkmarkdownrule\ntitle: $:/plugins/flibbles/relink-markdown/text/markdowntext/codeinline.js\ntype: application/javascript\n\nHandles markdown `code` and ``code``.\n\n\\*/\n\nvar utils = require(\"$:/plugins/flibbles/relink-markdown/utils/markdown\");\n\nexports.name = \"codeinline\";\nexports.types = {inline: true};\n\nexports.init = function(parser) {\n\tthis.parser = parser;\n};\n\nexports.findNextMatch = function(startPos) {\n\tvar match, matchRegExp = /`+/mg;\n\tmatchRegExp.lastIndex = startPos;\n\twhile (match = matchRegExp.exec(this.parser.source)) {\n\t\tvar next = this.parser.source.indexOf(match[0], matchRegExp.lastIndex);\n\t\t// make sure we find the corresponding ticks\n\t\tif (next >= 0) {\n\t\t\t// Make sure it's the right length\n\t\t\tvar end = next + match[0].length;\n\t\t\tif (match[0].length < 3 || !isLineStart(this.parser.source, next)) {\n\t\t\t\tif (this.parser.source.charAt(end) !== '`') {\n\t\t\t\t\t// make sure there aren't paragraph breaks between the points\n\t\t\t\t\tvar nextGraph = utils.indexOfParagraph(this.parser.source, matchRegExp.lastIndex);\n\t\t\t\t\tif (nextGraph < 0 || nextGraph > next) {\n\t\t\t\t\t\tthis.end = end;\n\t\t\t\t\t\treturn match.index;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn undefined;\n};\n\nfunction isLineStart(text, pos) {\n\t// if 3 or less spaces precede it, it's a line start.\n\tvar p = text.lastIndexOf('\\n', pos);\n\tif (pos - p > 3) {\n\t\treturn false;\n\t}\n\twhile (++p < pos) {\n\t\tif (text.charAt(p) !== ' ') {\n\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n};\n\nexports.relink = function() {\n\tthis.parser.pos = this.end;\n\treturn undefined;\n};\n\nexports.report = exports.relink;\n","module-type":"relinkmarkdownrule","type":"application/javascript"},"$:/plugins/flibbles/relink-markdown/text/markdowntext/footnote.js":{"title":"$:/plugins/flibbles/relink-markdown/text/markdowntext/footnote.js","text":"/*\\\nmodule-type: relinkmarkdownrule\ntitle: $:/plugins/flibbles/relink-markdown/text/markdowntext/footnote.js\ntype: application/javascript\n\nHandles markdown footnotes\n\n[1]: #link\n\n\\*/\n\nvar utils = require(\"$:/plugins/flibbles/relink-markdown/utils/markdown\");\n\nexports.name = \"markdownfootnote\";\nexports.types = {block: true};\n\nexports.init = function(parser) {\n\tthis.parser = parser;\n\tthis.matchRegExp = /\\[((?:[^\\\\\\]]|\\\\.)*)\\]:(\\s*)(#?)(\\S+)([^\\S\\n]*(?:\\n|$))/mg;\n\tthis.maxIndent = 3;\n};\n\nexports.report = function(text, callback, options) {\n\tvar m = this.match,\n\t\tdecodedLink,\n\t\tentry;\n\tthis.parser.pos = m.index + m[0].length;\n\ttry {\n\t\tdecodedLink = utils.decodeLink(m[4]);\n\t} catch (e) {\n\t\t// The link is malformed. Just skip it.\n\t\treturn;\n\t}\n\tif (isActuallyLink(m, decodedLink, options)) {\n\t\tcallback(decodedLink, '[' + utils.abridgeString(m[1], 18) + ']:');\n\t}\n};\n\nexports.relink = function(text, fromTitle, toTitle, options) {\n\tvar m = this.match,\n\t\tlink = m[4],\n\t\tentry,\n\t\tdecodedLink;\n\tthis.parser.pos = m.index + m[0].length;\n\ttry {\n\t\tdecodedLink = utils.decodeLink(m[4]);\n\t} catch (e) {\n\t\t// The link is malformed. Just skip it.\n\t\treturn;\n\t}\n\tif (isActuallyLink(m, toTitle, options) && decodedLink === fromTitle) {\n\t\tvar encodedTo = utils.encodeLink(toTitle);\n\t\tif (m[3] !== '#' && (encodedTo !== toTitle)) {\n\t\t\t// We need to add in a hash, because this old-type footnote can't\n\t\t\t// support the new title.\n\t\t\tm[3] = '#';\n\t\t}\n\t\tentry = { output: this.indentString + \"[\" + m[1] + \"]:\" + m[2] + m[3] + encodedTo + m[5] };\n\t}\n\treturn entry;\n};\n\nfunction isActuallyLink(match, tiddlerTitle, options) {\n\treturn match[1].charAt(0) !== '^'\n\t\t&& (match[3] === '#'\n\t\t\t|| (options.wiki.isImageTiddler(tiddlerTitle)\n\t\t\t\t&& decodeURIComponent(match[4]) === match[4]));\n};\n","module-type":"relinkmarkdownrule","type":"application/javascript"},"$:/plugins/flibbles/relink-markdown/text/markdowntext/link.js":{"title":"$:/plugins/flibbles/relink-markdown/text/markdowntext/link.js","text":"/*\\\nmodule-type: relinkmarkdownrule\ntitle: $:/plugins/flibbles/relink-markdown/text/markdowntext/link.js\ntype: application/javascript\n\nHandles markdown links\n\n[caption](#link)\n\n\\*/\n\nvar utils = require(\"$:/plugins/flibbles/relink-markdown/utils/markdown\");\nvar markdown = utils.getType('markdown');\n\nexports.name = \"markdownlink\";\nexports.types = {inline: true};\n\nexports.init = function(parser) {\n\tthis.parser = parser;\n};\n\nexports.findNextMatch = function(startPos) {\n\tthis.endMatch = this.matchLink(this.parser.source, startPos);\n\treturn this.endMatch ? this.endMatch.index : undefined;\n};\n\n/**A zero side-effect method which returns a regexp which pretended to match\n * the whole link, caption and all. I do this instead of just using a\n * regexp to begin with, because markdown links require context-free grammar\n * matching.\n * Currently, it doesn't properly set match[0]. No need as of yet.\n * 1. \"!\"\n * 2. caption\n * 3. \"\\s*<?#?\"\n * 4. \"link\"\n * 5. \"\\s*'tooltip'\"\n */\nexports.matchLink = function(text, pos) {\n\tpos = pos-1;\n\tvar match = undefined;\n\tdo {\n\t\tpos = text.indexOf('[', pos+1);\n\t\tif (pos < 0) {\n\t\t\treturn undefined;\n\t\t}\n\t\tvar caption = this.getEnclosed(text, pos, '[', ']');\n\t\tif (caption === undefined) {\n\t\t\tcontinue;\n\t\t}\n\t\tvar subPos = pos + caption.length+2;\n\t\tif (text.charAt(subPos) !== '(') {\n\t\t\tcontinue;\n\t\t}\n\t\t++subPos; // Skip over that '('\n\t\t// Now we need to find the preamble before the link.\n\t\tvar preambleRegExp = /(\\s*\\<\\s*#?)|(\\s*#?)/g;\n\t\tpreambleRegExp.lastIndex = subPos;\n\t\tvar preambleMatch = preambleRegExp.exec(text);\n\t\tsubPos += preambleMatch[0].length;\n\t\t// Now that we know what kind of link we have, let's try and get it.\n\t\tvar link, closingBracket = '';\n\t\tif (preambleMatch[1]) { // We have the \"#<.. ..>\" style link\n\t\t\tvar bracketRegExp = /([^\\>\\n\\r]+?)(\\s*>)/g;\n\t\t\tbracketRegExp.lastIndex = subPos;\n\t\t\tvar bracketMatch = bracketRegExp.exec(text);\n\t\t\tif (!bracketMatch) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tlink = bracketMatch[1];\n\t\t\tsubPos = bracketRegExp.lastIndex;\n\t\t\tclosingBracket = bracketMatch[2];\n\t\t} else { // We have the \"#...\" style link\n\t\t\tlink = extractUnquotedLink(text, subPos);\n\t\t\tif (!link) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tsubPos += link.length;\n\t\t}\n\t\t// match[1] and match[2] are the \"!\" and \"caption\", filled in later.\n\t\tvar tooltipRegExp = /((?:\\s+(?:'(?:[^'\\\\]|\\\\.)*'|\"(?:[^\"\\\\]|\\\\.)*\"|\\([^)]*\\)))?\\s*)\\)/g;\n\t\ttooltipRegExp.lastIndex = subPos;\n\t\tvar tooltipMatch = tooltipRegExp.exec(text);\n\t\tif (tooltipMatch\n\t\t&& tooltipMatch.index === subPos\n\t\t&& utils.indexOfParagraph(tooltipMatch[0]) < 0) {\n\t\t\tmatch = [null, \"\", caption, preambleMatch[0], link, closingBracket + tooltipMatch[1]];\n\t\t\tif (text.charAt(pos-1) === \"!\") {\n\t\t\t\tmatch.index = pos-1;\n\t\t\t\tmatch[1] = \"!\";\n\t\t\t} else {\n\t\t\t\tmatch.index = pos;\n\t\t\t}\n\t\t\tmatch[0] = text.substring(match.index, tooltipRegExp.lastIndex);\n\t\t}\n\t} while (!match);\n\treturn match;\n};\n\nexports.report = function(text, callback, options) {\n\tvar em = this.endMatch,\n\t\tcaption = em[2],\n\t\tprefix = em[1],\n\t\tisImage = (prefix === '!'),\n\t\thash = isImage ? em[3].trim() : '#',\n\t\tlink = em[4],\n\t\thasHash = em[3].lastIndexOf('#') >= 0;\n\tthis.parser.pos = em.index + em[0].length;\n\tif (!isImage) {\n\t\tmarkdown.report(caption, function(title, blurb, style) {\n\t\t\tcallback(title, prefix + '[' + (blurb || '') + '](' + hash + link + ')', style);\n\t\t}, options);\n\t}\n\tif (isImage || hasHash) {\n\t\tvar safeCaption = utils.abridgeString(caption, 18);\n\t\ttry {\n\t\t\tvar decodedLink = utils.decodeLink(link);\n\t\t\t// If the link doesn't have a hash, it can't have any escaping\n\t\t\tif (hasHash || (decodedLink === link)) {\n\t\t\t\tcallback(decodedLink, em[1] + '[' + safeCaption + '](' + hash + ')');\n\t\t\t}\n\t\t} catch (e) {\n\t\t\t// It must be a malformed link. Not our problem.\n\t\t\t// Just move on.\n\t\t}\n\t}\n};\n\nexports.relink = function(text, fromTitle, toTitle, options) {\n\tvar entry = {},\n\t\tem = this.endMatch,\n\t\tmodified = false,\n\t\tcaption = em[2],\n\t\tisImage = (em[1] === '!'),\n\t\tlink = em[4];\n\tthis.parser.pos = em.index + em[0].length;\n\tif (!isImage) {\n\t\tvar newCaption = markdown.relink(caption, fromTitle, toTitle, options);\n\t\tif (newCaption) {\n\t\t\tif (newCaption.output) {\n\t\t\t\tif (this.canBeCaption(newCaption.output)) {\n\t\t\t\t\tcaption = newCaption.output;\n\t\t\t\t\tmodified = true;\n\t\t\t\t} else {\n\t\t\t\t\tentry.impossible = true;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (newCaption.impossible) {\n\t\t\t\tentry.impossible = true;\n\t\t\t}\n\t\t}\n\t}\n\t// I don't know why internal images links don't use the '#', but links\n\t// do, but that's just how it is.\n\t// image lines can have the '#' or not. Though if they do, they can't\n\t// have any escaping.\n\tvar hasHash = em[3].lastIndexOf('#') >= 0;\n\ttry {\n\t\tvar decodedLink = utils.decodeLink(link);\n\t\tif (hasHash || (isImage && (decodedLink === link))) {\n\t\t\tif (decodedLink === fromTitle) {\n\t\t\t\tlink = utils.encodeLink(toTitle);\n\t\t\t\tif (link !== toTitle && !hasHash) {\n\t\t\t\t\t// If this is an image without a hash, and the title\n\t\t\t\t\t// required any escaping at all, then we need to add one.\n\t\t\t\t\tem[3] = (em[3] || '') + '#';\n\t\t\t\t}\n\t\t\t\tmodified = true;\n\t\t\t}\n\t\t}\n\t} catch (e) {\n\t\t// It must be a malformed link. Not our problem.\n\t\t// Keep going in case the caption needs relinking.\n\t}\n\tif (modified) {\n\t\t// This way preserves whitespace\n\t\tentry.output = em[1]+\"[\"+caption+\"](\"+em[3]+link+em[5]+\")\";\n\t}\n\tif (modified || entry.impossible) {\n\t\treturn entry;\n\t}\n\treturn undefined;\n};\n\nexports.canBeCaption = function(caption) {\n\treturn this.indexOfClose(caption+']', -1, '[', ']') === caption.length;\n};\n\nexports.getEnclosed = function(text, pos, openChar, closeChar) {\n\tvar capEnd = this.indexOfClose(text, pos, openChar, closeChar);\n\tif (capEnd < 0) {\n\t\treturn undefined;\n\t}\n\tvar enclosed = text.substring(pos+1, capEnd);\n\tif (enclosed.match(/\\n\\s*\\n/)) {\n\t\t// Paragraph breaks are not allowed\n\t\treturn undefined;\n\t}\n\treturn enclosed;\n};\n\nexports.indexOfClose = function(text, pos, openChar, closeChar) {\n\tvar close = pos-1,\n\t\topen = pos; // First char is open\n\tdo {\n\t\tclose = text.indexOf(closeChar, close+1);\n\t\tif (close < 0) {\n\t\t\treturn -1;\n\t\t}\n\t\topen = text.indexOf(openChar, open+1);\n\t} while (open >= 0 && open <= close);\n\treturn close;\n};\n\nfunction extractUnquotedLink(text, pos) {\n\tvar p = pos;\n\tvar depth = 0;\n\twhile (true) {\n\t\tvar c = text.charAt(p);\n\t\tif (!c) {\n\t\t\treturn undefined;\n\t\t} else if (c === '(') {\n\t\t\tdepth++;\n\t\t} else if(c === ')') {\n\t\t\tif (depth === 0) {\n\t\t\t\treturn text.substring(pos, p);\n\t\t\t}\n\t\t\tdepth--;\n\t\t} else if (/\\s/.test(c)) {\n\t\t\tif (depth === 0) {\n\t\t\t\treturn text.substring(pos, p);\n\t\t\t}\n\t\t\treturn undefined;\n\t\t}\n\t\t++p;\n\t}\n};\n","module-type":"relinkmarkdownrule","type":"application/javascript"},"$:/plugins/flibbles/relink-markdown/text/markdowntext/markdownrulebase.js":{"title":"$:/plugins/flibbles/relink-markdown/text/markdowntext/markdownrulebase.js","text":"/*\\\ntitle: $:/plugins/flibbles/relink-markdown/text/markdowntext/markdownrulebase.js\ntype: application/javascript\nmodule-type: global\n\nBase class for markdown parser rules\n\n\\*/\n(function(){\n\n/*jslint node: true, browser: true */\n/*global $tw: false */\n\"use strict\";\n\nvar utils = require(\"$:/plugins/flibbles/relink-markdown/utils/markdown\");\n\nvar MarkdownRuleBase = function() {};\n\nMarkdownRuleBase.prototype.init = function(parser) {\n\tthis.parser = parser;\n};\n\nMarkdownRuleBase.prototype.findNextMatch = function(startPos) {\n\tthis.matchRegExp.lastIndex = startPos;\n\twhile (this.match = this.matchRegExp.exec(this.parser.source)) {\n\t\tif (utils.indexOfParagraph(this.match[0]) >= 0) {\n\t\t\tcontinue;\n\t\t}\n\t\tif (this.maxIndent !== undefined) {\n\t\t\tvar indent = utils.indentation(this.parser.source,this.match.index);\n\t\t\tif (indent < 0\n\t\t\t || (this.parser.indent !== undefined\n\t\t\t  && (indent > this.parser.indent + this.maxIndent))) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tvar nl = this.parser.source.lastIndexOf('\\n', this.match.index-1)+1;\n\t\t\tthis.indentString = this.parser.source.substring(nl, this.match.index);\n\t\t\treturn nl < startPos ? startPos : nl;\n\t\t}\n\t\treturn this.match.index;\n\t}\n\treturn undefined;\n};\n\n\nexports.MarkdownRuleBase = MarkdownRuleBase;\n\n})();\n","type":"application/javascript","module-type":"global"},"$:/plugins/flibbles/relink-markdown/text/markdowntext.js":{"title":"$:/plugins/flibbles/relink-markdown/text/markdowntext.js","text":"/*\\\nmodule-type: relinktext\ntitle: $:/plugins/flibbles/relink-markdown/text/markdowntext.js\ntype: application/javascript\n\nThis relinks tiddlers which contain markdown. It tries to be agnostic to\nwhichever markdown plugin you're using.\n\n\\*/\n\n/*jslint node: false, browser: true */\n/*global $tw: false */\n\"use strict\";\n\nvar markdownHandler = require('$:/plugins/flibbles/relink/js/utils.js').getType('markdown');\n\nexports.type = \"text/markdown\";\n\nexports.report = markdownHandler.report;\nexports.relink = markdownHandler.relink;\n","module-type":"relinktext","type":"application/javascript"},"$:/plugins/flibbles/relink-markdown/text/x-markdowntext.js":{"title":"$:/plugins/flibbles/relink-markdown/text/x-markdowntext.js","text":"/*\\\nmodule-type: relinktext\ntitle: $:/plugins/flibbles/relink-markdown/text/x-markdowntext.js\ntype: application/javascript\n\nsame as \"text/markdown\", but for \"text/x-markdown\"\n\n\\*/\n\n/*jslint node: false, browser: true */\n/*global $tw: false */\n\"use strict\";\n\nvar MarkdownType = require(\"./markdowntext.js\");\n\nfor (var member in MarkdownType) {\n\texports[member] = MarkdownType[member];\n}\n\nexports.type = \"text/x-markdown\";\n","module-type":"relinktext","type":"application/javascript"},"$:/plugins/flibbles/relink-markdown/utils/markdown.js":{"title":"$:/plugins/flibbles/relink-markdown/utils/markdown.js","text":"/*\\\nmodule-type: library\ntitle: $:/plugins/flibbles/relink-markdown/utils/markdown.js\ntype: application/javascript\n\nMethods used in markdown parsing.\n\n\\*/\n\n// tiddlywiki/markdown can't handle having these characters escaped, so we\n// need to unescape them.\n// But we're unescaping everything except '%' and ' ' so this list isn't used.\n/*\nvar problemChars = {\n\t\"23\": \"#\",\n\t\"24\": \"$\",\n\t\"26\": \"&\",\n\t\"2B\": \"+\",\n\t\"2C\": \",\",\n\t\"2F\": \"/\",\n\t\"3A\": \":\",\n\t\"3B\": \";\",\n\t\"3D\": \"=\",\n\t\"3F\": \"?\",\n\t\"40\": \"@\",\n};\n*/\n\nvar utils = require(\"$:/plugins/flibbles/relink/js/utils.js\");\nexports = module.exports = Object.create(utils);\n\nexports.encodeLink = function(title) {\n\tvar balance = 0,\n\t\tencoded = title.replace(/[\\(\\) %\\\\]/g, function(p) {\n\t\tswitch (p) {\n\t\tcase '(':\n\t\t\tif (balance >=1) {\n\t\t\t\treturn '%28';\n\t\t\t}\n\t\t\tbalance++;\n\t\t\tbreak;\n\t\tcase ')':\n\t\t\tif (balance <= 0) {\n\t\t\t\treturn '%29';\n\t\t\t}\n\t\t\tbalance--;\n\t\t\tbreak;\n\t\tcase '%':\n\t\t\treturn '%25';\n\t\tcase ' ':\n\t\t\treturn '%20';\n\t\tcase '\\\\':\n\t\t\treturn '%5C';\n\t\t}\n\t\treturn p;\n\t});\n\t// If there were open parenthesis without closes, we need to escape them.\n\twhile (balance--) {\n\t\tvar i = encoded.lastIndexOf('(');\n\t\tencoded = encoded.substr(0, i) + '%28' + encoded.substr(i+1);\n\t}\n\treturn encoded;\n};\n\nexports.decodeLink = function(link) {\n\tvar array = [];\n\tvar index = 0;\n\tvar slash;\n\twhile ((slash = link.indexOf('\\\\', index+1)) >= 0) {\n\t\tarray.push(link.substring(index,slash));\n\t\tindex = slash+1;\n\t}\n\tarray.push(link.substr(index));\n\treturn decodeURIComponent(array.join(''));\n};\n\n// Returns index of next paragraph, or -1\nexports.indexOfParagraph = function(text, startPos) {\n\tvar regExp = /\\n\\s*\\n/mg;\n\tregExp.lastIndex = startPos || 0;\n\tvar match = regExp.exec(text);\n\treturn match ? regExp.lastIndex : -1;\n};\n\n/** Returns how much indentation there is between pos and the previous\n * newline (or other char).\n * tabs are counted as 4 chars.\n */\nexports.indentation = function(text, pos, startChar) {\n\tvar p = text.lastIndexOf(startChar || '\\n', pos-1);\n\tvar count = 0;\n\twhile (++p < pos) {\n\t\tvar c = text.charAt(p);\n\t\tif (c === ' ') {\n\t\t\tcount++;\n\t\t} else if (c === '\\t') {\n\t\t\tcount = count + 4 - (count%4);\n\t\t} else {\n\t\t\treturn -1;\n\t\t}\n\t}\n\treturn count;\n};\n\n\nexports.getSettings = function(wiki) {\n\t// Stored here so it's only calculated once, but also so it's different\n\t// per tiddler for testing\n\tif (wiki._markdownSettings === undefined) {\n\t\tvar settings = Object.create(null);\n\t\tvar text = wiki.getTiddlerText(\"$:/config/markdown/renderWikiText\");\n\t\tsettings.wikitext =  (text === undefined || text.toLowerCase() === \"true\");\n\t\ttext = wiki.getTiddlerText(\"$:/config/markdown/renderWikiTextPragma\");\n\t\tif (text) {\n\t\t\ttext = text.trim() + '\\n';\n\t\t} else {\n\t\t\ttext = '';\n\t\t}\n\t\tsettings.wikitextPragma = text;\n\t\twiki._markdownSettings = settings;\n\t}\n\treturn wiki._markdownSettings;\n};\n\n/**I don't actually use this, but I've kept the code around anyway.\n * The only time this plugin is installed and markdown isn't enabled would\n * be if the user forgot to install a markdown plugin, or they disabled it.\n * I GUESS Relink should still be Relinking markdown in that case.\n */\nexports.markdownEnabled = function() {\n\tif (_enabled === undefined) {\n\t\tvar test = $tw.wiki.renderText(\"text/html\", \"text/x-markdown\", \"[test](#test)\");\n\t\t_enabled = (test.indexOf(\"<a\") >= 0);\n\t}\n\treturn _enabled;\n};\nvar _enabled;\n","module-type":"library","type":"application/javascript"},"$:/plugins/flibbles/relink-markdown/readme":{"title":"$:/plugins/flibbles/relink-markdown/readme","text":"Markdown support for Relink.\n\n...or Relink support for Markdown.\n\nThis supplimental plugin allows ''text/x-markdown'' files to be relinked. This means markdown linking syntax like `[Caption](#MyTiddler)` is supported. If your markdown supports wikitext markup as well, this will handle that too.\n\nThis plugin is designed for `tiddlywiki/markdown`, but it will also work for `anstosa/tw5-markdown` and others.\n"}}}