Skip to content

Instantly share code, notes, and snippets.

@tristansokol
Created December 14, 2024 03:42
Show Gist options
  • Save tristansokol/1b295d812b1b99a650b3c6d66b65858f to your computer and use it in GitHub Desktop.
Save tristansokol/1b295d812b1b99a650b3c6d66b65858f to your computer and use it in GitHub Desktop.
Mealie backup json to markdown converter
import { readFileSync, writeFileSync } from "fs";
const data = readFileSync('./database.json', 'utf-8')
const json = JSON.parse(data)
let recipes = {}
json.recipes.forEach(recipe => {
recipes[recipe.id] = recipe
})
json.notes.forEach(note => {
recipes[note.recipe_id].notes = note
})
json.recipe_instructions.forEach(instruction => {
if (!recipes[instruction.recipe_id].recipe_instructions)
recipes[instruction.recipe_id].recipe_instructions = []
recipes[instruction.recipe_id].recipe_instructions.push(instruction)
})
json.recipe_timeline_events.forEach(event => {
if (!recipes[event.recipe_id].recipe_timeline_events)
recipes[event.recipe_id].recipe_timeline_events = []
recipes[event.recipe_id].recipe_timeline_events.push(event)
})
json.recipes_ingredients.forEach(ingredient => {
if (!recipes[ingredient.recipe_id].recipes_ingredients)
recipes[ingredient.recipe_id].recipes_ingredients = []
recipes[ingredient.recipe_id].recipes_ingredients.push(ingredient)
})
Object.values(recipes).forEach(recipe => {
let markdown = ""
markdown += `# ${recipe.name}\n`
//prep time and cook time
if (recipe.prep_time) {
markdown += `*Prep Time: ${recipe.prep_time}*\n`
}
if (recipe.cook_time) {
markdown += `*Cook Time: ${recipe.cook_time}*\n`
}
if (recipe.perform_time) {
markdown += `*Active Time: ${recipe.perform_time}*\n`
}
if (recipe.total_time) {
markdown += `*Total Time: ${recipe.total_time}*\n`
}
//source orgurl if available
if (recipe.org_url)
markdown += `*Source: [${recipe.org_url}](${recipe.org_url})*\n`
//created at in readable format
markdown += `*Created At: ${new Date(recipe.created_at).toLocaleDateString()}*\n`
//yeild
markdown += `*Yield: ${recipe.recipe_yield}*\n`
//date dded
markdown += `*Date Added: ${new Date(recipe.date_added).toLocaleDateString()}*\n`
markdown += `## Description\n`
markdown += `${recipe.description}\n`
markdown += `## Ingredients\n`
let Ingredients = recipe.recipes_ingredients.sort((a, b) => a.position - b.position)
Ingredients.forEach(ingredient => {
markdown += `- ${ingredient.note}\n`
})
let instructions = recipe.recipe_instructions.sort((a, b) => a.position - b.position)
markdown += `## Instructions\n`
instructions.forEach(instruction => {
if (instruction.title) {
markdown += `### ${instruction.title}\n`
}
markdown += `${instruction.position}. ${instruction.text}\n`
})
//notes - timeline events
markdown += `## Notes\n`
if (recipe.notes) {
markdown += `${recipe.notes.text}\n`
}
if (recipe.recipe_timeline_events) {
let events = recipe.recipe_timeline_events.sort((a, b) => new Date(a.created_at) - new Date(b.created_at))
events.forEach(event => {
markdown += `${new Date(event.created_at).toLocaleString()}. ${event.message || event.subject}\n`
})
}
writeFileSync(`./recipes/${recipe.name}.md`, markdown)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment