Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Type Safety for Parsed Markdown Recipes #25

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/services/markdown/__fixtures__/broken-recipe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
title: Seemingly a correct recipe
ingredients:
- name: Oh
amount: wait
- name: What
amountt: is
imageSlug: /going/on.exe
duration: 420 years
tags: "Foo, bar, baz"
---

Lorem ipsum dolor sit amet, set nunc est potandum.
17 changes: 17 additions & 0 deletions src/services/markdown/__fixtures__/valid-recipe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: Title of the recipe
imageSlug: /images/recipes/valid-recipe.jpg
ingredients:
- name: Bugs
amount: Zero
imageSlug: /images/ingredients/bugs.png
- name: Type safety
amount: 1×
imageSlug: /images/ingredients/type-safety.png
duration: P0Y0M0DT0H5M0S
tags:
- Foo
- Bar
---

Lorem ipsum dolor sit amet, set nunc est potandum.
28 changes: 28 additions & 0 deletions src/services/markdown/parseRecipe.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import fs from "fs";

import { describe, expect, it } from "bun:test";

import { parseRecipe } from ".";

describe("parseRecipe", () => {
it("should return a parsed recipe if the contents are correct", () => {
const validRecipe = fs.readFileSync(
"./src/services/markdown/__fixtures__/valid-recipe.md",
"utf-8"
);

expect(parseRecipe(validRecipe)).toBeDefined();
});

it("should return nothing if the recipe is broken or malformed", () => {
const brokenRecipe = fs.readFileSync(
"./src/services/markdown/__fixtures__/valid-recipe.md",
"utf-8"
);
const huh = parseRecipe(brokenRecipe);

console.log(huh);

expect(huh).toBeUndefined();
});
});
17 changes: 17 additions & 0 deletions src/services/markdown/parseRecipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ import { RecipeMetadata } from "./types";
export const parseRecipe = (fileName: string) => {
const { data: metadata, content } = matter(fileName);

if (
typeof content !== "string" ||
typeof metadata.title !== "string" ||
typeof metadata.imageSlug !== "string" ||
!Array.isArray(metadata.ingredients) ||
metadata.ingredients.some((ingredient) => {
typeof ingredient.name !== "string" ||
typeof ingredient.amount !== "string" ||
typeof ingredient.imageSlug !== "string";
}) ||
typeof metadata.duration !== "string" ||
!Array.isArray(metadata.tags) ||
metadata.tags.some((tag) => typeof tag !== "string")
) {
return;
}

return {
metadata,
content,
Expand Down