在 Markdown 中嵌入 JavaScript
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我遇到了同样的问题,但我设法通过在开始标记后添加换行符来使 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.
在某些情况下可能有效的不同解决方案:(当我尝试嵌入 CodePen 示例时,所选答案对我不起作用)
将其添加到您的默认布局中:
在需要一些 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:
In posts where you need some JavaScript files, you can add them in the YAML front matter like so:
The
async
might not be necessary or wanted but you could probably add that as a parameter incustomjs
. (see YAML front matter for Jekyll and nested lists for details)我使用这段代码在 Markdown 中编写 JavaScript 代码。
I use this code to write JavaScript code in markdown.
Markdown 支持内联 XHTML,但不支持 Javascript。
Markdown supports inline XHTML but not Javascript.
他们在他们的网站上提供的示例显示了一个空的
包含换行符的标签。也许就是这样?
The example they give on their site shows an empty
<script>
tag containing a newline. Maybe that's it?我发现转义结束 '>'开始和结束“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:
It will be rendered like this:
That's just my two cents.
我用一个名为 Showdown 的库构建了一个 Express 服务器,该库将 Markdown 转换为 HTML,并且还允许您在 Markdown 文件中使用 HTML,这就是我能够使用 javascript 并链接到我的 css 文件的方式。
TOC.md
server.js
或者您可以使用快速静态中间件来完成。您只需将 javascript 文件放入名为 public 的文件夹中即可。
TOC.md
server.js
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
server.js
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
server.js
您可以使用 pandoc,它可以很好地处理此输入(通常是 javascript)。
You could use pandoc, which handles this input (and javascript generally) just fine.
根据我的经验,只要删除可能混淆 Markdown 的代码格式,Markdown 就会将 JavaScript 文本输出为纯文本。
基本上我所做的就是检查生成的 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.
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.
一个好主意是将本地和云 js 源分开:
在帖子文件中:
在页脚包含后的默认文件中:
A good idea is to have local and cloud js sources separated:
In the post file:
In the default file after footer inclusion:
只需缩进第一行包含 <脚本>标签。
Just indent the first line contains < script > tag.