Skip to content

Commit 67d19c4

Browse files
authored
Merge pull request #144 from vivliostyle/fix-param-name-excludes
fix: Rename `readMetadata` argument and `Metadata` property `excludes` to be more understandable
2 parents 1309117 + bb1a0ac commit 67d19c4

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,11 +356,11 @@ Read metadata from Markdown frontmatter.
356356

357357
Useful if just want to get the metadata, Markdown parse and metadata typing (for TypeScript) are handled by the VFM side.
358358

359-
`readMetadata(md: string, excludes: string[]): Metadata`
359+
`readMetadata(md: string, customKeys: string[]): Metadata`
360360

361361
- params:
362362
- `md`: `String` Markdown text.
363-
- `excludes`: `String[]` A collection of key names to be ignored by meta processing.
363+
- `customKeys`: `String[]` A collection of key names to be ignored by meta processing.
364364
- returns:
365365
- `metadata`: `Metadata` Metadata.
366366

@@ -385,11 +385,11 @@ console.log(metadata);
385385

386386
About `Metadata` details, refer to [VFM](https://vivliostyle.github.io/vfm/#/vfm)'s "Frontmatter" or type information of TypeScript.
387387

388-
**About `excludes`**
388+
**About `customKeys`**
389389

390390
Use this if want to add custom metadata with a third party tool.
391391

392-
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`.
392+
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`.
393393

394394
```js
395395
import { readMetadata } from '@vivliostyle/vfm'
@@ -409,13 +409,15 @@ Results:
409409
```js
410410
{
411411
title: 'title',
412-
excludes: {
412+
custom: {
413413
tags: ['foo', 'bar']
414414
}
415415
}
416416
```
417417

418-
`tags` is stored and retained structure in `excludes` instead of `meta`.
418+
`tags` is stored and retained structure in `custom` instead of `meta`.
419+
420+
If specify a default key such as `title`, it will be processed as `custom`.
419421

420422
#### User-specified metadata
421423

src/plugins/metadata.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export type Metadata = {
7676
* The data types converted from Frontmatter's YAML are retained.
7777
* Use this if want to add custom metadata with a third party tool.
7878
*/
79-
excludes?: {
79+
custom?: {
8080
[key: string]: any;
8181
};
8282
};
@@ -254,23 +254,26 @@ const readSettings = (data: any): VFMSettings => {
254254
/**
255255
* Read metadata from Markdown frontmatter.
256256
*
257-
* 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`.
257+
* 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`.
258258
* @param md Markdown.
259-
* @param excludes A collection of key names to be ignored by meta processing.
259+
* @param customKeys A collection of key names to be ignored by meta processing.
260260
* @returns Metadata.
261261
*/
262-
export const readMetadata = (md: string, excludes: string[] = []): Metadata => {
262+
export const readMetadata = (
263+
md: string,
264+
customKeys: string[] = [],
265+
): Metadata => {
263266
const metadata: Metadata = {};
264267
const data = parseMarkdown(md);
265268
const others: Array<Array<Attribute>> = [];
266269

267270
for (const key of Object.keys(data)) {
268-
if (excludes.includes(key)) {
269-
if (!metadata.excludes) {
270-
metadata.excludes = {};
271+
if (customKeys.includes(key)) {
272+
if (!metadata.custom) {
273+
metadata.custom = {};
271274
}
272275

273-
metadata.excludes[key] = data[key];
276+
metadata.custom[key] = data[key];
274277
continue;
275278
}
276279

tests/metadata.test.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { stringify } from '../src/index';
2-
import { readMetadata } from '../src/plugins/metadata';
2+
import { Metadata, readMetadata } from '../src/plugins/metadata';
33

44
it('Read all', () => {
55
const received = readMetadata(
@@ -49,7 +49,7 @@ other-meta2: 'other2'
4949
5050
`,
5151
);
52-
const expected = {
52+
const expected: Metadata = {
5353
id: 'my-page',
5454
lang: 'ja',
5555
dir: 'ltr',
@@ -512,9 +512,9 @@ tags: ["Foo", "Bar"]
512512
---
513513
`;
514514
const received = readMetadata(md, ['tags']);
515-
const expected = {
515+
const expected: Metadata = {
516516
title: 'Title',
517-
excludes: {
517+
custom: {
518518
tags: ['Foo', 'Bar'],
519519
},
520520
};
@@ -526,10 +526,9 @@ it('Overwrite key with excludes', () => {
526526
title: ["Foo", "Bar"]
527527
---
528528
`;
529-
// It's not supposed to, but it can be overridden, so we'll test it.
530529
const received = readMetadata(md, ['title']);
531-
const expected = {
532-
excludes: {
530+
const expected: Metadata = {
531+
custom: {
533532
title: ['Foo', 'Bar'],
534533
},
535534
};

0 commit comments

Comments
 (0)