How to get Markdown processed content in Jekyll tag plugin

后端 未结 2 1608
情书的邮戳
情书的邮戳 2020-12-30 10:48

I\'m working on a Jekyll tag plugin for my Octopress site to help me make a \'note\' element. I just want to be able to highlight a piece of information on my blog as a sid

相关标签:
2条回答
  • 2020-12-30 10:54

    I found this implementation of a 'Markdown block tag' which seems to be a relatively straightforward implementation. Note the use of site.getConverterImpl() in the render method.

    I used that example to produce this figure tag (based on prior art to another question I found on SO but can no longer locate)

    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)
    

    The above plugin manages to parse markdown within the contents of the block. So given the following block tag usage:

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

    You'll get the following 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>
    

    Hope this helps! I spent a couple hours last night and got nowhere, but this morning I found this example and it clicked within 20 minutes.

    0 讨论(0)
  • 2020-12-30 11:02

    Jekyll 3.x : getConverterImpl is now deprecated

    Use find_converter_instance to get the converter :

    def render(context)
      text = super
      site = context.registers[:site]
      converter = site.find_converter_instance(::Jekyll::Converters::Markdown)
     _output += "<figcaption>#{converter.convert(_caption)}</figcaption>"
    
    0 讨论(0)
提交回复
热议问题