Skip to content

Commit

Permalink
Merge pull request #50 from tmayoff/clear_shopping_list
Browse files Browse the repository at this point in the history
  • Loading branch information
tmayoff authored Mar 3, 2024
2 parents aa0ccba + 88cf1bd commit ebe06a2
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 71 deletions.
120 changes: 57 additions & 63 deletions src/meal_plan/shopping_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,53 @@ import { TFile } from 'obsidian';
import type { Ingredient } from 'parse-ingredient';
import { get } from 'svelte/store';
import type { Context } from '../context';
import { append_markdown_ext } from '../utils/filesystem';
import { get_current_week } from './utils';

export async function clear_checked_ingredients(ctx: Context) {
let file_path = get(ctx.settings).shopping_list_note;
if (!file_path.endsWith('.md')) {
file_path += '.md';
}
const file_path = append_markdown_ext(get(ctx.settings).shopping_list_note);

const file = ctx.app.vault.getAbstractFileByPath(file_path);
if (file instanceof TFile) {
const list_items = ctx.app.metadataCache.getFileCache(file)?.listItems;
if (list_items === undefined) return;

// Get current files content
let content = await ctx.app.vault.read(file);

// Since we're modifying the content but keeping the original content's metadata we need to keep track of
// how much we remove and offset all removals by that amount
let offset = 0;
for (const item of list_items) {
if (item.task !== undefined && item.task !== ' ') {
const pos = item.position;
content = content.substring(0, pos.start.offset - offset) + content.substring(pos.end.offset + 1 - offset);
offset += pos.start.offset + pos.end.offset + 1;
}
}
const file = ctx.app.vault.getFileByPath(file_path);
if (file == null) return;

const list_items = ctx.app.metadataCache.getFileCache(file)?.listItems?.filter((i) => {
return i.task !== undefined && i.task !== ' ';
});
if (list_items === undefined) return;

// Get current files content
let content = await ctx.app.vault.read(file);

// Since we're modifying the content but keeping the original content's metadata we need to keep track of
// how much we remove and offset all removals by that amount
let offset = 0;

// Save the new content
ctx.app.vault.modify(file, content);
for (const item of list_items) {
const pos = item.position;
const start = pos.start.offset - offset;
const length = pos.end.offset - pos.start.offset + 1;

content = content.substring(0, start) + content.substring(start + length);
offset += length;
}

// Save the new content
ctx.app.vault.modify(file, content);
}

export async function generate_shopping_list(ctx: Context) {
let file_path = get(ctx.settings).meal_plan_note;
if (!file_path.endsWith('.md')) {
file_path += '.md';
}
const meal_plan_file_path = append_markdown_ext(get(ctx.settings).meal_plan_note);

let ingredients: Array<Ingredient> = [];
const meal_plan_file = ctx.app.vault.getFileByPath(file_path);
if (meal_plan_file != null) {
ingredients = get_ingredients(ctx, meal_plan_file);
}
const meal_plan_file = ctx.app.vault.getFileByPath(meal_plan_file_path);
if (meal_plan_file == null) return;
const ingredients = get_ingredients(ctx, meal_plan_file);

file_path = get(ctx.settings).shopping_list_note;
if (!file_path.endsWith('.md')) {
file_path += '.md';
}
const shopping_list_file_path = append_markdown_ext(get(ctx.settings).shopping_list_note);

let file = ctx.app.vault.getAbstractFileByPath(file_path);
let file = ctx.app.vault.getFileByPath(shopping_list_file_path);
if (file == null) {
ctx.app.vault.create(file_path, '');
file = ctx.app.vault.getAbstractFileByPath(file_path);
ctx.app.vault.create(shopping_list_file_path, '');
file = ctx.app.vault.getFileByPath(shopping_list_file_path);
}

if (file instanceof TFile) {
Expand Down Expand Up @@ -112,29 +106,29 @@ function get_ingredients(ctx: Context, file: TFile) {
const r = get(ctx.recipes).find((r) => {
return r.path.path === recipeFile.path;
});
if (r !== undefined) {
// Before adding an ingredient check if it's already in the list
// If it is add the quanities together otherwise add it to the list
for (const i of r.ingredients) {
const existing = ingredients.findIndex((existing) => {
return existing.description === i.description && i.unitOfMeasure === existing.unitOfMeasure;
});

if (
ignore_list.find((ignored) => {
return i.description.toLowerCase() === ignored.toLowerCase();
}) != null
) {
continue;
}

if (existing === -1) {
ingredients.push(i);
} else {
let raw = ingredients[existing].quantity ?? 0;
raw += i.quantity ?? 0;
ingredients[existing].quantity = raw;
}
if (r === undefined) continue;

// Before adding an ingredient check if it's already in the list
// If it is add the quanities together otherwise add it to the list
for (const i of r.ingredients) {
const existing = ingredients.findIndex((existing) => {
return existing.description === i.description && i.unitOfMeasure === existing.unitOfMeasure;
});

if (
ignore_list.find((ignored) => {
return i.description.toLowerCase() === ignored.toLowerCase();
}) != null
) {
continue;
}

if (existing === -1) {
ingredients.push(i);
} else {
let raw = ingredients[existing].quantity ?? 0;
raw += i.quantity ?? 0;
ingredients[existing].quantity = raw;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test_vault/Meals/Bacon Pesto Tomato Pizza.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source: ChefsPlate
servings: "2"
meal: Dinner
---
# Ingredients
# Ingredients
- 100g Bacon
- 340g Pizza Dough
- 1 Tomato
Expand Down
2 changes: 0 additions & 2 deletions test_vault/Meals/Creamy Garlic Chicken & Potatoes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ servings: "4"
meal: Dinner
---
# Ingredients
---
- 800g Chicken Breasts
- 800g Yellow Potatoes
- 3 Garlic Cloves, minced
Expand All @@ -19,7 +18,6 @@ meal: Dinner
- 100g cream cheese
- 40g Parmesan cheese, shredded
- 50g Mozzarella, shredded
---

# Directions
0. Before starting
Expand Down
12 changes: 7 additions & 5 deletions test_vault/Shopping List.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
- [ ] 1600 g Chicken Breasts
- [ ] 1600 g Yellow Potatoes
- [ ] 1600 g Chicken Breast
- [ ] 1600 g Yellow Potato
- [ ] 6 Garlic Clove
- [ ] 2 tbsp Onion Powder
- [ ] 4 tbsp Za'atar
- [ ] 4 tbsp Chili Flakes
- [ ] 4 tbsp Chili Flake
- [ ] 4 tbsp Paprika
- [ ] 1 cup Milk
- [ ] 200 g cream cheese
- [ ] 80 g Parmesan cheese, shredded
- [ ] 100 g Mozzarella, shredded
- [ ] 80 g Parmesan cheese
- [ ] 100 g Mozzarella

0 comments on commit ebe06a2

Please sign in to comment.