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 sectionCounter = 0; var sectionList = []; var linkDefinition = undefined; var sectionsToRemove = []; var modifiedSections = []; var insertAfterSection = undefined; var fileChanged = false; function updateSectionList(newSectionList, newLinkDefinition) { modifiedSections = []; sectionsToRemove = []; insertAfterSection = undefined; // Render everything if file or linkDefinition changed if(fileChanged === true || linkDefinition != newLinkDefinition) { fileChanged = false; linkDefinition = newLinkDefinition; sectionsToRemove = sectionList; sectionList = newSectionList; modifiedSections = newSectionList; return; } // Find modified sections starting from top 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 bottom 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); modifiedSections = newSectionList.slice(leftIndex, newSectionList.length + rightIndex); var rightSections = sectionList.slice(sectionList.length + rightIndex, sectionList.length); insertAfterSection = _.last(leftSections); sectionsToRemove = sectionList.slice(leftIndex, sectionList.length + rightIndex); sectionList = leftSections.concat(modifiedSections).concat(rightSections); } var doFootnotes = false; var hasFootnotes = false; partialRendering.onSectionsCreated = function(sectionListParam) { var newSectionList = []; var newLinkDefinition = ""; hasFootnotes = false; _.each(sectionListParam, function(text) { text += "\n\n"; // Strip link definitions text = text.replace(/^```.*\n[\s\S]*?\n```|^[ ]{0,3}\[(.+)\]:[ \t]*\n?[ \t]*(\S+?)>?(?=\s|$)[ \t]*\n?[ \t]*((\n*)["(](.+?)[")][ \t]*)?(?:\n+)/gm, function(wholeMatch, link) { if(link) { newLinkDefinition += wholeMatch; return ""; } return wholeMatch; }); // And footnotes eventually if(doFootnotes) { text = text.replace(/^```.*\n[\s\S]*?\n```|\n[ ]{0,3}\[\^(.+?)\]\:[ \t]*\n?([\s\S]*?)\n{1,2}((?=\n[ ]{0,3}\S)|$)/g, function(wholeMatch, footnote) { if(footnote) { hasFootnotes = true; newLinkDefinition += wholeMatch; return ""; } return wholeMatch; }); } // Skip space only sections if(/\S/.test(text)) { // Add section to the newSectionList newSectionList.push({ id: ++sectionCounter, text: text }); } }); updateSectionList(newSectionList, newLinkDefinition); }; var footnoteContainerElt = $('