diff --git a/src/jekyll/FrontMatterConverter.ts b/src/jekyll/FrontMatterConverter.ts index 49b06908..cc048f1a 100644 --- a/src/jekyll/FrontMatterConverter.ts +++ b/src/jekyll/FrontMatterConverter.ts @@ -20,8 +20,13 @@ const parseFrontMatter = (content: string): [FrontMatter, string] => { const frontMatterLines = content.substring(3, endOfFrontMatter); const body = content.substring(endOfFrontMatter + 3).trimStart(); - const frontMatter = yaml.load(frontMatterLines) as FrontMatter; - return [frontMatter, body]; + try { + const frontMatter = yaml.load(frontMatterLines) as FrontMatter; + return [frontMatter, body]; + } catch (e) { + console.error(e); + return [{}, content]; + } }; const join = (result: FrontMatter, body: string) => `--- diff --git a/src/tests/FrontMatterConverter.test.ts b/src/tests/FrontMatterConverter.test.ts index 3a8cd1ca..fdf4dfb6 100644 --- a/src/tests/FrontMatterConverter.test.ts +++ b/src/tests/FrontMatterConverter.test.ts @@ -374,3 +374,27 @@ date: 2021-01-01 12:00:00 +0900 }); }); + +describe('FrontMatterConverter Edge Case Tests', () => { + const malformedFrontMatterContents = `--- +title "test" // Missing colon +date: 2021-01-01 12:00:00 +0900 +categories: [test] +--- +# test +`; + it('should handle malformed front matter', () => { + const result = convertFrontMatter(malformedFrontMatterContents); + expect(result).toEqual(malformedFrontMatterContents); // Assuming the function passes through malformed front matter as is + }); + + const incompleteFrontMatterContents = `--- +title: "test" +date: 2021-01-01 12:00:00 +0900 +# test +`; + it('should handle interrupted parsing', () => { + const result = convertFrontMatter(incompleteFrontMatterContents); + expect(result).toEqual(incompleteFrontMatterContents); // Assuming the function passes through incomplete front matter as is + }); +});