From dbc368fc3c825b2c23cc638f66f18b68f061c490 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 5 Feb 2023 16:19:07 +0000 Subject: [PATCH] BlogEngine: Update the BlogIndex page to use the new BlogController. Remove reliance on the old Pythonlist --- package.json | 2 + src/content/blog/2021-01-27-albums-2020.md | 0 src/lib/blog/BlogPost.test.ts | 81 ++++++++++++++++--- src/lib/blog/BlogPost.ts | 66 ++++++++++++++- src/lib/blog/BlogPostSet.test.ts | 13 +++ src/lib/blog/BlogPostSet.ts | 16 +++- src/lib/blog/markdown-repository.test.ts | 3 + src/lib/blog/markdown-repository.ts | 35 +++++--- .../blog/test-builders/blog-post-builder.ts | 33 +++++++- src/routes/api/blog.json/+server.ts | 25 ++---- .../api/blog.json/BlogController.test.ts | 7 +- src/routes/api/blog.json/BlogController.ts | 32 +++++++- src/routes/blog/+page.svelte | 2 + yarn.lock | 25 +++++- 14 files changed, 289 insertions(+), 51 deletions(-) delete mode 100644 src/content/blog/2021-01-27-albums-2020.md diff --git a/package.json b/package.json index a9210bb..be65d35 100644 --- a/package.json +++ b/package.json @@ -46,11 +46,13 @@ "nanoid": "3.3.4", "node-fetch": "^3.2.10", "rehype-stringify": "^9.0.3", + "remark": "^14.0.2", "remark-frontmatter": "^4.0.1", "remark-parse": "^10.0.1", "remark-rehype": "^10.1.0", "remark-stringify": "^10.0.2", "sanitize-html": "^2.7.0", + "strip-markdown": "^5.0.0", "to-vfile": "^7.2.3", "unified": "^10.1.2", "zod": "^3.18.0" diff --git a/src/content/blog/2021-01-27-albums-2020.md b/src/content/blog/2021-01-27-albums-2020.md deleted file mode 100644 index e69de29..0000000 diff --git a/src/lib/blog/BlogPost.test.ts b/src/lib/blog/BlogPost.test.ts index cde83f2..a663bf2 100644 --- a/src/lib/blog/BlogPost.test.ts +++ b/src/lib/blog/BlogPost.test.ts @@ -1,42 +1,92 @@ import { describe, it, expect } from 'vitest'; import { BlogPost } from './BlogPost.js'; +import { aBlogPost } from './test-builders/blog-post-builder.js'; const exampleMarkdownWithFrontMatter = `--- title: "Test Blog Post" date: 2023-02-01T08:00:00Z slug: "2023-02-01-test" +author: Thomas Wilson --- This is the content of the blog post. -This is a [link](http://www.bbc.co.uk) +This is a [link](http://www.bbc.co.uk). - This is a list item - This is another list item `; describe('BlogPost', () => { - it(`should construct`, () => { - // GIVEN - const blogPost = new BlogPost({ title: 'Test Title', markdownContent: 'Test Content' }); + describe(`Constructing`, () => { + it(`should construct`, async () => { + // GIVEN + const blogPost = new BlogPost({ + title: 'Test Title', + author: 'Test Author', + date: new Date('2022-01-01T00:00Z'), + slug: 'test-slug', + markdownContent: 'Test Content', + }); - // THEN - expect(blogPost.title).toBe('Test Title'); - expect(blogPost.markdownContent).toBe('Test Content'); + // WHEN + await blogPost.build(); + + // THEN + const expectedBlogPost = await aBlogPost() + .withTitle('Test Title') + .withAuthor('Test Author') + .withDate(new Date('2022-01-01T00:00Z')) + .withSlug('test-slug') + .withMarkdownContent('Test Content') + .constructAndThenBuild(); + + expect(blogPost).toStrictEqual(expectedBlogPost); + expect(blogPost.html).toBeDefined(); + expect(blogPost.excerpt).toBeDefined(); + }); + }); + + describe(`Building the blog post`, () => { + it(`should know if a blog post has been built`, () => { + // GIVEN + const blogPost = aBlogPost().build(); + + // WHEN + const hasBeenBuilt = blogPost.hasBeenBuilt; + + // THEN + expect(hasBeenBuilt).toBe(false); + expect(blogPost.html).toBeNull(); + expect(blogPost.excerpt).toBeNull(); + }); + + it(`should know if a blog post has been built`, async () => { + // GIVEN + const blogPost = aBlogPost().build(); + + // WHEN + await blogPost.build(); + + // THEN + expect(blogPost.hasBeenBuilt).toBe(true); + expect(blogPost.html).toBeDefined(); + expect(blogPost.excerpt).toBeDefined(); + }); }); it(`Should parse markdown to HTML`, async () => { // GIVEN - const blogPost = new BlogPost({ title: 'Test Title', markdownContent: exampleMarkdownWithFrontMatter }); + const blogPost = await aBlogPost().withMarkdownContent(exampleMarkdownWithFrontMatter).constructAndThenBuild(); // WHEN - const html = await blogPost.getHtml(); + const html = blogPost.html; // THEN expect(html).toStrictEqual( [ `

This is the content of the blog post.

`, - `

This is a link

`, + `

This is a link.

`, `