showdownjs / showdown

A bidirectional Markdown to HTML to Markdown converter written in Javascript
http://www.showdownjs.com/
MIT License
14.26k stars 1.56k forks source link

Dynamic loading of showdown showdown global is undefined. #729

Closed cbuteau closed 5 years ago

cbuteau commented 5 years ago

Hello,

I work for a big company that is heistant to use 3rp party OS software. I am working on a demo to have embedded documentation in our application.

I want to get the technical details nailed with a specific vrsion before I approach anyone higher up the ladder.

I previously used showdown in node js to make a script converting markdown to html for a small wiki site. It was easy to use and flexible.,

I have a script to load showdown dynamically.

  function loadScript(url, callback) {
      // Adding the script tag to the head as suggested before
      var head = document.getElementsByTagName('head')[0];
      var script = document.createElement('script');
      script.type = 'text/javascript';
      script.src = url;

      // Then bind the event to the callback function.
      // There are several events for cross browser compatibility.
      script.onreadystatechange = callback;
      script.onload = callback;

      script.onerror = function(err) {
        console.error(err);
      };

      // Fire the loading
      head.appendChild(script);
  }

The script loads and I have loaded from the following points.

var SHOWDOWN_URL_old = 'https://cdnjs.cloudflare.com/ajax/libs/showdown/1.9.0/showdown.min.js';
  var SHOWDOWN_URL_ver = 'https://cdn.rawgit.com/showdownjs/showdown/1.8.0/dist/showdown.min.js';
  var SHOWDOWN_URL = 'https://rawgit.com/showdownjs/showdown/develop/dist/showdown.min.js';
  var SHOWDOWN_DEBUG_URL = 'https://cdn.rawgit.com/showdownjs/showdown/1.8.0/dist/showdown.js';

they all load but there is no showdown global...

what am I missing?

Thank you for your time. -Christopher Buteau

obedm503 commented 5 years ago

tested this, and your script does work. https://jsfiddle.net/8hd1ojp0/

Are you making sure to use showdown after the loaded event (the callback)?

cbuteau commented 5 years ago

I debugged library and it is because our product is using requirejs that I do not get a global. I do not have access to requirejs config to add it with a path.

I will have to abandon idea. Thanks for your feedback.

cbuteau commented 5 years ago

I closed this but I did try the following...

require([SHOWDOWN_URL], function(showdown) {
    var converter = new showdown.Converter({
         tables: true
    });
});

and it worked...