Embed javascript in markdown

后端 未结 11 2507
[愿得一人]
[愿得一人] 2020-12-13 02:23

I\'m using the Maruku markdown processor. I\'d like this

*blah* blah \"blah\" in [markdown](blah)



        
相关标签:
11条回答
  • 2020-12-13 02:37

    I built an express server with a library called Showdown that converts markdown to HTML, and also will let you use HTML in your markdown file, and this is how I am able to use javascript and also link to my css file.

    TOC.md

    <script src="/js/toc"></script>
    

    server.js

    app.get('/js/toc', (req, res) => {
        fs.readFile(path.join(__dirname, 'public', 'js', 'toc.js'), 'utf-8', (err, data) => {
            if(err) throw err;
            res.set({
                'Content-Type': 'text/javascript'
            })
            res.send(data)
        })
    })
    

    Or you could do it using express static middleware. You would just need to put your javascript file inside a folder called public.

    TOC.md

    <script src="/static/js/toc.js"></script>
    

    server.js

    app.use('/static', express.static('public'))
    
    0 讨论(0)
  • 2020-12-13 02:37

    A good idea is to have local and cloud js sources separated:

    In the post file:

    cloudjs:
     - //cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.min.js
     - //cdnjs.cloudflare.com/ajax/libs/topojson/1.6.9/topojson.min.js
    localjs:
     - datamaps.world.min.js
     - custom.js  
    

    In the default file after footer inclusion:

       {% for js in page.cloudjs %}
    
            <script type="text/javascript" src="{{ js }}"></script>
    
        {% endfor %}
    
    
        {% for js in page.localjs %}
    
            <script type="text/javascript" src="{{ "/assets/scripts/" | prepend: site.baseurl | append: js }}"></script>
    
        {% endfor %}
    
    0 讨论(0)
  • 2020-12-13 02:37

    Just indent the first line contains < script > tag.

    0 讨论(0)
  • 2020-12-13 02:38

    I had this same problem, but I managed to get JavaScript to appear in my code by putting a newline after the opening tag.

    0 讨论(0)
  • 2020-12-13 02:50

    To my experience, markdown will outpus javascript text as plain text as long as you remove the code formatting that may confuse markdown.

    1. remove comments from javascript, as /* ... */ is translated to < em>
    2. remove the space indent in the front of each line. < p> may be inserted according to your indentation.

    Basically what I do is to review the generated html and find out what extra tags are inserted in between my javascript code by markdown. And remove the formatting that generates the extra tag.

    0 讨论(0)
  • 2020-12-13 02:51

    I found that escaping the closing '>' symbol in both, the opening and closing 'script' tags, will display it correctly, for example:

    • If you type the follwing:

      <script\>... javascript code...</script\>
      
    • It will be rendered like this:

      <script>... javascript code...</script>
      

    That's just my two cents.

    0 讨论(0)
提交回复
热议问题