Skip to content

Commit

Permalink
fix: Fixed to be empty string instead of null when only key is specif…
Browse files Browse the repository at this point in the history
…ied in Frontmatter YAML
  • Loading branch information
akabekobeko committed Jul 1, 2021
1 parent d54a48d commit 1a64e33
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 9 deletions.
2 changes: 2 additions & 0 deletions docs/vfm.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,8 @@ Frontmatter is a way of defining metadata in Markdown (file) units. Write YAML a

I'm using [js-yaml](https://www.npmjs.com/package/js-yaml) for parse in YAML. Schema is [JSON_SCHEMA](https://yaml.org/spec/1.2/spec.html#id2803231).

The js-yaml parse makes `key:` to `key: null`. However, VFM treats this as an empty string. If `key:` or `key:""` is specified as the property of the attribute value, it is output as `key=""`.

**VFM**

```yaml
Expand Down
12 changes: 6 additions & 6 deletions src/plugins/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ const createHead = (data: Metadata, vfile: VFile) => {

// <title>...</title>
{
const title = data.title || vfile.stem;
if (title) {
const title = typeof data.title === 'string' ? data.title : vfile.stem;
if (typeof title === 'string') {
head.push({ type: 'text', value: '\n' }, h('title', [title]));
}
}
Expand Down Expand Up @@ -122,19 +122,19 @@ const createBody = (data: Metadata, tree: Node) => {
const createHTML = (data: Metadata, tree: Node, vfile: VFile) => {
const props = createProperties(data.html);

if (data.id) {
if (typeof data.id === 'string') {
props.id = data.id;
}

if (data.lang) {
if (typeof data.lang === 'string') {
props.lang = data.lang;
}

if (data.dir) {
if (typeof data.dir === 'string') {
props.dir = data.dir;
}

if (data.class) {
if (typeof data.class === 'string') {
props.class = data.class;
}

Expand Down
16 changes: 13 additions & 3 deletions src/plugins/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ const parseMarkdown = (md: string): KeyValue => {
return processor.processSync(md).data as 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.
*/
const readStringOrNullValue = (value: string | null) => {
return value === null ? '' : `${value}`;
};

/**
* Read an attributes from data object.
* @param data Data object.
Expand All @@ -168,7 +178,7 @@ const readAttributes = (data: any): Array<Attribute> | undefined => {

const result: Array<Attribute> = [];
for (const key of Object.keys(data)) {
result.push({ name: key, value: `${data[key]}` });
result.push({ name: key, value: readStringOrNullValue(data[key]) });
}

return result;
Expand Down Expand Up @@ -231,7 +241,7 @@ export const readMetadata = (md: string): Metadata => {
case 'dir':
case 'class':
case 'title':
metadata[key] = `${data[key]}`;
metadata[key] = readStringOrNullValue(data[key]);
break;

case 'html':
Expand All @@ -258,7 +268,7 @@ export const readMetadata = (md: string): Metadata => {
default:
others.push([
{ name: 'name', value: key },
{ name: 'content', value: `${data[key]}` },
{ name: 'content', value: readStringOrNullValue(data[key]) },
]);
break;
}
Expand Down
64 changes: 64 additions & 0 deletions tests/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,70 @@ Text
expect(received).toBe(expected);
});

it('If the value is null or empty string, it will be an empty string', () => {
const md = `---
id:
lang:
dir: ""
class:
title: ""
html:
data-color-mode:
data-light-theme:
data-dark-theme:
body:
id:
class:
base:
target:
href:
meta:
- name:
media:
content:
- name:
media:
content:
link:
- rel:
href:
- rel:
href:
script:
- type:
src:
- type:
src:
author:
---
Text
`;

const received = stringify(md);
const expected = `<!doctype html>
<html data-color-mode="" data-light-theme="" data-dark-theme="" id="" lang="" dir="" class="">
<head>
<meta charset="utf-8">
<title></title>
<base target="" href="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="" media="" content="">
<meta name="" media="" content="">
<meta name="author" content="">
<link rel="" href="">
<link rel="" href="">
<script type="" src=""></script>
<script type="" src=""></script>
</head>
<body id="" class="">
<p>Text</p>
</body>
</html>
`;
expect(received).toBe(expected);
});

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

0 comments on commit 1a64e33

Please sign in to comment.