Stackedit/public/res/extensions/markdownSectionParser.js

123 lines
5.0 KiB
JavaScript
Raw Normal View History

define([
2013-11-17 22:59:03 +00:00
"underscore",
"extensions/markdownExtra",
"extensions/mathJax",
2014-03-30 01:44:51 +00:00
"extensions/partialRendering",
2013-11-17 22:59:03 +00:00
"classes/Extension",
2014-03-30 01:44:51 +00:00
"crel",
], function(_, markdownExtra, mathJax, partialRendering, Extension, crel) {
var markdownSectionParser = new Extension("markdownSectionParser", "Markdown section parser");
2013-11-07 23:10:38 +00:00
var eventMgr;
markdownSectionParser.onEventMgrCreated = function(eventMgrParameter) {
eventMgr = eventMgrParameter;
};
2014-03-19 00:33:57 +00:00
2014-03-17 02:01:46 +00:00
var sectionList = [];
2014-03-30 01:44:51 +00:00
var previewContentsElt;
2014-03-19 00:33:57 +00:00
2014-03-17 02:01:46 +00:00
// Regexp to look for section delimiters
var regexp = '^.+[ \\t]*\\n=+[ \\t]*\\n+|^.+[ \\t]*\\n-+[ \\t]*\\n+|^\\#{1,6}[ \\t]*.+?[ \\t]*\\#*\\n+'; // Title delimiters
markdownSectionParser.onPagedownConfigure = function(editor) {
2013-11-21 17:47:19 +00:00
if(markdownExtra.enabled) {
2013-11-17 22:59:03 +00:00
if(_.some(markdownExtra.config.extensions, function(extension) {
return extension == "fenced_code_gfm";
})) {
regexp = '^```.*\\n[\\s\\S]*?\\n```|' + regexp; // Fenced block delimiters
}
}
2013-11-21 17:47:19 +00:00
if(mathJax.enabled) {
2013-11-17 22:59:03 +00:00
// Math delimiter has to follow 1 empty line to be considered as a section delimiter
regexp = '^[ \\t]*\\n\\$\\$[\\s\\S]*?\\$\\$|' + regexp; // $$ math block delimiters
regexp = '^[ \\t]*\\n\\\\\\\\[[\\s\\S]*?\\\\\\\\]|' + regexp; // \\[ \\] math block delimiters
regexp = '^[ \\t]*\\n\\\\?\\\\begin\\{[a-z]*\\*?\\}[\\s\\S]*?\\\\end\\{[a-z]*\\*?\\}|' + regexp; // \\begin{...} \\end{...} math block delimiters
2013-11-17 22:59:03 +00:00
}
regexp = new RegExp(regexp, 'gm');
2014-03-19 00:33:57 +00:00
var converter = editor.getConverter();
2014-03-30 01:44:51 +00:00
if(!partialRendering.enabled) {
converter.hooks.chain("preConversion", function() {
return _.reduce(sectionList, function(result, section) {
return result + '\n<div class="se-preview-section-delimiter"></div>\n\n' + section.text + '\n\n';
}, '');
});
editor.hooks.chain("onPreviewRefresh", function() {
var wmdPreviewElt = document.getElementById("wmd-preview");
var childNode = wmdPreviewElt.firstChild;
function createSectionElt() {
var sectionElt = crel('div', {
class: 'wmd-preview-section preview-content'
});
var isNextDelimiter = false;
while (childNode) {
var nextNode = childNode.nextSibling;
var isDelimiter = childNode.className == 'se-preview-section-delimiter';
if(isNextDelimiter === true && childNode.tagName == 'DIV' && isDelimiter) {
// Stop when encountered the next delimiter
break;
}
isNextDelimiter = true;
isDelimiter || sectionElt.appendChild(childNode);
childNode = nextNode;
}
return sectionElt;
}
var newSectionEltList = document.createDocumentFragment();
sectionList.forEach(function(section) {
newSectionEltList.appendChild(createSectionElt(section));
});
previewContentsElt.innerHTML = '';
previewContentsElt.appendChild(wmdPreviewElt);
previewContentsElt.appendChild(newSectionEltList);
});
}
};
markdownSectionParser.onReady = function() {
previewContentsElt = document.getElementById("preview-contents");
};
2014-03-19 00:33:57 +00:00
2014-03-29 01:22:24 +00:00
var fileDesc;
markdownSectionParser.onFileSelected = function(fileDescParam) {
fileDesc = fileDescParam;
};
2014-03-17 02:01:46 +00:00
var sectionCounter = 0;
2014-03-29 01:22:24 +00:00
function parseFileContent(fileDescParam, content) {
if(fileDescParam !== fileDesc) {
return;
}
2014-03-21 00:36:28 +00:00
var frontMatter = (fileDesc.frontMatter || {})._frontMatter || '';
2014-03-23 02:33:41 +00:00
var text = content.substring(frontMatter.length);
2014-03-17 02:01:46 +00:00
var tmpText = text + "\n\n";
function addSection(startOffset, endOffset) {
var sectionText = tmpText.substring(offset, endOffset);
sectionList.push({
id: ++sectionCounter,
2014-03-19 00:33:57 +00:00
text: sectionText,
textWithFrontMatter: frontMatter + sectionText
2014-03-17 02:01:46 +00:00
});
2014-03-19 00:33:57 +00:00
frontMatter = '';
2014-03-17 02:01:46 +00:00
}
sectionList = [];
var offset = 0;
// Look for delimiters
tmpText.replace(regexp, function(match, matchOffset) {
// Create a new section with the text preceding the delimiter
addSection(offset, matchOffset);
offset = matchOffset;
});
// Last section
addSection(offset, text.length);
eventMgr.onSectionsCreated(sectionList);
}
2014-03-19 00:33:57 +00:00
2014-03-17 02:01:46 +00:00
markdownSectionParser.onFileOpen = parseFileContent;
markdownSectionParser.onContentChanged = parseFileContent;
return markdownSectionParser;
2014-03-19 00:33:57 +00:00
});