ponymark
v3.3.4
Published
Next-generation PageDown fork
Downloads
18
Readme
ponymark
Next-generation
PageDown
fork
Features
- Super Modular!
- No libraries or frameworks
- Clean CSS styling using prefixes
- GitHub Flavored Markdown
- Syntax Highlighting through highlight.js
- Image drag and drop
- Default image uploading endpoint for the server-side
- Support for imgur and local file uploads
- MIT licensed
Install
npm i -S ponymark
bower i -S ponymark
Then you have to provide it with a function that can parse Markdown. This can be a function you wrote by yourself or a reference to a module such as a ultramarked.
ponymark.configure({
markdown: function (text) {
return parse(text);
}
});
ponymark.configure({
markdown: require('ultramarked')
});
Usage
Just include the JavaScript and call the ponymark
method on a <textarea>
element. You'll get a button bar, the editor, and a preview area just like the one in StackOverflow. Remember to include the CSS to get the button bar working correctly. You can include the Stylus sources directly. The syntax highlighting styles come bundled separately, so that you can pick any other you want.
Test syntax highlighting themes here, then download them here. Just include the style files as is, and you'll be fine.
var elem = document.querySelector('textarea');
ponymark(elem);
You can also specify different containers for the editor and the HTML preview. This is useful when you have multiple inputs and then want the preview to be placed somewhere else in the DOM.
ponymark({
textarea: document.querySelector('textarea'),
preview: document.querySelector('footer')
});
Image Uploads
Ponymark supports image uploading through your site. Simply configure it before invoking ponymark
.
ponymark.configure({
imageUploads: '/api/v1/images'
});
You can specify a method by passing an object instead. The default method used is PUT
, and the default FormData
key used to upload the image is 'image'
.
ponymark.configure({
imageUploads: {
method: 'POST',
url: '/api/v0/images',
key: 'imgur'
}
});
Server-Side
Ponymark will send requests to the specified HTTP resource with the user image. As for the server-side, a helper is provided, and you can use it as demonstrated below with your favorite web back-end of choice. Here's an example using express@^4.1.2
.
'use strict';
var path = require('path');
var ponymark = require('ponymark');
var dir = path.resolve('./temp/images');
module.exports = function (req, res, next) {
var options = {
// note that the key should match imageUploads.key, see above
image: req.files && req.files.image,
imgur: process.env.IMGUR_API_KEY,
local: dir
};
ponymark.imageUpload(options, uploaded);
function uploaded (err, result) {
if (err) {
errored(err.message); return;
}
res.status(200).json(result);
}
function errored (message) {
res.status(400).json({ messages: [message] });
}
};
If an API key to imgur isn't provided, then the local file system will be used. That is super unreliable in production environments, so either provide the API key, or implement your own endpoint. The local file system functionality is entirely disabled when process.env.NODE_ENV === 'production'
. The local
option specifies the directory where files will be uploaded to, and the localUrl
method is provided an absolute file path, and expects you to return the URL you'll be using to serve that file from. That url will only be used for the response.
The HTTP endpoint is expected to return a JSON response like the one below. This is the type of response returned in result
if the upload doesn't fail.
{
"url": "http://i.imgur.com/cC3fCEN.jpg",
"alt": "doge.png"
}
Screenshot
License
MIT