Permalink
Browse files

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
  • Loading branch information...
1 parent dc28410 commit 0440ef313e2181e901daee24ed05894bb96720d8 @pdeschen pdeschen committed Nov 1, 2012
Showing with 37 additions and 36 deletions.
  1. +37 −36 src/showdown.js
View
@@ -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) {

0 comments on commit 0440ef3

Please sign in to comment.