Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added VFM settings to Frontmatter #113

Merged
merged 1 commit into from
Jul 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions docs/vfm.md
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,9 @@ script:
vfm:
math: false
theme: 'theme.css'
partial: false
hardLineBreaks: false
disableFormatHtml: false
author: 'Author'
---

Expand Down Expand Up @@ -506,10 +509,13 @@ author: 'Author'

**vfm**

|Property|Type|Description|
|---|---|---|
|`math`|Boolean|Enable math syntax, default 'true'.|
|`theme`|String|Vivliostyle theme package or bare CSS file.|
| Property | Type | Default | Description |
| ------------------: | :-------: | :-----: | --- |
| `math` | `Boolean` | `true` | Enable math syntax. |
| `partial` | `Boolean` | `false` | Output markdown fragments. |
| `hardLineBreaks` | `Boolean` | `false` | Add `<br>` at the position of hard line breaks, without needing spaces. |
| `disableFormatHtml` | `Boolean` | `false` | Disable automatic HTML format. |
| `theme` | `String` | - | Vivliostyle theme package or bare CSS file. |

### Priority with options

Expand Down
19 changes: 16 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export * from './plugins/metadata';
export interface StringifyMarkdownOptions {
/** Custom stylesheet path/URL. */
style?: string | string[];
/** Output markdown fragments. */
/** Output markdown fragments. */
partial?: boolean;
/** Document title (ignored in partial mode). */
title?: string;
Expand Down Expand Up @@ -95,8 +95,21 @@ export function VFM(
metadata: Metadata = {},
): Processor {
checkMetadata(metadata, { style, title, language });
if (metadata.vfm && metadata.vfm.math !== undefined) {
math = metadata.vfm.math;

// Prioritize metadata `vfm` settings over options
if (metadata.vfm) {
if (metadata.vfm.math !== undefined) {
math = metadata.vfm.math;
}
if (metadata.vfm.partial !== undefined) {
partial = metadata.vfm.partial;
}
if (metadata.vfm.hardLineBreaks !== undefined) {
hardLineBreaks = metadata.vfm.hardLineBreaks;
}
if (metadata.vfm.disableFormatHtml !== undefined) {
disableFormatHtml = metadata.vfm.disableFormatHtml;
}
}

const processor = unified()
Expand Down
17 changes: 16 additions & 1 deletion src/plugins/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ export type Attribute = {
export type VFMSettings = {
/** Enable math syntax. */
math?: boolean;
/** Output markdown fragments. */
partial?: boolean;
/** Add `<br>` at the position of hard line breaks, without needing spaces. */
hardLineBreaks?: boolean;
/** Disable automatic HTML format. */
disableFormatHtml?: boolean;
/** Path of theme. */
theme?: string;
/** Enable TOC mode. */
toc: boolean;
toc?: boolean;
};

/** Metadata from Frontmatter. */
Expand Down Expand Up @@ -222,6 +228,15 @@ const readSettings = (data: any): VFMSettings => {

return {
math: typeof data.math === 'boolean' ? data.math : undefined,
partial: typeof data.partial === 'boolean' ? data.partial : undefined,
hardLineBreaks:
typeof data.hardLineBreaks === 'boolean'
? data.hardLineBreaks
: undefined,
disableFormatHtml:
typeof data.disableFormatHtml === 'boolean'
? data.disableFormatHtml
: undefined,
theme: typeof data.theme === 'string' ? data.theme : undefined,
toc: typeof data.toc === 'boolean' ? data.toc : false,
};
Expand Down
68 changes: 68 additions & 0 deletions tests/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ script:
src: 'sample2.js'
vfm:
math: false
partial: true
hardLineBreaks: true
disableFormatHtml: true
theme: 'theme.css'
author: 'Author'
other-meta1: 'other1'
Expand Down Expand Up @@ -111,6 +114,9 @@ other-meta2: 'other2'
],
vfm: {
math: false,
partial: true,
hardLineBreaks: true,
disableFormatHtml: true,
toc: false,
theme: 'theme.css',
},
Expand Down Expand Up @@ -414,3 +420,65 @@ head: |
`;
expect(received).toBe(expected);
});

it('vfm.partial: true', () => {
const md = `---
vfm:
partial: true
---

Text
`;
const received = stringify(md, { partial: false });
const expected = `
<p>Text</p>
`;
expect(received).toBe(expected);
});

it('vfm.hardLineBreaks: true', () => {
const md = `---
vfm:
hardLineBreaks: true
---

Text
Text
`;
const received = stringify(md, { hardLineBreaks: false });
const expected = `<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<p>Text<br>Text</p>
</body>
</html>
`;
expect(received).toBe(expected);
});

it('vfm.disableFormatHtml: true', () => {
const md = `---
vfm:
disableFormatHtml: true
---

Text
`;
const received = stringify(md, { disableFormatHtml: false });
const expected = `<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<p>Text</p>
</body>
</html>
`;
expect(received).toBe(expected);
});