diff --git a/README.md b/README.md index 7a77cad..c1e9873 100644 --- a/README.md +++ b/README.md @@ -89,3 +89,7 @@ The following websites use `spoonful`: 1. [mine](https://henry.is-a.dev) If you would like your website added here then please open an issue or PR. + +# Credits +Many thanks to [Llakala](https://github.com/llakala) for her work on the +recursive import functions and the toKebabCase. diff --git a/lib/importPosts.nix b/lib/importPosts.nix index 397e0d1..4d3fb1d 100644 --- a/lib/importPosts.nix +++ b/lib/importPosts.nix @@ -1,4 +1,4 @@ -# Written by llakala (https://github.com/llakala) and given to me with +# Written by Llakala (https://github.com/llakala) and given to me with # permission via Discord. args: let inherit diff --git a/lib/importTemplates.nix b/lib/importTemplates.nix index 5a88c00..d2d875d 100644 --- a/lib/importTemplates.nix +++ b/lib/importTemplates.nix @@ -1,4 +1,4 @@ -# Written by llakala (https://github.com/llakala) and given to me with +# Written by Llakala (https://github.com/llakala) and given to me with # permission via Discord. let inherit diff --git a/lib/toKebabCase.nix b/lib/toKebabCase.nix index 20a6951..6e2a4ec 100644 --- a/lib/toKebabCase.nix +++ b/lib/toKebabCase.nix @@ -1,13 +1,30 @@ -{lib}: let - inherit (lib) splitStringBy toLower toUpper trim; - inherit (builtins) concatStringsSep filter; - isLower = c: c == toLower c; - isUpper = c: c == toUpper c; - splitOnSpacesAndUppers = elem: - splitStringBy (prev: curr: curr == " " || (isLower prev && isUpper curr)) - true - elem; - filterSpaces = filter (x: x != "" && x != " "); +# Written by Llakala (https://github.com/llakala) and given to me with +# permission via Discord. +let + inherit + (builtins) + isList + split + foldl' + elemAt + replaceStrings + ; + toLower = replaceStrings ["A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"] ["a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"]; in - elem: - toLower (concatStringsSep "-" (map trim (filterSpaces (splitOnSpacesAndUppers elem)))) + str: + toLower ( + foldl' ( + acc: group: + # if current elem was a capturing group for a space or [lower][upper] + if isList group + then + # if we're just splitting on a space, ignore it + if elemAt group 0 == null + then acc + "_" + # If we're splitting on a capital letter, put an underscore in the middle + else acc + (elemAt group 0) + "_" + (elemAt group 1) + else + # Just a normal lowercase character + acc + group + ) "" (split " |([a-z])([A-Z])" str) + )