Skip to content

Commit

Permalink
feat: Expose readMetadata API
Browse files Browse the repository at this point in the history
  • Loading branch information
akabekobeko committed Jun 28, 2021
1 parent 53e04a4 commit 6ff37c5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
40 changes: 38 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Vivliostyle Flavored Markdown (VFM), a Markdown syntax optimized for book author
- [Advanced usage](#advanced-usage)
- [Unified processor](#unified-processor)
- [Unified plugin](#unified-plugin)
- [readMetadata](#readmetadata)
- [Spec](#spec)
- [Principles](#principles)
- [Links](#links)
Expand Down Expand Up @@ -71,7 +72,7 @@ yarn add @vivliostyle/vfm
```

```js
const { stringify } = require('@vivliostyle/vfm');
import { stringify } from '@vivliostyle/vfm';

console.log(
stringify(`
Expand All @@ -90,6 +91,7 @@ This snippet will generates:
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>はじめに</title>
</head>
<body>
<h1>はじめに</h1>
Expand Down Expand Up @@ -330,7 +332,7 @@ import { VFM } from '@vivliostyle/vfm';

const processor = VFM({ partial: true });
const html = processor.processSync('# Hello').toString();
````
```

#### Unified plugin

Expand All @@ -349,6 +351,40 @@ function main() {
}
```

#### readMetadata

Read metadata from Markdown frontmatter.

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

`readMetadata(md: string): Metadata`

- params:
- `md`: `String` Markdown text.
- returns:
- `metadata`: `Metadata` Metadata.

```js
import { readMetadata } from '@vivliostyle/vfm'

const md = `---
id: 'my-page'
lang: 'en'
dir: 'ltr'
class: 'my-class'
title: 'Title'
vfm:
math: false
theme: 'sample.css'
---
`;

const metadata = readMetadata(md);
console.log(metadata);
```

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

## Spec

[Current Status](https://github.com/vivliostyle/vfm/projects/1)
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import rehypeFormat from 'rehype-format';
import rehypeStringify from 'rehype-stringify';
import unified, { Processor } from 'unified';
import { mdast as doc } from './plugins/document';
//import { mdast as doc } from './plugins/document.old';
import { hast as hastMath } from './plugins/math';
import { Metadata, readMetadata } from './plugins/metadata';
import { replace as handleReplace, ReplaceRule } from './plugins/replace';
import { reviveParse as markdown } from './revive-parse';
import { reviveRehype as html } from './revive-rehype';
import { debug } from './utils';

// Expose metadata reading by VFM
export * from './plugins/metadata';

/**
* Option for convert Markdown to a stringify (HTML).
*/
Expand Down

0 comments on commit 6ff37c5

Please sign in to comment.