Static CMS
Star StaticJsCMS/static-cms on GitHub
DocsContributingCommunity

Markdown

The markdown widget provides a full fledged text editor allowing users to format text with features such as headings and blockquotes. Users can change their editing view with a handy toggle button.

Please note: If you want to use your markdown editor to fill a markdown file contents after its frontmatter, you'll have to name the field body so the CMS recognizes it and saves the file accordingly.

Widget Options

For common options, see Common widget options.

NameTypeDefaultDescription
defaultstring''Optional. The default value for the field. Accepts markdown content
media_libraryMedia Library Options{}Optional. Media library settings to apply when a media library is opened by the current widget. See Media Library Options
media_folderstringOptional. Specifies the folder path where uploaded files should be saved, relative to the base of the repo
public_folderstringOptional. Specifies the folder path where the files uploaded by the media library will be accessed, relative to the base of the built site

Media Library Options

NameTypeDefaultDescription
allow_multiplebooleantrueOptional. When set to false, prevents multiple selection for any media library extension, but must be supported by the extension in use
configstring{}Optional. A configuration object that will be passed directly to the media library being used - available options are determined by the library
choose_urlstring
| boolean
trueOptional. When set to false, the "Insert from URL" button will be hidden

Example

name: body
label: Blog post content
widget: markdown

This would render as:

Markdown widget example

Please note: The markdown widget outputs a raw markdown string. Your static site generator may or may not render the markdown to HTML automatically. Consult with your static site generator's documentation for more information about rendering markdown.

Shortcodes

Shortcodes can be added to customize the Markdown editor via registerShortcode.

Usage

CMS.registerShortcode('youtube', {
  label: 'YouTube',
  openTag: '[',
  closeTag: ']',
  separator: '|',
  toProps: args => {
    if (args.length > 0) {
      return { src: args[0] };
    }

    return { src: '' };
  },
  toArgs: ({ src }) => {
    return [src];
  },
  control: ({ src, onChange }) => {
    return h('span', {}, [
      h('input', {
        key: 'control-input',
        value: src,
        onChange: event => {
          onChange({ src: event.target.value });
        },
      }),
      h(
        'iframe',
        {
          key: 'control-preview',
          width: '420',
          height: '315',
          src: `https://www.youtube.com/embed/${src}`,
        },
        '',
      ),
    ]);
  },
  preview: ({ src }) => {
    return h(
      'span',
      {},
      h(
        'iframe',
        {
          width: '420',
          height: '315',
          src: `https://www.youtube.com/embed/${src}`,
        },
        '',
      ),
    );
  },
});