Skip to content

Commit

Permalink
fix: Fixed an issue that caused an error if YAML object was null
Browse files Browse the repository at this point in the history
  • Loading branch information
akabekobeko committed Jul 2, 2021
1 parent 1a64e33 commit 43233a3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/plugins/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ export type Metadata = {
head?: string;
};

/** Key/Value pair. */
/**
* Key/Value pair.
* Definition to enable subscript access of `Object`.
*/
type KeyValue = { [key: string]: any };

/**
Expand All @@ -81,7 +84,7 @@ interface MetadataVFile extends VFile {
* @param tree Tree of Markdown AST.
* @returns Title text or `undefined`.
*/
const readTitleFromHeading = (tree: Node) => {
const readTitleFromHeading = (tree: Node): string | undefined => {
const heading = select('heading', tree) as Element | undefined;
if (!heading) {
return;
Expand Down Expand Up @@ -160,9 +163,9 @@ const parseMarkdown = (md: string): KeyValue => {
* Read the string or null value in the YAML parse result.
* If the value is null, it will be an empty string
* @param value Value of YAML parse result.
* @returns String.
* @returns Text.
*/
const readStringOrNullValue = (value: string | null) => {
const readStringOrNullValue = (value: string | null): string => {
return value === null ? '' : `${value}`;
};

Expand All @@ -172,7 +175,7 @@ const readStringOrNullValue = (value: string | null) => {
* @returns Attributes of HTML tag.
*/
const readAttributes = (data: any): Array<Attribute> | undefined => {
if (typeof data !== 'object') {
if (data === null || typeof data !== 'object') {
return;
}

Expand Down
21 changes: 21 additions & 0 deletions tests/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,27 @@ Text
expect(received).toBe(expected);
});

it('Specify null or empty string for Object', () => {
const md = `---
html:
meta:
-
vfm: ''
---
`;
const received = stringify(md);
const expected = `<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body></body>
</html>
`;
expect(received).toBe(expected);
});

it('Style from options', () => {
const md = `---
link:
Expand Down

0 comments on commit 43233a3

Please sign in to comment.