Simplified extensions implementation

This commit is contained in:
Titus 2012-06-20 17:02:53 -06:00
parent 644fb7b983
commit 4234de3a91

View File

@ -64,7 +64,21 @@
// //
// Showdown namespace // Showdown namespace
// //
var Showdown = {}; var Showdown = { extensions: {} };
//
// forEach
//
var forEach = Showdown.forEach = function(obj, callback) {
if (typeof obj.forEach === 'function') {
obj.forEach(callback);
} else {
var i, len = obj.length;
for (i = 0; i < len; i++) {
callback(obj[i], i, obj);
}
}
};
// //
// converter // converter
@ -88,8 +102,8 @@ var g_html_blocks;
var g_list_level = 0; var g_list_level = 0;
// Global extensions // Global extensions
var g_lang_extensions = new Showdown.Array(); var g_lang_extensions = [];
var g_output_modifiers = new Showdown.Array(); var g_output_modifiers = [];
// //
@ -100,22 +114,20 @@ var g_output_modifiers = new Showdown.Array();
if (converter_options && converter_options.extensions) { if (converter_options && converter_options.extensions) {
// Iterate over each plugin // Iterate over each plugin
var plugins = new Showown.Array(converter_options.extensions); converter_options.extensions.forEach(function(plugin){
plugins.forEach(function(plugin){
// Iterate over each extensino within that plugin // Iterate over each extensino within that plugin
var extensions = new Showdown.Array(plugin(this)); plugin(this).forEach(function(ext){
extensions.forEach(function(ext){
// Sort extensions by type // Sort extensions by type
if (x.type) { if (ext.type) {
if (x.type === 'language' || x.type === 'lang') { if (ext.type === 'language' || ext.type === 'lang') {
g_lang_extensions.push(x); g_lang_extensions.push(ext);
} else if (x.type === 'output' || x.type === 'html') { } else if (ext.type === 'output' || ext.type === 'html') {
g_output_modifiers.push(x); g_output_modifiers.push(ext);
} }
} else { } else {
// Assume language extension // Assume language extension
g_lang_extensions.push(x); g_output_modifiers.push(ext);
} }
}); });
@ -1390,26 +1402,5 @@ var escapeCharacters_callback = function(wholeMatch,m1) {
} // end of Showdown.converter } // end of Showdown.converter
//
// Showdown.Array
// (some) ES5 methods that don't modify Array.prototype
//
Showdown.Array = function(baseArray) { this.base = baseArray ? baseArray : []; }
Showdown.Array.prototype = [];
(function(array, parent) {
array.forEach = function(callback) {
if (this.base.forEach) { this.base.forEach(callback); }
else {
var i, len = this.base.length;
for (i = 0; i < len; i++) {
callback(this.base[i], i, this.base);
}
}
};
}(Showdown.Array.prototype, Array.prototype));
// export // export
if (typeof module !== 'undefined') module.exports = Showdown; if (typeof module !== 'undefined') module.exports = Showdown;