How to fold / wrap the content after number of characters

Today I went back to this problem and found a way simpler solution: a plugin. That way we can use Ruby functions, which makes the code way shorter and enables us to use that function on multiple lines, without repeating the same liquid code over and over.

Here is the plugin code:

module Jekyll
  module WrapLinesFilter
    def wrap_lines(input_string)
      max_length = 72
      wrapped_string = ""

      while input_string.length > max_length
        line = input_string.slice!(0, max_length)
        line += "\r\n "
        wrapped_string += line
      end

      wrapped_string += input_string

      wrapped_string

    end
  end
end

Liquid::Template.register_filter(Jekyll::WrapLinesFilter)

I put that into _plugins/wrap_lines_filter.rb and then added this to the ics template file snippet:

{{ event.content | strip_html | prepend: "DESCRIPTION:" | normalize_whitespace | wrap_lines }}
{{ event.title | escape | prepend: "SUMMARY:" | wrap_lines }}
{{ event.url | absolute_url | prepend: "URL:" | wrap_lines }}

This creates an ICS file that validates with the icalendar.org validator :tada:

1 Like