Patching YAML number parsing for Jekyll

Last modified: March 13th, 2023

Working with a specific static site generator?
Customize CloudCannon's documentation to suit your SSG.

Great! We'll show you documentation relevant to .
You can change this any time using the dropdown in the navigation bar.

This is a patch for SafeYAML (Jekyll YAML parser) to fix a parsing error.

Updating a text input in CloudCannon to 1,234 saves the value unquoted, allowed by the YAML specification. Jekyll then parses this as a number which can case unexpected results. This patch removes the comma when testing for a number.

_plugins/patch.rb
copied
# This is a patch for SafeYAML to fix a parsing error.
# It ensures the following is parsed as a string rather than a number:
#
# a_string: 1,234
#
# Rendering this with {{ page.a_string }}
# Unpatched is: 1234
# Patched is: 1,234

module SafeYAML
  class Transform
    class ToInteger
      MATCHERS = Deep.freeze([
        /\A[-+]?(0|([1-9][0-9_]*))\Z/, # decimal
        /\A0[0-7]+\Z/,                  # octal
        /\A0x[0-9a-f]+\Z/i,             # hexadecimal
        /\A0b[01_]+\Z/                  # binary
      ])

      def transform?(value)
        MATCHERS.each_with_index do |matcher, idx|
          return true, Integer(value) if matcher.match(value)
        end
        try_edge_cases?(value)
      end
    end
  end
end
Open in a new tab