Embed javascript in markdown

2019-01-31 06:32发布

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

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

<script src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script>
...do stuff...
</script>

but it complains when I render it with a multitude of errors. The first one being

 ___________________________________________________________________________
| Maruku tells you:
+---------------------------------------------------------------------------
| Could you please format this better?
| I see that "<script type='text/javascript'>" is left after the raw HTML.
| At line 31
|   raw_html     |<script src='http://code.jquery.com/jquery-1.4.2.min.js' /><script type='text/javascript'>|
|       text --> |//<![CDATA[|

and then the rest seems like the parser is going nuts. Then it renders the javascript into a div on the page. I've tried making it a CDATA block and extra spacing between the jquery and my script.

Help?

8条回答
放我归山
2楼-- · 2019-01-31 06:36

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.

查看更多
劫难
3楼-- · 2019-01-31 06:37

You could use pandoc, which handles this input (and javascript generally) just fine.

查看更多
Viruses.
4楼-- · 2019-01-31 06:43

The example they give on their site shows an empty <script> tag containing a newline. Maybe that's it?

查看更多
贼婆χ
5楼-- · 2019-01-31 06:45

Different solution that might work in some cases: (the selected answer didn't work for me when I was trying to embed a CodePen example)

  • add this to your default layout:

    <!-- Custom JavaScript files set in YAML front matter -->
    {% for js in page.customjs %}
    <script async type="text/javascript" src="{{ js }}"></script>
    {% endfor %}
    
  • In posts where you need some JavaScript files, you can add them in the YAML front matter like so:

    ---
    layout: post
    title: Adding custom JavaScript for a specific post
    category: posts
    customjs:
     - http://code.jquery.com/jquery-1.4.2.min.js
     - http://yourdomain.com/yourscript.js
    ---
    

The async might not be necessary or wanted but you could probably add that as a parameter in customjs. (see YAML front matter for Jekyll and nested lists for details)

查看更多
Summer. ? 凉城
6楼-- · 2019-01-31 06:49

Markdown supports inline XHTML but not Javascript.

查看更多
太酷不给撩
7楼-- · 2019-01-31 06:53

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

查看更多
登录 后发表回答