require "kemal" require "json" require "markd" # or any markdown parser for crystal # Structure to hold recipe metadata struct Recipe property title : String property tags : Array(String) property file_path : String # explicit initializer to ensure all properties are set def initialize(title : String, tags : Array(String), file_path : String) @title = title @tags = tags @file_path = file_path end def self.load_from_folder(folder_path : String) recipes = [] of Recipe Dir.glob("#{folder_path}/*/*.md") do |file_path| content = File.read(file_path) title = content.lines[0].chomp.sub(/^# /, "") tags = [] of String # add tag parsing logic if needed recipes << Recipe.new(title, tags, file_path) end recipes end end # Load recipes from the patterns directory recipes = Recipe.load_from_folder("patterns") # Route to display all recipes by category get "/" do # group recipes by category for rendering categorized_recipes = recipes.group_by { |r| File.basename(File.dirname(r.file_path)).to_s } render "src/views/index.ecr", "src/views/layout.ecr" end # Route to display a specific recipe get "/patterns/:category/:recipe" do |env| category = env.params.url["category"] recipe = env.params.url["recipe"] recipe_path = "patterns/#{category}/#{recipe}.md" if File.exists?(recipe_path) markdown_content = File.read(recipe_path) html_content = Markd.to_html(markdown_content) render "src/views/recipe.ecr", "src/views/layout.ecr" else env.response.status_code = 404 "Recipe not found" end end