From 60affeaf09d7d86fc00c5a2322715b8dfaf688d8 Mon Sep 17 00:00:00 2001 From: akabeko Date: Thu, 20 Jan 2022 23:35:45 +0900 Subject: [PATCH] fix: Rename `readMetadata` argument and `Metadata` property `excludes` to be more understandable --- README.md | 10 ++++++---- src/plugins/metadata.ts | 19 +++++++++++-------- tests/metadata.test.ts | 13 ++++++------- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index e0ae33b..01cf5f4 100644 --- a/README.md +++ b/README.md @@ -385,11 +385,11 @@ console.log(metadata); About `Metadata` details, refer to [VFM](https://vivliostyle.github.io/vfm/#/vfm)'s "Frontmatter" or type information of TypeScript. -**About `excludes`** +**About `customKeys`** Use this if want to add custom metadata with a third party tool. -Keys that are not defined as VFM are treated as `meta`. If you specify a key name in `excludes`, the key and its data type will be preserved and stored in `excludes` instead of `meta`. +Keys that are not defined as VFM are treated as `meta`. If you specify a key name in `customKeys`, the key and its data type will be preserved and stored in `custom` instead of `meta`. ```js import { readMetadata } from '@vivliostyle/vfm' @@ -409,13 +409,15 @@ Results: ```js { title: 'title', - excludes: { + custom: { tags: ['foo', 'bar'] } } ``` -`tags` is stored and retained structure in `excludes` instead of `meta`. +`tags` is stored and retained structure in `custom` instead of `meta`. + +If specify a default key such as `title`, it will be processed as `custom`. #### User-specified metadata diff --git a/src/plugins/metadata.ts b/src/plugins/metadata.ts index 3025d07..2c66f9f 100644 --- a/src/plugins/metadata.ts +++ b/src/plugins/metadata.ts @@ -76,7 +76,7 @@ export type Metadata = { * The data types converted from Frontmatter's YAML are retained. * Use this if want to add custom metadata with a third party tool. */ - excludes?: { + custom?: { [key: string]: any; }; }; @@ -254,23 +254,26 @@ const readSettings = (data: any): VFMSettings => { /** * Read metadata from Markdown frontmatter. * - * Keys that are not defined as VFM are treated as `meta`. If you specify a key name in `excludes`, the key and its data type will be preserved and stored in `excludes` instead of `meta`. + * Keys that are not defined as VFM are treated as `meta`. If you specify a key name in `customKeys`, the key and its data type will be preserved and stored in `custom` instead of `meta`. * @param md Markdown. - * @param excludes A collection of key names to be ignored by meta processing. + * @param customKeys A collection of key names to be ignored by meta processing. * @returns Metadata. */ -export const readMetadata = (md: string, excludes: string[] = []): Metadata => { +export const readMetadata = ( + md: string, + customKeys: string[] = [], +): Metadata => { const metadata: Metadata = {}; const data = parseMarkdown(md); const others: Array> = []; for (const key of Object.keys(data)) { - if (excludes.includes(key)) { - if (!metadata.excludes) { - metadata.excludes = {}; + if (customKeys.includes(key)) { + if (!metadata.custom) { + metadata.custom = {}; } - metadata.excludes[key] = data[key]; + metadata.custom[key] = data[key]; continue; } diff --git a/tests/metadata.test.ts b/tests/metadata.test.ts index e50fd37..3f75d64 100644 --- a/tests/metadata.test.ts +++ b/tests/metadata.test.ts @@ -1,5 +1,5 @@ import { stringify } from '../src/index'; -import { readMetadata } from '../src/plugins/metadata'; +import { Metadata, readMetadata } from '../src/plugins/metadata'; it('Read all', () => { const received = readMetadata( @@ -49,7 +49,7 @@ other-meta2: 'other2' `, ); - const expected = { + const expected: Metadata = { id: 'my-page', lang: 'ja', dir: 'ltr', @@ -512,9 +512,9 @@ tags: ["Foo", "Bar"] --- `; const received = readMetadata(md, ['tags']); - const expected = { + const expected: Metadata = { title: 'Title', - excludes: { + custom: { tags: ['Foo', 'Bar'], }, }; @@ -526,10 +526,9 @@ it('Overwrite key with excludes', () => { title: ["Foo", "Bar"] --- `; - // It's not supposed to, but it can be overridden, so we'll test it. const received = readMetadata(md, ['title']); - const expected = { - excludes: { + const expected: Metadata = { + custom: { title: ['Foo', 'Bar'], }, };