如何在Jekyll标签插件中获取Markdown处理的内容

rha*_*ter 6 liquid jekyll jekyll-extensions octopress

我正在为我的Octopress网站制作一个Jekyll标签插件,以帮助我创建一个"注释"元素.我只是希望能够在我的博客上突出显示一条信息作为旁注,就像这样.

在此输入图像描述

问题是,我无法弄清楚如何处理这个标签的内容(即Markdown或Textile).上面的图片只是实现了我实际上用html代码制作我的链接.以下是我在内容中使用markdown时最终结果的结果.

在此输入图像描述

在我的帖子中,我正在写这样的内容.

{% note %}
This is the third post in my Start to Finish series.  Last time I talked about [Git](/blog/2013/09/25/getting-started-with-git/).
{% endnote %}
Run Code Online (Sandbox Code Playgroud)

这是我的插件代码.它基于图像标记代码,并且它真的不是很多.

module Jekyll
  class NoteTag < Liquid::Block
    @title = nil

    def initialize(tag_name, markup, tokens)
      @title = markup
      super
    end

    def render(context)
      output = super(context)
      title = "Note"
      if !@title.empty?
        title += ": #{@title}"
      end
      "</section>\n<div class=\"note\"><span class=\"title\">#{title}</span>#{output}</div>\n<section>"
    end
  end
end

Liquid::Template.register_tag('note', Jekyll::NoteTag)
Run Code Online (Sandbox Code Playgroud)

你知道如何在这个标签的内容上使用转换器吗?我通常使用Markdown作为我的帖子,但我想为其他人发布这个插件,所以我希望它像Jekyll的其余部分一样充满活力.

sho*_*ger 12

我发现了这个" Markdown块标记 "的实现,这似乎是一个相对简单的实现.注意site.getConverterImpl()render方法中的使用.

我用这个例子来制作这个数字标签(基于现有技术我在SO上找到的另一个问题,但不能再找到了)

module Jekyll
  module Tags
    class FigureTag < Liquid::Block

      CaptionUrl = /(\S[\S\s]*)\s+(https?:\/\/\S+)\s+(.+)/i
      Caption = /(\S[\S\s]*)/

      def initialize(tag_name, markup, tokens)
        super
        @caption = nil
        if markup =~ CaptionUrl
          @caption = "\n\t\t<figcaption>#{$1}<a href='#{$2}'>#{$3}</a></figcaption>\n\t"
        elsif markup =~ Caption
          @caption = "\n\t\t<figcaption>#{$1}</figcaption>\n\t"
        end
        @markup = markup
      end

      def render(context)
        site = context.registers[:site]
        converter = site.getConverterImpl(::Jekyll::Converters::Markdown)
        output = converter.convert(super(context))
        "<figure class=\"center\">#{output}#{@caption}</figure>"
      end
    end
  end
end

Liquid::Template.register_tag('fig', Jekyll::Tags::FigureTag)
Run Code Online (Sandbox Code Playgroud)

上面的插件设法在块的内容中解析markdown.因此,给出以下块标记用法:

{% fig Test fig %}
This should be parsed as *markdown* [man](http://example.com/).
{% endfig %}
Run Code Online (Sandbox Code Playgroud)

你会得到以下html:

<figure class="center"><p>This should be parsed as <em>markdown</em> <a href="http://example.com/">man</a>.</p>
    <figcaption>Test fig </figcaption>
</figure>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!我昨晚花了几个小时无处可去,但今天早上我找到了这个例子,它在20分钟内点击了.


Daf*_*oly 7

Jekyll 3.x:getConverterImpl现已弃用

使用find_converter_instance获取转换器:

def render(context)
  text = super
  site = context.registers[:site]
  converter = site.find_converter_instance(::Jekyll::Converters::Markdown)
 _output += "<figcaption>#{converter.convert(_caption)}</figcaption>"
Run Code Online (Sandbox Code Playgroud)