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.
The method of creating the Viewer is similar to that of the Editor.
Ref. Getting Started
You need to add the container element where the Viewer will be created.
...
<body>
<div id="viewer"></div>
</body>
...
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.
- ES6 Modules
import Viewer from '@toast-ui/editor/dist/toastui-editor-viewer';
- CommonJS
const Viewer = require('@toast-ui/dist/toastui-editor-viewer');
const Viewer = toastui.Editor;
Note that the CDN file of the Viewer should use the following:
...
<body>
...
<script src="https://uicdn.toast.com/editor/latest/toastui-editor-viewer.js"></script>
</body>
...
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.
- ES6 Modules
import '@toast-ui/editor/dist/toastui-editor-viewer.css';
- CommonJS
require('@toast-ui/editor/dist/toastui-editor-viewer.css');
...
<head>
...
<link rel="stylesheet" href="https://uicdn.toast.com/editor/latest/toastui-editor-viewer.min.css" />
</head>
...
You can create an instance with options and call various API after creating an instance.
const viewer = new Viewer({
el: document.querySelector('#viewer'),
height: '600px',
initialValue: '# hello'
});
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.
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.
import Editor from '@toast-ui/editor';
const viewer = Editor.factory({
el: document.querySelector('#viewer'),
viewer: true,
height: '500px',
initialValue: '# hello'
});
You can see the example here.