Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@vivliostyle/vfm

vivliostyle2.3kApache-2.02.3.0TypeScript support: included

Custom Markdown syntax specialized in book authoring.

markdown, vfm, vivliostyle

readme

Vivliostyle Flavored Markdown

Actions Status: test npm-badge npm: total downloads npm: license

Vivliostyle Flavored Markdown (VFM), a Markdown syntax optimized for book authoring. It is standardized and published for Vivliostyle and its sibling projects.

Table of contents

Install

npm install -g @vivliostyle/vfm

Use

vfm --help
vfm README.md
echo "# Hello" | vfm

Usage with vivliostyle command

npm i -g @vivliostyle/cli
vfm README.md --style https://raw.githubusercontent.com/jagat-xpub/cosmology/gh-pages/css/scholarly.css > book.html
vivliostyle build book.html -s A4

API

npm install --save @vivliostyle/vfm
import { stringify } from '@vivliostyle/vfm';

console.log(
  stringify(`
# はじめに

{Vivliostyle|ビブリオスタイル}の世界へようこそ。
`),
);

This snippet will generates:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>はじめに</title>
  </head>
  <body>
    <h1>はじめに</h1>
    <p>
      <ruby>Vivliostyle<rt>ビブリオスタイル</rt></ruby
      >の世界へようこそ。
    </p>
  </body>
</html>

Options

style (default: undefined)

Set the specified value as the href attribute of <link rel="stylesheet">.

stringify('# Hello', { style: 'https://example.com/book.css' });

will generates:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="https://example.com/book.css" />
  </head>
  <body>
    <p><h1>Hello</h1></p>
  </body>
</html>

style can be an array of styles.

stringify('# Hello', {
  style: ['https://example.com/book.css', 'https://example.com/extra.css'],
});

will generates:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="https://example.com/book.css" />
    <link rel="stylesheet" href="https://example.com/extra.css" />
  </head>
  <body>
    <p><h1>Hello</h1></p>
  </body>
</html>

partial (default: false)

If true is specified, only the HTML defined in <body> is output.

stringify('# Hello', { partial: true });

will generates:

<p><h1>Hello</h1></p>

title (default: undefined)

Set the specified value as the text of <title>.

stringify('# Hello', { title: 'Hello' });

will generates:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Hello</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
  </head>
  <body>
    <p><h1>Hello</h1></p>
  </body>
</html>

language (default: undefined)

Set the specified value as the lang attribute of <html>.

stringify('# Hello', { language: 'ja' });

will generates:

<!doctype html>
<html lang="ja">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
  </head>
  <body>
    <p><h1>Hello</h1></p>
  </body>
</html>

hardLineBreaks (default: false)

Converts line breaks to <br>.

stringify(
  `
new
line
`,
  { hardLineBreaks: true },
);

will generates:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
  </head>
  <body>
    <p>
      new<br />
      line
    </p>
  </body>
</html>

disableFormatHtml (default: false)

Disable automatic HTML format. Explicitly specify true if want unformatted HTML during development or debug.

stringify(
  `text`,
  { disableFormatHtml: true },
);

will generates:

<!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>

math (default: true)

Handles math syntax. The default value is true, which is valid.

stringify(
  `$x = y$`
);

will generates:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.9/MathJax.js?config=TeX-MML-AM_CHTML"></script>
  </head>
  <body data-math-typeset="true">
    <p><span class="math inline">\(x = y\)</span></p>
  </body>
</html>

To explicitly disable it, specify false for this option or math: false for Markdown's Frontmatter.

stringify(
  `$x = y$`,
  { math: false }
);

will generates:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>
  <body>
    <p>$x = y$</p>
  </body>
</html>

Advanced usage

Unified processor

import { VFM } from '@vivliostyle/vfm';

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

Unified plugin

import unified from 'unified';
import vfm from '@vivliostyle/vfm/lib/revive-parse';
import html from '@vivliostyle/vfm/lib/revive-rehype';

function main() {
  unified()
    .use(vfm)
    .use(customRemarkPlugin)
    .use(html)
    .use(customRehypePlugin)
    .processSync('# Hello');
}

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, customKeys: string[]): Metadata

  • params:
    • md: String Markdown text.
    • customKeys: String[] A collection of key names to be ignored by meta processing.
  • returns:
    • metadata: Metadata Metadata.
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 details, refer to VFM's "Frontmatter" or type information of TypeScript.

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 customKeys, the key and its data type will be preserved and stored in custom instead of meta.

import { readMetadata } from '@vivliostyle/vfm'

const md = `---
title: 'Title'
tags: ['foo', 'bar']
---
`;

const metadata = readMetadata(md, ['tags']);
console.log(metadata);

Results:

{
  title: 'title',
  custom: {
    tags: ['foo', 'bar']
  }
}

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

Metadata can be specified for stringify, this specification takes precedence over Frontmatter.

The following usage is assumed.

  • Use metadata other than Frontmatter
  • Process Frontmatter metadata obtained by readMetadata
stringify(
  '# Title',
  {},
  { title: 'My Page', body: [{ name: 'id', value: 'page' }] },
);

HTML:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>My Page</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>
  <body id="page">
    <section id="title" class="level1">
      <h1>Title</h1>
    </section>
  </body>
</html>

Spec

Current Status

Principles

  1. Open: steadily improving through open discussion and feedback from the vast community.
  2. Consistent: Provides reference implementation for parsing and converting VFM to HTML, allowing other non Vivliostyle projects to use this syntax for their purposes.

Contribution

We want you to:

Maintainers

changelog

2.2.1 (2023-11-05)

