diff --git a/src/lib/blog/markdown/markdown-builder.test.ts b/src/lib/blog/markdown/markdown-builder.test.ts index 2a1bd09..aec69a8 100644 --- a/src/lib/blog/markdown/markdown-builder.test.ts +++ b/src/lib/blog/markdown/markdown-builder.test.ts @@ -1,28 +1,77 @@ import { describe, it, expect } from 'vitest'; import { MarkdownBuilder } from './markdown-builder.js'; -const exampleMarkdown = [ - `---`, - `title: "This is a title"`, - `---`, - `This is a title. This is a body.`, - `This is an incredibly long new set`, - `of words to read. I hope you`, - `enjoy reading them. I hope you`, - `enjoy reading them. I hope you`, -].join('\n'); +const markdownBuilder = (tags: string[] | undefined = undefined): string => { + let exampleMarkdown = [`---`, `title: "This is a title"`, `date: 2025-03-13T18:44:00Z`, `draft: false`]; + + if (tags !== undefined) { + exampleMarkdown = [...exampleMarkdown, `tags:`, ...tags.map((tag) => ` - ${tag}`)]; + } + + exampleMarkdown = [ + ...exampleMarkdown, + `---`, + `This is a title. This is a body.`, + `This is an incredibly long new set`, + `of words to read. I hope you`, + `enjoy reading them. I hope you`, + `enjoy reading them. I hope you`, + ]; + + return exampleMarkdown.join('\n'); +}; describe(`MarkdownBuilder`, () => { - // const markdownBuilder = new MarkdownBuilder(); + const markdown = markdownBuilder(); it(`should build an excerpt`, async () => { - // GIVEN - const markdown = exampleMarkdown; - // WHEN const excerpt = await MarkdownBuilder.getExcerptFromMarkdown(markdown, 10); // THEN expect(excerpt).toBe(`This is a title. This is a body. This is`); }); + + describe(`getFrontmatter`, () => { + it(`should be able to identify the fields in front-matter`, () => { + // WHEN + const frontmatter = MarkdownBuilder.getFrontmatter<{ title: string; date: Date }>(markdown, 'test.md'); + + // THEN + expect(frontmatter).toStrictEqual({ + title: 'This is a title', + date: new Date(`2025-03-13T18:44:00Z`), + draft: false, + }); + }); + }); + + describe(`getTagsFromMarkdown`, () => { + it(`should build the tags`, () => { + // WHEN + const tags = MarkdownBuilder.getTagsFromMarkdown<{ tags: string[] }>( + markdownBuilder(['one-tag', 'another']), + 'test.md' + ); + + // THEN + expect(tags).toStrictEqual(['one-tag', 'another']); + }); + + it(`should build when the tags are empty`, () => { + // WHEN + const tags = MarkdownBuilder.getTagsFromMarkdown<{ tags: string[] }>(markdownBuilder(), 'test.md'); + + // THEN + expect(tags).toStrictEqual([]); + }); + + it(`should build when there is no 'tags' field`, () => { + // WHEN + const tags = MarkdownBuilder.getTagsFromMarkdown<{ tags: string[] }>(markdownBuilder(), 'test.md'); + + // THEN + expect(tags).toStrictEqual([]); + }); + }); }); diff --git a/src/lib/blog/markdown/markdown-builder.ts b/src/lib/blog/markdown/markdown-builder.ts index 4d96275..1023ec4 100644 --- a/src/lib/blog/markdown/markdown-builder.ts +++ b/src/lib/blog/markdown/markdown-builder.ts @@ -18,7 +18,7 @@ export class MarkdownBuilder { return value.toString(); } - static getFrontmatter>(markdownContent: string, fileName: string): T | null { + static getFrontmatter = {}>(markdownContent: string, fileName: string): T | null { const processor = this.getFrontmatterProcessor(); const parsedMarkdown = processor.parse(markdownContent); @@ -47,6 +47,17 @@ export class MarkdownBuilder { .join(' '); } + static getTagsFromMarkdown(markdownContent: string, theFileName: string): string[] { + const frontMatter = this.getFrontmatter(markdownContent, theFileName); + + if (frontMatter === null) { + return []; + } else if (!frontMatter.tags) { + return []; + } + return frontMatter.tags; + } + private static getFrontmatterProcessor() { return unified() // .use(remarkParse)