Skip to content

Commit 217cc86

Browse files
Merge pull request #8 from stackbithq/markdown-writer-fix
Check body type in Markdown writer
2 parents 840dec0 + a91075d commit 217cc86

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

lib/file-writers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ const writeFile = util.promisify(fs.writeFile);
66

77
module.exports.writeFrontmatterMarkdown = (
88
filePath,
9-
{ body = "", frontmatter }
9+
{ body = "", frontmatter = {} }
1010
) => {
1111
const lines = [
1212
"---",
1313
yaml.stringify(frontmatter).trim(),
1414
"---",
15-
body.length > 0 ? body.trim() : "",
15+
body ? body.toString().trim() : "",
1616
""
1717
];
1818
const content = lines.join("\n");

lib/file-writers.test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const util = require("util");
2+
3+
const mockPromisifiedFunction = jest.fn();
4+
5+
util.promisify = jest.fn(() => mockPromisifiedFunction);
6+
7+
jest.mock("fs");
8+
9+
const fileWriters = require("./file-writers");
10+
11+
afterEach(() => {
12+
jest.clearAllMocks();
13+
});
14+
15+
describe("`writeFrontmatterMarkdown()`", () => {
16+
test("writes Markdown files with frontmatter", () => {
17+
const mockContent = {
18+
body: "This is the body",
19+
frontmatter: {
20+
name: "John Doe"
21+
}
22+
};
23+
const mockPath = "/Users/johndoe/file.md";
24+
25+
fileWriters.writeFrontmatterMarkdown(mockPath, mockContent);
26+
27+
expect(mockPromisifiedFunction).toHaveBeenCalledTimes(1);
28+
29+
const [filePath, content] = mockPromisifiedFunction.mock.calls[0];
30+
31+
expect(filePath).toBe(mockPath);
32+
expect(content).toBe(
33+
`---\nname: ${mockContent.frontmatter.name}\n---\n${mockContent.body}\n`
34+
);
35+
});
36+
37+
test("converts body to string", () => {
38+
const mockContent = {
39+
body: [1, 2, 3],
40+
frontmatter: {
41+
name: "John Doe"
42+
}
43+
};
44+
const mockPath = "/Users/johndoe/file.md";
45+
46+
fileWriters.writeFrontmatterMarkdown(mockPath, mockContent);
47+
48+
expect(mockPromisifiedFunction).toHaveBeenCalledTimes(1);
49+
50+
const [filePath, content] = mockPromisifiedFunction.mock.calls[0];
51+
52+
expect(filePath).toBe(mockPath);
53+
expect(content).toBe("---\nname: John Doe\n---\n1,2,3\n");
54+
});
55+
});

0 commit comments

Comments
 (0)