在 Markdown 中嵌入 JavaScript

发布于 2024-08-30 17:02:05 字数 861 浏览 8 评论 0原文

我正在使用 Maruku markdown 处理器。我想要这个,

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

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

但是当我渲染它时出现大量错误时它会抱怨。第一个是

 ___________________________________________________________________________
| 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[|

,然后其余的似乎解析器都疯了。然后它将 javascript 渲染到页面上的 div 中。我尝试将其设为 CDATA 块并在 jquery 和我的脚本之间添加额外的间距。

帮助?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

森林迷了鹿 2024-09-06 17:02:05

我遇到了同样的问题,但我设法通过在开始标记后添加换行符来使 JavaScript 出现在我的代码中。

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

林空鹿饮溪 2024-09-06 17:02:05

在某些情况下可能有效的不同解决方案:(当我尝试嵌入 CodePen 示例时,所选答案对我不起作用)

  • 将其添加到您的默认布局中:

    
    {% 用于 page.customjs 中的 js %}
    <脚本异步类型=“text/javascript”src=“{{ js }}”>
    {% 结束 %}
    
  • 在需要一些 JavaScript 文件的帖子中,您可以将它们添加到 YAML 前面的内容中,如下所示:

    <前><代码>---
    布局:帖子
    标题:为特定帖子添加自定义 JavaScript
    类别: 帖子
    定制:
    - http://code.jquery.com/jquery-1.4.2.min.js
    - http://yourdomain.com/yourscript.js
    ---

async 可能不是必需的或不需要的,但您可以添加它作为 customjs 中的参数。 (有关详细信息,请参阅 Jekyll 和嵌套列表的 YAML front Matter

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)

灼疼热情 2024-09-06 17:02:05

我使用这段代码在 Markdown 中编写 JavaScript 代码。

只需在 "```" 之后添加 js ,您的 JavaScript 代码就会突出显示。


 ```js
   const myvar = "hello"
   module.exports.response = response = ()=>{mycode here}
    ```

I use this code to write JavaScript code in markdown.

just add js after "```" , and you'll have your JavaScript code highlighted.


 ```js
   const myvar = "hello"
   module.exports.response = response = ()=>{mycode here}
    ```
耶耶耶 2024-09-06 17:02:05

Markdown 支持内联 XHTML,但不支持 Javascript。

Markdown supports inline XHTML but not Javascript.

Saygoodbye 2024-09-06 17:02:05

他们在他们的网站上提供的示例显示了一个空的

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

最近可好 2024-09-06 17:02:05

我发现转义结束 '>'开始和结束“script”标签中的符号将正确显示,例如:

  • 如果您键入以下内容:

    ;
    
  • 它将呈现如下:

    ;
    

这只是我的两分钱。

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.

听,心雨的声音 2024-09-06 17:02:05

我用一个名为 Showdown 的库构建了一个 Express 服务器,该库将 Markdown 转换为 HTML,并且还允许您在 Markdown 文件中使用 HTML,这就是我能够使用 javascript 并链接到我的 css 文件的方式。

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)
    })
})

或者您可以使用快速静态中间件来完成。您只需将 javascript 文件放入名为 public 的文件夹中即可。

TOC.md

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

server.js

app.use('/static', express.static('public'))

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'))
滥情稳全场 2024-09-06 17:02:05

您可以使用 pandoc,它可以很好地处理此输入(通常是 javascript)。

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

不再让梦枯萎 2024-09-06 17:02:05

根据我的经验,只要删除可能混淆 Markdown 的代码格式,Markdown 就会将 JavaScript 文本输出为纯文本。

  1. 从 javascript 中删除注释,因为 /* ... */ 被翻译为 < em>
  2. 删除每行前面的空格缩进。 < p>可以根据您的缩进插入。

基本上我所做的就是检查生成的 html 并找出通过 markdown 在我的 javascript 代码之间插入了哪些额外标签。并删除生成额外标签的格式。

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.

烟雨凡馨 2024-09-06 17:02:05

一个好主意是将本地和云 js 源分开:

在帖子文件中:

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  

在页脚包含后的默认文件中:

   {% 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 %}

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 %}
甜宝宝 2024-09-06 17:02:05

只需缩进第一行包含 <脚本>标签。

Just indent the first line contains < script > tag.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文