New partialRendering extension
This commit is contained in:
parent
bbca97d0ff
commit
4ee5e5bbb0
@ -5,6 +5,7 @@ define([
|
||||
"classes/Extension",
|
||||
"settings",
|
||||
"text!html/settingsExtensionsAccordion.html",
|
||||
"extensions/partialRendering",
|
||||
"extensions/userCustom",
|
||||
"extensions/googleAnalytics",
|
||||
"extensions/dialogAbout",
|
||||
|
@ -61,6 +61,9 @@ define([
|
||||
editor.hooks.chain("onPreviewRefresh", prettyPrint);
|
||||
}
|
||||
Markdown.Extra.init(converter, options);
|
||||
|
||||
// Store extensions list in converter for partialRendering
|
||||
converter.extraExtensions = markdownExtra.config.extensions;
|
||||
};
|
||||
|
||||
return markdownExtra;
|
||||
|
101
js/extensions/partialRendering.js
Normal file
101
js/extensions/partialRendering.js
Normal file
@ -0,0 +1,101 @@
|
||||
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 sectionList = [];
|
||||
var convertedSectionsList = [];
|
||||
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(text.substring(offset, matchOffset) + "\n" + linkDefinition);
|
||||
offset = matchOffset;
|
||||
}
|
||||
}
|
||||
return "";
|
||||
});
|
||||
// Last section
|
||||
newSectionList.push(text.substring(offset, text.length) + linkDefinition);
|
||||
|
||||
sectionList = newSectionList;
|
||||
}
|
||||
|
||||
var isRendering = false;
|
||||
function renderSections() {
|
||||
// Renders sections
|
||||
isRendering = true;
|
||||
convertedSectionsList = _.map(sectionList, converter.makeHtml);
|
||||
isRendering = false;
|
||||
|
||||
$("#wmd-preview").html(convertedSectionsList.join(""));
|
||||
|
||||
// Move footnotes in the footer...
|
||||
if(hasFootnotes === true) {
|
||||
// Recreate a footnote list
|
||||
var footnoteElts = $("<ol>");
|
||||
$("#wmd-preview > div.footnotes > ol > li").each(function(index) {
|
||||
hasFootnotes = true;
|
||||
var elt = $(this);
|
||||
footnoteElts.append(elt);
|
||||
// Restore footnotes numbers
|
||||
var refId = "#fnref\\:" + elt.attr("id").substring(3);
|
||||
$(refId).text(index + 1);
|
||||
});
|
||||
// Append the whole footnotes at the end of the document
|
||||
$("#wmd-preview > div.footnotes").remove();
|
||||
$("#wmd-preview").append($('<div class="footnotes">').append("<hr>").append(footnoteElts));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
partialRendering.onEditorConfigure = function(editor) {
|
||||
converter = editor.getConverter();
|
||||
converter.hooks.chain("preConversion", function(text) {
|
||||
if(isRendering === true) {
|
||||
return text;
|
||||
}
|
||||
extractSections(text);
|
||||
return "";
|
||||
});
|
||||
editor.hooks.chain("onPreviewRefresh", function() {
|
||||
$("#wmd-preview").html(renderSections());
|
||||
});
|
||||
};
|
||||
|
||||
return partialRendering;
|
||||
});
|
7
js/html/partialRenderingSettingsBlock.html
Normal file
7
js/html/partialRenderingSettingsBlock.html
Normal file
@ -0,0 +1,7 @@
|
||||
<p>Binds together editor and preview scrollbars.</p>
|
||||
<blockquote class="muted">
|
||||
<b>NOTE:</b> The mapping between Markdown and HTML is based on the
|
||||
position of the title elements (h1 h2 ...) in the page. Therefore if
|
||||
your document does not contain any title the mapping will be linear and
|
||||
consequently less accurate.
|
||||
</blockquote>
|
Loading…
Reference in New Issue
Block a user