Forked from YujiSoftware/remove_frontmatter_keys.rb
Created
November 23, 2022 19:32
-
-
Save Graywolf9/ae4b8e9b15ecaf168db1ac3801e237b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env ruby | |
require 'yaml' | |
require 'json' | |
removed = Hash.new { |hash, key| hash[key] = 0 } | |
ARGV.each do |base| | |
Dir.glob(["**/*.html", "**/*.md"], base: base).each do |path| | |
file = File.join(base, path) | |
next if not FileTest.file?(file) | |
content = File.read(file) | |
yaml = YAML.load(content) | |
yaml.reject! do |key, value| | |
if (key != "title" && key != "slug" && key != "original_slug" && !key.start_with?("l10n") && !key.start_with?("i10n")) | |
removed[key] += 1 | |
true | |
else | |
false | |
end | |
end | |
new_content = YAML.dump(yaml) | |
new_content += "---\n" | |
if content.include?("title: '") | |
new_content.gsub!(/title: "([^"]+)"/, "title: '\\1'") | |
elsif not content.include?('title: "') | |
new_content.gsub!(/title: "([^"]+)"/, "title: \\1") | |
end | |
in_meta = true | |
content.each_line.with_index do |line, index| | |
new_content += line if not in_meta | |
if in_meta && line.start_with?("---") && index > 0 | |
in_meta = false | |
end | |
end | |
File.write(file, new_content) | |
end | |
end | |
puts JSON.pretty_generate(removed) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment