How make custom Liquid tag append to page.tags

I have a custom Liquid tag that (in addition to customizing the look) I want to add to the array of page.tags. The first couple of lines of the render function (shown below) is supposed to do that. It works when I do a fresh build of the site. When I do an incremental build, it does not.

It seems I’ve created a hack that doesn’t quite work correctly. Please help me know what I’m missing.

module Jekyll
  class Term < Liquid::Tag
    def initialize(tag_name, input, tokens)
      super
      vars = input.split(" ")
      @acronym = vars[0]
      @category = vars[1]
    end

    def render(context)
      tags = context.environments.first['page']['tags']
      tags.append(@acronym) unless tags.include?(@acronym)

      begin
        definition = context.registers[:site].data['terminology']["#{@category}"]["#{@acronym}"]['definition']

        if definition.nil?
          "<a style=\"color:red\" href=\"/terminology/#{@category}\##{@acronym}\" title=\"???\" class=\"term\">#{@acronym}</a>"
        else
          "<a href=\"/terminology/#{@category}\##{@acronym}\" title=\"#{definition}\" class=\"term\">#{@acronym}</a>"
        end
      rescue NoMethodError
        "<span style=\"color:red\">#{@category}/#{@acronym}</span>"
      end
    end
  end
end

Liquid::Template.register_tag('term', Jekyll::Term)

Here’s a shorter version focusing on the important code:

module Jekyll
class Term < Liquid::Tag
def initialize(tag_name, input, tokens)
super
vars = input.split(" ")
@acronym = vars[0]
@category = vars[1]
end

def render(context)
  tags = context.environments.first['page']['tags']
  tags.append(@acronym) unless tags.include?(@acronym)

  definition = context.registers[:site].data['terminology']["#{@category}"]["#{@acronym}"]['definition']

  result = if definition.nil?
             "<a style=\"color:red\" href=\"/terminology/#{@category}\##{@acronym}\" title=\"???\" class=\"term\">#{@acronym}</a>"
           else
             "<a href=\"/terminology/#{@category}\##{@acronym}\" title=\"#{definition}\" class=\"term\">#{@acronym}</a>"
           end

  result rescue "<span style=\"color:red\">#{@category}/#{@acronym}</span>"
end

end
end

Liquid::Template.register_tag(‘term’, Jekyll::Term)
This version retains the important parts of the code, including the initialize method, the render method with appending to tags, and the Liquid::Template.register_tag statement to register the custom tag.

If by “incremental build” you mean using the --Incremental flag as in jekyll serve --incremental you should be aware that this feature is still experimental and does not work well.

By just running jekyll serve, jekyll should be able to pick up any modification you made to your website and rebuild accordingly.

I had forgotten that incremental was still experimental. Thanks for the reminder.

This is an easy mistake to make since this is so poorly documented and jekyll serve suggests in its output that you use this flag