Bug Fixes

  • HTML block causes wrong sectioning (vfm 2.2.0) (#184) (6848823), closes #183

Reverts

  • Revert "chore: Update support version of Node.js (#179)" (#180) (cf4776c), closes #179 #180

2.2.0 (2023-10-14)

Bug Fixes

  • HTML block with markdown heading causes wrong HTML sectioning (#174) (c551024), closes #171
  • HTML title generated from heading should not have ruby text and HTML tags (#178) (59fee36), closes #166

Features

  • Support non-sectionize headings, enclosed by equal number of hashes (#172) (463c615), closes #155
  • Support section-end mark (line with only hashes) (#175) (63869ed), closes #155

2.1.0 (2023-03-11)

Features

1.3.0 (2023-03-11)

Features

1.2.2 (2022-05-29)

Bug Fixes

  • remove heading attributes notation on page title (f23ff1f)
  • Type error of object property (e8755d9)

1.2.1 (2022-01-20)

Bug Fixes

  • Rename readMetadata argument and Metadata property excludes to be more understandable (60affea)

1.2.0 (2022-01-20)

Bug Fixes

  • Fixed TOC link in Japanese version (a46c2a3)

Features

  • Add excludes argument to readMetadata (174c8dd)

1.1.0 (2021-12-07)

Features

  • Supports metadata specification for stringify (249cf32)

1.0.2 (2021-07-26)

1.0.1 (2021-07-27)

Features

  • Explicitly support Node.js LTS (0778773)
  • Merge the frontmatter's class properties (f5a3461)

1.0.0-alpha.27 (2021-07-10)

Bug Fixes

  • Fixed an issue where ruby couldn't escape pipe with a backslash (ac111a2)

1.0.0-alpha.26 (2021-07-04)

Features

  • Added VFM settings to Frontmatter (ff4efc7)

1.0.0-alpha.25 (2021-07-03)

Bug Fixes

  • Fixed an issue that caused an error if Object was null for vfm (38906f0)
  • Fixed an issue that caused an error if YAML object was null (43233a3)
  • Fixed to be empty string instead of null when only key is specified in Frontmatter YAML (1a64e33)

1.0.0-alpha.24 (2021-06-28)

Bug Fixes

  • Fixed an issue where the optional style specification was ignored (88bdb6c)

Features

  • Expose readMetadata API (6ff37c5)

1.0.0-alpha.23 (2021-06-27)

Bug Fixes

  • Don't convert dates and times that aren't quoted (4adef06)

Features

  • Implemented document processing as VFM in case of major metadeta design changes (fb57dae)
  • Improved metadata definition with frontmatter (449fa46)

1.0.0-alpha.22 (2021-05-15)

Bug Fixes

  • Fixed an issue where inline footnotes were copied to title tag and section id when heading was defined (59f8db0)

Features

1.0.0-alpha.21 (2021-05-02)

Bug Fixes

  • Error of tsc (6e3b85a)
  • Math syntax attributes should have been specified in the wrapping <span> instead of the <body>. (13dbc79)

Features

  • If math syntax is enabled and <math> is detected, <script> is output. (6a707ef)
  • Math syntax enabled default (408a623)

1.0.0-alpha.20 (2021-04-30)

Features

  • hidden specification of the heading is output as an attribute (a933f6b)
  • Added section leveling (021b72f)
  • Copy the section attributes from the heading, however the id will be moved (a355dd5)
  • Disable section with blockquote heading (f493797)

1.0.0-alpha.19 (2021-04-26)

Bug Fixes

  • change data-math-typeset attribute to string boolean (ee733b7)
  • change the value of data-math-typeset from boolean to string (ab3442b)
  • incorrect specification of characters immediately before/after the start and end in math syntax (e0948ec)

Features

  • add html tag for math syntax (bcffbda)
  • escape $ with an even number of \ and one-letter expression in math syntax (inline) (26e4cb8)
  • implemented math syntax with MathJax (1f62c24)
  • implemented MathJax multi-line specification, pre/post-conditions for start and end symbols, and escape (2611e92)
  • one-letter expression in math syntax (display) (13f195a)
  • set aria-hidden: true to <figcaption> so that screen readers don't even read <img>'s alt and <figcaption> (91bb30c)

1.0.0-alpha.18 (2021-04-09)

Bug Fixes

  • fixed a crash when specifying the HTML pre tag (3fbbb82)
  • Fixed type overload inconsistencies by casting (59b887e)

Features

  • frontmatter and metadata (d12188b)

1.0.0-alpha.17 (2021-02-22)

1.0.0-alpha.16 (2021-01-22)

Bug Fixes

Features

  • Added --auto-line-breaks option to CLI (24fe380)
  • Change automatic line breaks to optional (bebc736)
  • Changed to keep the default value per property by expanding the option (3a691f6)
  • Renamed line breaks option (494185a)

1.0.0-alpha.15 (2021-01-14)

Bug Fixes

  • replace() throws Error if empty ReplaceRules array is given (b0cfb6a)

1.0.0-alpha.14 (2021-01-11)

1.0.0-alpha.13 (2021-01-11)

1.0.0-alpha.12 (2021-01-11)

1.0.0-alpha.11 (2020-12-16)

Bug Fixes

Features

1.0.0-alpha.10 (2020-07-02)

Bug Fixes

  • unwanted code attribution (098eeb1)

Features

1.0.0-alpha.9 (2020-07-02)

Features

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

1.0.0-alpha.5 (2020-06-24)

Bug Fixes

Features

1.0.0-alpha.4 (2020-06-24)

Bug Fixes

Features

  • auto-generate id for headings (c2b3433)

1.0.0-alpha.3 (2020-06-15)

Features

1.0.0-alpha.2 (2020-06-14)

Bug Fixes

  • strict checks on nested fence symbols (cd4fa32)

Features