👀 Viewer

What Is Viewer?

TOAST UI Editor (henceforth referred to as ‘Editor’) provides the viewer in case you want to show Markdown content without loading the Editor. The Viewer is much lighter than the Editor.

Creating Viewer

The method of creating the Viewer is similar to that of the Editor.

Ref. Getting Started

Adding Wrapper Element

You need to add the container element where the Viewer will be created.

  1. ...
  2. <body>
  3. <div id="viewer"></div>
  4. </body>
  5. ...

Importing Viewer’s Constructor Function

The Viewer can be used by creating an instance with the constructor function. To get the constructor function, you should import the module using one of the following ways depending on your environment.

Using Module Format in Node Environment

  • ES6 Modules
  1. import Viewer from '@toast-ui/editor/dist/toastui-editor-viewer';
  • CommonJS
  1. const Viewer = require('@toast-ui/dist/toastui-editor-viewer');

Using Namespace in Browser Environment

  1. const Viewer = toastui.Editor;

Note that the CDN file of the Viewer should use the following:

  1. ...
  2. <body>
  3. ...
  4. <script src="https://uicdn.toast.com/editor/latest/toastui-editor-viewer.js"></script>
  5. </body>
  6. ...

Adding CSS Files

You need to add the CSS files needed for the Viewer. Import CSS files in node environment, and add it to html file when using CDN.

Using in Node Environment

  • ES6 Modules
  1. import '@toast-ui/editor/dist/toastui-editor-viewer.css';
  • CommonJS
  1. require('@toast-ui/editor/dist/toastui-editor-viewer.css');

Using in Browser Environment by CDN

  1. ...
  2. <head>
  3. ...
  4. <link rel="stylesheet" href="https://uicdn.toast.com/editor/latest/toastui-editor-viewer.min.css" />
  5. </head>
  6. ...

Creating Instance

You can create an instance with options and call various API after creating an instance.

  1. const viewer = new Viewer({
  2. el: document.querySelector('#viewer'),
  3. height: '600px',
  4. initialValue: '# hello'
  5. });

viewer-01

The basic options available are:

  • height: Height in string or auto ex) 300px | auto
  • initialValue: Initial value. Set Markdown string

Find out more options here.

Another Way to Create Viewer

Be careful not to load both an editor and a viewer at the same time because an editor already contains a viewer function, you can initialize with Editor.factory() of an editor and set the viewer option to value true in order to make the a viewer.

  1. import Editor from '@toast-ui/editor';
  2. const viewer = Editor.factory({
  3. el: document.querySelector('#viewer'),
  4. viewer: true,
  5. height: '500px',
  6. initialValue: '# hello'
  7. });

Example

You can see the example here.