Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
songkg7 committed Jun 28, 2024
1 parent 6bf00a8 commit f542c8c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/jekyll/FrontMatterConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => `---
Expand Down
24 changes: 24 additions & 0 deletions src/tests/FrontMatterConverter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
});

0 comments on commit f542c8c

Please sign in to comment.