javascript - Markdown-it not working, Throwing errors on page load -


i trying use markdown-it js take markdown content out of html element on page, , render html (say, during page load). in document ready function below, have used code similar way described in documentation.

no matter do, getting 1 of these errors

  • typeerror: window.markdownit not function mid.html:101:22
  • error: mismatched anonymous define() module: function (){var e;return function r(e,t,n){function s(o,a){if(!t[o]){if(!e[o ...
  • e.src not defined
  • require not defined

what doing wrong or missing? eye-opening insights!

<!doctype html> <html>  <head>   <!-- <title>markdown in js</title> -->   <meta charset="utf-8"/> </head>  <body> <title>hello markdown</title>  <xmp id="markdown" style="display: none;"> # markdown text goes in here  ## chapter 1  lorem ipsum dolor sit amet, consectetur adipisicing elit, sed eiusmod tempor incididunt ut labore et dolore magna aliqua.  ## chapter 2  ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.  ## emphasis  **this bold text**  __this bold text__  *this italic text*  _this italic text_  ~~strikethrough~~  ## footnotes  footnote 1 link[^first].  footnote 2 link[^second].  inline footnote^[text of inline footnote] definition.  ## blockquotes  > blockquotes can nested...  ```xml <plugin>     <groupid>org.apache.maven.plugins</groupid>     <artifactid>maven-jar-plugin</artifactid>     <version>2.4</version>     .... </plugin> ```  ## code  inline `code`  indented code      // comments     line 1 of code     line 2 of code     line 3 of code   block code "fences"  ``` sample text here... ```  syntax highlighting  ``` js var foo = function (bar) {   return bar++; };  ---  [^first]: footnote **can have markup**  , multiple paragraphs.  [^second]: footnote text. </xmp>  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/markdown-it/4.4.0/markdown-it.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.20/require.min.js"></script>  <script>    $(function() {          var xmp = $('xmp#markdown');          var mdtext = xmp.innerhtml; // take content in xmp#markdown input           var md = window.markdownit();                 /*.use(require('markdown-it-abbr'))                 .use(require('markdown-it-container'), 'warning')                 .use(require('markdown-it-deflist'))                 .use(require('markdown-it-emoji'))                 .use(require('markdown-it-footnote'))                 .use(require('markdown-it-ins'))                 .use(require('markdown-it-mark'))                 .use(require('markdown-it-sub'))                 .use(require('markdown-it-sup'));*/           // howto: render xmp#markdown content html          var resultinline = md.renderinline(mdtext);          // or use xmp.innerhtml = md.render(mdtext);    }); </script> </body> </html> 

thanks vivek ragunathan

the require mentioned in documentation nodejs require, , not requirejs. if want use in browser, you'll have create build using npm , browserify.

in case, loading file should enough (fiddle):

$(function () {     var xmp = $('xmp#markdown');     var mdtext = xmp.html(); // take content in xmp#markdown input - use .html() because jquery object      var md = window.markdownit();      // howto: render xmp#markdown content html     $('#result').html(md.render(mdtext)); }); 

note because xmp using jquery $('xmp#markdown');, have use .html(), , not `.innerhtml'.


Comments