代码之家  ›  专栏  ›  技术社区  ›  rharter

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

  •  10
  • rharter  · 技术社区  · 11 年前

    我正在为我的Octopress网站开发一个Jekyll标签插件,帮助我制作一个“note”元素。我只想能够在我的博客上突出显示一条信息作为旁注,就像这样。

    enter image description here

    问题是,我不知道如何处理这个标签的内容(即Markdown或Textile)。上面的图片只是我实际使用html代码制作链接。以下是当我在内容中使用markdown时的结果。

    enter image description here

    在我的帖子中,我是这样写的。

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

    这是我的插件代码。它基于图像标签代码,实际上并没有太多内容。

    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)
    

    你知道我如何对这个标签的内容使用转换器吗?我通常在帖子中使用Markdown,但我想为其他人发布这个插件,所以我希望它像Jekyll的其他插件一样是动态的。

    2 回复  |  直到 4 年前
        1
  •  12
  •   DaftWooly    9 年前

    杰基尔3.x: 获取转换器Impl 现在已弃用

    使用 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>"
    
        2
  •  12
  •   sholsinger    11 年前

    我发现了一个‘ Markdown block tag 这似乎是一个相对简单的实现。注意使用 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)
    

    上面的插件能够解析块内容中的markdown。因此,给定以下块标记用法:

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

    您将获得以下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>
    

    希望这能有所帮助!我昨晚花了几个小时,但一无所获,但今天早上我发现了这个例子,它在20分钟内就成功了。