فهرست منبع

fix bug with this scope handling and extension converter argument

* `this` scope is captured as `self`, so that it can be properly passed to `Showdown.forEach(plugin(self)` otherwise,
  extension plugin will end up with converter pointing to global scope.
* because Showdown is not using proper prototype chain (i.e. references to yet to be defined functions), I had to
   move the extension handling below makeHtml so that it is defined and available within the plugin itself
Pascal Deschenes 12 سال پیش
والد
کامیت
0440ef313e
1فایلهای تغییر یافته به همراه37 افزوده شده و 36 حذف شده
  1. 37 36
      src/showdown.js

+ 37 - 36
src/showdown.js

@@ -135,42 +135,6 @@ if (typeof module !== 'undefind' && typeof exports !== 'undefined' && typeof req
 	}
 }
 
-//
-// Options:
-//
-
-// Parse extensions options into separate arrays
-if (converter_options && converter_options.extensions) {
-
-	// Iterate over each plugin
-	Showdown.forEach(converter_options.extensions, function(plugin){
-
-		// Assume it's a bundled plugin if a string is given
-		if (typeof plugin === 'string') {
-			plugin = Showdown.extensions[stdExtName(plugin)];
-		}
-
-		if (typeof plugin === 'function') {
-			// Iterate over each extension within that plugin
-			Showdown.forEach(plugin(this), function(ext){
-				// Sort extensions by type
-				if (ext.type) {
-					if (ext.type === 'language' || ext.type === 'lang') {
-						g_lang_extensions.push(ext);
-					} else if (ext.type === 'output' || ext.type === 'html') {
-						g_output_modifiers.push(ext);
-					}
-				} else {
-					// Assume language extension
-					g_output_modifiers.push(ext);
-				}
-			});
-		} else {
-			throw "Extension '" + plugin + "' could not be loaded.  It was either not found or is not a valid extension.";
-		}
-	});
-}
-
 this.makeHtml = function(text) {
 //
 // Main function. The order in which other subs are called here is
@@ -246,6 +210,43 @@ this.makeHtml = function(text) {
 
 	return text;
 };
+//
+// Options:
+//
+
+// Parse extensions options into separate arrays
+if (converter_options && converter_options.extensions) {
+
+  var self = this;
+
+	// Iterate over each plugin
+	Showdown.forEach(converter_options.extensions, function(plugin){
+
+		// Assume it's a bundled plugin if a string is given
+		if (typeof plugin === 'string') {
+			plugin = Showdown.extensions[stdExtName(plugin)];
+		}
+
+		if (typeof plugin === 'function') {
+			// Iterate over each extension within that plugin
+			Showdown.forEach(plugin(self), function(ext){
+				// Sort extensions by type
+				if (ext.type) {
+					if (ext.type === 'language' || ext.type === 'lang') {
+						g_lang_extensions.push(ext);
+					} else if (ext.type === 'output' || ext.type === 'html') {
+						g_output_modifiers.push(ext);
+					}
+				} else {
+					// Assume language extension
+					g_output_modifiers.push(ext);
+				}
+			});
+		} else {
+			throw "Extension '" + plugin + "' could not be loaded.  It was either not found or is not a valid extension.";
+		}
+	});
+}
 
 
 var _ExecuteExtension = function(ext, text) {