define([ "jquery", "underscore", "classes/Extension", "text!html/partialRenderingSettingsBlock.html", ], function($, _, Extension, partialRenderingSettingsBlockHTML) { var partialRendering = new Extension("partialRendering", "Partial rendering", true); partialRendering.settingsBlock = partialRenderingSettingsBlockHTML; var converter = undefined; var sectionIdGenerator = 0; var sectionList = [ { text: "" } ]; var sectionsToRemove = undefined; function updateSectionList(newSectionList) { // Find modified sections starting from left var leftIndex = sectionList.length; _.some(sectionList, function(section, index) { if(index >= newSectionList.length || section.text != newSectionList[index].text) { leftIndex = index; return true; } }); // Find modified sections starting from right var rightIndex = -sectionList.length; _.some(sectionList.slice().reverse(), function(section, index) { if(index >= newSectionList.length || section.text != newSectionList[newSectionList.length - index - 1].text) { rightIndex = -index; return true; } }); if(leftIndex === sectionList.length && rightIndex === -leftIndex) { // No modification detected... return; } // Create an array composed of left unmodified, modified, right // unmodified sections var leftSections = sectionList.slice(0, leftIndex); var modifiedSections = newSectionList.slice(leftIndex, newSectionList.length + rightIndex); var rightSections = sectionList.slice(sectionList.length + rightIndex, sectionList.length); sectionsToRemove = sectionList.slice(leftIndex, sectionList.length + rightIndex); sectionList = leftSections.concat(modifiedSections).concat(rightSections); } var hasFootnotes = false; function extractSections(text) { text += "\n\n"; // Strip link definitions var linkDefinition = ""; text = text.replace(/^[ ]{0,3}\[(.+)\]:[ \t]*\n?[ \t]*(\S+?)>?(?=\s|$)[ \t]*\n?[ \t]*((\n*)["(](.+?)[")][ \t]*)?(?:\n+)/gm, function(wholeMatch) { linkDefinition += wholeMatch; return ""; }); // And eventually footnotes... hasFootnotes = false; var doFootnotes = _.some(converter.extraExtensions, function(extension) { return extension == "footnotes"; }); if(doFootnotes) { text = text.replace(/\n[ ]{0,3}\[\^(.+?)\]\:[ \t]*\n?([\s\S]*?)\n{1,2}((?=\n[ ]{0,3}\S)|$)/g, function(wholeMatch) { hasFootnotes = true; linkDefinition += wholeMatch; return ""; }); } // Look for titles var newSectionList = []; var offset = 0; text.replace(/^```.*\n[\s\S]*?\n```|(^.+[ \t]*\n=+[ \t]*\n+|^.+[ \t]*\n-+[ \t]*\n+|^\#{1,6}[ \t]*.+?[ \t]*\#*\n+)/gm, function(match, title, matchOffset) { if(title) { // We just found a title which means end of the previous section if(matchOffset > offset) { newSectionList.push({ id: ++sectionIdGenerator, text: text.substring(offset, matchOffset) + "\n" + linkDefinition }); offset = matchOffset; } } return ""; }); // Last section newSectionList.push({ id: ++sectionIdGenerator, text: text.substring(offset, text.length) + linkDefinition }); updateSectionList(newSectionList); } var isRendering = false; var footnoteContainer = $('