在Jekyll HTML中嵌入Markdown [英] Embedding Markdown in Jekyll HTML

查看:122
本文介绍了在Jekyll HTML中嵌入Markdown的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在使用Jekyll时在HTML文件中嵌入降价。有没有办法实现类似以下内容?

 #index.html 

---
layout:default
---


< p> [Stack Overflow](http://www.stackoverflow.com)< / p>

注意:我知道我可以这样做。

 #index.html 

---
布局:默认
---


< p>< a href =http://www.stackoverflow.com>堆栈溢出< / a>< / p>


解决方案

以下是如何使用< a href =http://jekyllrb.com/docs/plugins/ =noreferrer> Jekyll插件:

 模块Jekyll 
class MarkdownBlock< Liquid :: Block
def initialize(tag_name,text,tokens)
super
end
requirekramdown
def render(context)
content =超级
#{Kramdown :: Document.new(content).to_html}
结束
结束
结束
Liquid :: Template.register_tag('markdown' ,Jekyll :: MarkdownBlock)

(要将此片段安装为插件,请将其放入 *。rb 文件在 _plugins 目录的源站点根目录下)



<然后,像这样使用它:

  {%markdown%} 
[Stack Overflow](http:/ / pre
$ b <%>

编辑:请参阅@ Cristian的答案以获得更好的解决方案!如果您使用的是Kramdown(这可能是您使用Jekyll的情况),您可以使用它的功能在 div 的内部以 markdown =1属性。


I'm trying to nest markdown in an HTML file while using Jekyll. Is there a way to achieve something like the following?

# index.html

---
layout: default
---


<p>[Stack Overflow](http://www.stackoverflow.com)</p>

Note: I'm aware that I could do this instead.

# index.html

---
layout: default
---


<p><a href="http://www.stackoverflow.com">Stack Overflow</a></p>

解决方案

Here's how you can define a markdown block with a Jekyll plugin:

module Jekyll
  class MarkdownBlock < Liquid::Block
    def initialize(tag_name, text, tokens)
      super
    end
    require "kramdown"
    def render(context)
      content = super
      "#{Kramdown::Document.new(content).to_html}"
    end
  end
end
Liquid::Template.register_tag('markdown', Jekyll::MarkdownBlock)

(To install this snippet as a plugin, put it in a *.rb file under _plugins directory of your source site root)

Then, use it like this:

{% markdown %}
[Stack Overflow](http://www.stackoverflow.com)
{% endmarkdown %}

EDIT: See @Cristian's answer for a better solution! If you're using Kramdown (which is likely the case since you are using Jekyll), you can use it's feature to render markdown inside div's with a markdown="1" attribute.

这篇关于在Jekyll HTML中嵌入Markdown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