simplemde-markdown-editor/README.md

264 lines
12 KiB
Markdown
Raw Normal View History

2015-06-22 22:01:44 +02:00
# SimpleMDE - Markdown Editor
2015-09-01 06:30:55 +02:00
A drop-in JavaScript textarea replacement for writing beautiful and understandable Markdown. The WYSIWYG-esque editor allows users who may be less experienced with Markdown to use familiar toolbar buttons and shortcuts. In addition, the syntax is rendered while editing to clearly show the expected result. Headings are larger, emphasized words are italicized, links are underlined, etc. SimpleMDE is one of the first editors to feature both built-in autosaving and spell checking.
2015-06-22 20:16:28 +02:00
2015-08-18 16:16:55 +02:00
[**Demo**](http://nextstepwebs.github.io/simplemde-markdown-editor)
2015-06-22 23:04:58 +02:00
2015-09-02 05:36:05 +02:00
[![Preview](http://i.imgur.com/zqWfJwO.png)](http://nextstepwebs.github.io/simplemde-markdown-editor)
2015-06-22 20:16:28 +02:00
2015-08-18 16:15:04 +02:00
## Why not a WYSIWYG editor or pure Markdown?
2015-08-18 16:16:02 +02:00
WYSIWYG editors that produce HTML are often complex and buggy. Markdown solves this problem in many ways, plus Markdown can be rendered natively on more platforms than HTML. However, Markdown is not a syntax that an average user will be familiar with, nor is it visually clear while editing. In otherwords, for an unfamiliar user, the syntax they write will make little sense until they click the preview button. SimpleMDE has been designed to bridge this gap for non-technical users who are less familiar with or just learning Markdown syntax.
2015-08-18 16:15:04 +02:00
## Install
Via [npm](https://www.npmjs.com/package/simplemde).
2015-09-02 17:50:10 +02:00
```
npm install simplemde --save
```
Via [bower](https://www.bower.io).
```
bower install simplemde --save
```
Via [jsDelivr](http://www.jsdelivr.com/#!simplemde). *Please note, jsDelivr may take a few days to update to the latest release.*
2015-06-22 20:16:28 +02:00
2015-06-26 20:52:19 +02:00
```HTML
2015-06-23 04:30:04 +02:00
<link rel="stylesheet" href="//cdn.jsdelivr.net/simplemde/latest/simplemde.min.css">
<script src="//cdn.jsdelivr.net/simplemde/latest/simplemde.min.js"></script>
2015-06-22 20:16:28 +02:00
```
## Quick start
After installing, load SimpleMDE on the first textarea on a page
2015-06-22 20:16:28 +02:00
2015-06-26 20:55:23 +02:00
```HTML
2015-06-26 20:52:19 +02:00
<script>
2015-06-22 22:01:44 +02:00
var simplemde = new SimpleMDE();
2015-06-26 20:52:19 +02:00
</script>
2015-06-22 20:16:28 +02:00
```
2015-10-20 18:04:24 +02:00
#### Using a specific textarea
2015-06-22 20:16:28 +02:00
Pure JavaScript method
2015-06-26 20:55:23 +02:00
```HTML
2015-06-26 20:52:19 +02:00
<script>
2015-08-12 06:25:37 +02:00
var simplemde = new SimpleMDE({ element: document.getElementById("MyID") });
2015-06-26 20:52:19 +02:00
</script>
2015-06-22 20:16:28 +02:00
```
jQuery method
2015-06-26 20:54:04 +02:00
```HTML
2015-06-26 20:52:19 +02:00
<script>
2015-08-12 06:25:37 +02:00
var simplemde = new SimpleMDE({ element: $("#MyID")[0] });
2015-06-26 20:52:19 +02:00
</script>
2015-06-22 20:16:28 +02:00
```
## Get/set the content
2015-06-22 20:16:28 +02:00
2015-06-26 20:52:19 +02:00
```JavaScript
2015-06-26 22:33:28 +02:00
simplemde.value();
2015-06-22 20:16:28 +02:00
```
```JavaScript
2015-09-04 17:32:47 +02:00
simplemde.value("This text will appear in the editor");
```
2015-06-22 20:16:28 +02:00
## Configuration
2015-09-20 03:42:20 +02:00
- **autoDownloadFontAwesome**: If set to `true`, force downloads Font Awesome (used for icons). If set to false, prevents downloading. Defaults to `undefined`, which will intelligently check whether Font Awesome has already been included, then download accordingly.
2015-06-26 22:33:28 +02:00
- **autofocus**: If set to `true`, autofocuses the editor. Defaults to `false`.
2015-11-04 05:24:28 +01:00
- **autosave**: *Saves the text that's being written and will load it back in the future. It will forget the text when the form it's contained in is submitted.*
2015-06-26 22:36:38 +02:00
- **enabled**: If set to `true`, autosave the text. Defaults to `false`.
2015-06-26 22:33:28 +02:00
- **delay**: Delay between saves, in milliseconds. Defaults to `10000` (10s).
2015-11-19 21:47:15 +01:00
- **uniqueId**: You must set a unique string identifier so that SimpleMDE can autosave. Something that separates this from other instances of SimpleMDE elsewhere on your website.
2015-09-17 07:57:03 +02:00
- **element**: The DOM element for the textarea to use. Defaults to the first textarea on the page.
2015-09-24 22:03:20 +02:00
- **hideIcons**: An array of icon names to hide. Can be used to hide specific icons without completely customizing the toolbar.
2015-09-17 07:57:03 +02:00
- **indentWithTabs**: If set to `false`, indent using spaces instead of tabs. Defaults to `true`.
- **initialValue**: If set, will customize the initial value of the editor.
2015-11-03 18:28:06 +01:00
- **insertTexts**: Customize how certain buttons that insert text behave. Takes an array with two elements. The first element will be the text inserted before the cursor or highlight, and the second element will be inserted after. For example, this is the default link value: `["[", "](http://)"]`.
- horizontalRule
- image
- link
2015-11-25 11:07:51 +01:00
- table
- **blockStyles**: Customize how certain buttons that style blocks of text behave.
- **bold** Can be set to `**` or `__`. Defaults to `**`
- **italic** Can be set to `*` or `_`. Defaults to `*`
2015-09-17 07:57:03 +02:00
- **lineWrapping**: If set to `false`, disable line wrapping. Defaults to `true`.
2015-09-17 07:40:57 +02:00
- **parsingConfig**: Adjust settings for parsing the Markdown during editing (not previewing).
2015-11-03 18:28:06 +01:00
- **allowAtxHeaderWithoutSpace**: If set to `true`, will render headers without a space after the `#`. Defaults to `false`.
- **strikethrough**: If set to `false`, will not process GFM strikethrough syntax. Defaults to `true`.
2015-09-17 07:40:57 +02:00
- **underscoresBreakWords**: If set to `true`, let underscores be a delimiter for separating words. Defaults to `false`.
2015-09-17 07:57:03 +02:00
- **previewRender**: Custom function for parsing the plaintext Markdown and returning HTML. Used when user previews.
- **renderingConfig**: Adjust settings for parsing the Markdown during previewing (not editing).
- **singleLineBreaks**: If set to `false`, disable parsing GFM single line breaks. Defaults to `true`.
2015-09-25 23:48:43 +02:00
- **codeSyntaxHighlighting**: If set to `true`, will highlight using [highlight.js](https://github.com/isagalaev/highlight.js). Defaults to `false`. To use this feature you must include highlight.js on your page. For example, include the script and the CSS files like:<br>`<script src="https://cdn.jsdelivr.net/highlight.js/latest/highlight.min.js"></script>`<br>`<link rel="stylesheet" href="https://cdn.jsdelivr.net/highlight.js/latest/styles/github.min.css">`
2015-09-17 07:57:03 +02:00
- **spellChecker**: If set to `false`, disable the spell checker. Defaults to `true`.
- **status**: If set to `false`, hide the status bar. Defaults to `true`.
- Optionally, you can set an array of status bar elements to include, and in what order.
- **tabSize**: If set, customize the tab size. Defaults to `2`.
- **toolbar**: If set to `false`, hide the toolbar. Defaults to the [array of icons](#toolbar-icons).
- **toolbarTips**: If set to `false`, disable toolbar button tips. Defaults to `true`.
2015-06-22 20:16:28 +02:00
2015-06-26 20:52:19 +02:00
```JavaScript
2015-11-03 18:30:09 +01:00
// Most options demonstrate the non-default behavior
2015-06-26 22:35:24 +02:00
var simplemde = new SimpleMDE({
2015-06-26 19:53:24 +02:00
autofocus: true,
2015-06-26 22:33:28 +02:00
autosave: {
enabled: true,
2015-11-19 21:47:15 +01:00
uniqueId: "MyUniqueID",
2015-06-26 22:33:28 +02:00
delay: 1000,
},
2015-09-17 07:57:03 +02:00
element: document.getElementById("MyID"),
2015-09-24 22:03:20 +02:00
hideIcons: ["guide", "heading"],
2015-09-17 07:57:03 +02:00
indentWithTabs: false,
initialValue: "Hello world!",
2015-11-03 18:28:06 +01:00
insertTexts: {
horizontalRule: ["", "\n\n-----\n\n"],
image: ["![](http://", ")"],
link: ["[", "](http://)"],
2015-11-25 11:07:51 +01:00
table: ["", "\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Text | Text | Text |\n\n"],
2015-11-03 18:28:06 +01:00
},
2015-09-17 07:57:03 +02:00
lineWrapping: false,
parsingConfig: {
allowAtxHeaderWithoutSpace: true,
strikethrough: false,
underscoresBreakWords: true,
},
2015-09-04 00:59:50 +02:00
previewRender: function(plainText) {
return customMarkdownParser(plainText); // Returns HTML from a custom parser
2015-09-05 21:28:50 +02:00
},
previewRender: function(plainText, preview) { // Async method
setTimeout(function(){
preview.innerHTML = customMarkdownParser(plainText);
}, 250);
return "Loading...";
},
renderingConfig: {
singleLineBreaks: false,
codeSyntaxHighlighting: true,
},
2015-09-17 07:57:03 +02:00
spellChecker: false,
status: false,
2015-11-03 18:37:00 +01:00
status: ["autosave", "lines", "words", "cursor"], // Optional usage
2015-09-17 07:57:03 +02:00
tabSize: 4,
toolbar: false,
toolbarTips: false,
2015-06-22 20:16:28 +02:00
});
2015-06-26 20:52:19 +02:00
```
#### Toolbar icons
2015-11-03 17:51:38 +01:00
Below are the built-in toolbar icons (only some of which are enabled by default), which can be reorganized however you like. "Name" is the name of the icon, referenced in the JS. "Action" is either a function or a URL to open. "Class" is the class given to the icon. "Tooltip" is the small tooltip that appears via the `title=""` attribute. Any `Ctrl` or `Alt` in the title tags will be converted automatically to their Mac equivalents when needed. Additionally, you can add a separator between any icons by adding `"|"` to the toolbar array.
2015-09-17 08:01:28 +02:00
Name | Action | Tooltip<br>Class
:--- | :----- | :--------------
bold | toggleBold | Bold (Ctrl+B)<br>fa fa-bold
italic | toggleItalic | Italic (Ctrl+I)<br>fa fa-italic
strikethrough | toggleStrikethrough | Strikethrough<br>fa fa-strikethrough
heading | toggleHeadingSmaller | Heading (Ctrl+H)<br>fa fa-header
heading-smaller | toggleHeadingSmaller | Smaller Heading (Ctrl+H)<br>fa fa-header
heading-bigger | toggleHeadingBigger | Bigger Heading (Shift+Ctrl+H)<br>fa fa-lg fa-header
heading-1 | toggleHeading1 | Big Heading<br>fa fa-header fa-header-x fa-header-1
heading-2 | toggleHeading2 | Medium Heading<br>fa fa-header fa-header-x fa-header-2
heading-3 | toggleHeading3 | Small Heading<br>fa fa-header fa-header-x fa-header-3
code | toggleCodeBlock | Code (Ctrl+Alt+C)<br>fa fa-code
quote | toggleBlockquote | Quote (Ctrl+')<br>fa fa-quote-left
unordered-list | toggleUnorderedList | Generic List (Ctrl+L)<br>fa fa-list-ul
ordered-list | toggleOrderedList | Numbered List (Ctrl+Alt+L)<br>fa fa-list-ol
link | drawLink | Create Link (Ctrl+K)<br>fa fa-link
image | drawImage | Insert Image (Ctrl+Alt+I)<br>fa fa-picture-o
horizontal-rule | drawHorizontalRule | Insert Horizontal Line<br>fa fa-minus
2015-11-25 11:07:51 +01:00
table | drawTable | Insert Table<br>fa fa-table
preview | togglePreview | Toggle Preview (Ctrl+P)<br>fa fa-eye no-disable
side-by-side | toggleSideBySide | Toggle Side by Side (F9)<br>fa fa-columns no-disable no-mobile
fullscreen | toggleFullScreen | Toggle Fullscreen (F11)<br>fa fa-arrows-alt no-disable no-mobile
2015-09-17 08:01:28 +02:00
guide | [This link](http://nextstepwebs.github.io/simplemde-markdown-editor/markdown-guide) | Markdown Guide<br>fa fa-question-circle
Customize the toolbar using the `toolbar` option like:
```JavaScript
2015-08-17 23:34:05 +02:00
// Customize only the order of existing buttons
var simplemde = new SimpleMDE({
toolbar: ["bold", "italic", "heading", "|", "quote"],
});
// Customize all information and/or add your own icons
var simplemde = new SimpleMDE({
toolbar: [{
2015-07-14 19:58:28 +02:00
name: "bold",
2015-11-03 17:49:15 +01:00
action: SimpleMDE.toggleBold,
className: "fa fa-bold",
2015-11-03 17:51:38 +01:00
title: "Bold (Ctrl+B)",
},
"|", // Separator
...
],
});
```
#### Height
2015-06-26 20:52:19 +02:00
To change the minimum height (before it starts auto-growing):
```CSS
.CodeMirror, .CodeMirror-scroll {
min-height: 200px;
2015-06-26 20:52:19 +02:00
}
```
Or, you can keep the height static:
```CSS
.CodeMirror {
height: 300px;
}
2015-06-22 20:16:28 +02:00
```
2015-06-22 20:25:42 +02:00
2015-07-11 17:09:02 +02:00
## Event handling
You can catch the following list of events: https://codemirror.net/doc/manual.html#events
2015-07-20 18:49:47 +02:00
```JavaScript
2015-07-11 17:09:02 +02:00
var simplemde = new SimpleMDE();
simplemde.codemirror.on("change", function(){
console.log(simplemde.value());
});
```
## Useful methods
The following self-explanatory methods may be of use while developing with SimpleMDE.
2015-10-20 22:03:43 +02:00
```js
var simplemde = new SimpleMDE();
simplemde.isPreviewActive(); // returns boolean
simplemde.isSideBySideActive(); // returns boolean
simplemde.isFullscreenActive(); // returns boolean
simplemde.clearAutosavedValue(); // no returned value
```
2015-06-22 20:30:38 +02:00
## How it works
2015-06-26 21:00:45 +02:00
SimpleMDE is an improvement of [lepture's Editor project](https://github.com/lepture/editor) and includes a great many number of changes. It is bundled with [CodeMirror](https://github.com/codemirror/codemirror) and depends on [Font Awesome](http://fortawesome.github.io/Font-Awesome/).
2015-06-22 20:30:38 +02:00
2015-09-01 06:30:55 +02:00
CodeMirror is the backbone of the project and parses much of the Markdown syntax as it's being written. This allows us to add styles to the Markdown that's being written. Additionally, a toolbar and status bar have been added to the top and bottom, respectively. Previews are rendered by [Marked](https://github.com/chjj/marked) using GFM.
2015-06-22 20:30:38 +02:00
2015-06-22 20:25:55 +02:00
## What's changed?
2015-06-22 22:01:44 +02:00
As mentioned earlier, SimpleMDE is an improvement of [lepture's Editor project](https://github.com/lepture/editor). So you might be wondering, what's changed? Quite a bit actually. Here's some notable changes:
2015-06-22 20:25:42 +02:00
- Upgraded from CodeMirror 3 to CodeMirror 5
2015-07-20 18:51:58 +02:00
- Many changes to the style, appearance, and user friendliness
2015-06-22 20:25:42 +02:00
- Interface more closely resembles Bootstrap
- Now mobile friendly
2015-06-26 23:22:23 +02:00
- Option to autosave the text as you type
2015-07-21 06:26:47 +02:00
- Now spell checks what you write
2015-06-22 20:25:42 +02:00
- The text editor now automatically grows as you type more
- Fixed a large amount of bugs
- Switched to Font Awesome icons
- Improved preview rendering in many ways
- Improved as-you-type appearance of headers and code blocks
- Simplified the toolbar
2015-06-26 20:52:19 +02:00
- Many new options during instantiation
2015-07-20 18:51:58 +02:00
- New icons and tooltips
- Additional keyboard shortcuts