npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

markdown-wysiwyg

v1.0.9

Published

Markdown WYSIWYG editor (returns markdown code)

Downloads

12

Readme

Description

Markdown-WYSIWYG is an HTML5 custom element that allows you to easily integrate a WYSIWYG editor to a webpage that returns the MarkDown code of the content rather than the HTML output.

The benefit of this approach is that you can directly save the Markdown code to your database which is cleaner than saving the HTML.
Also, by using a Markdown parser, you can be sure to only output valid and safe HTML code to your clients.
It saves you the hurdle of having to whitelist HTML tags and attributes where it is very easy to fail and expose yourself to XSS attack.
Moreover, by limiting the formatting the user can use, you have better control over the styling of the content.

It is recommended to use MarkDown-It for rendering the Markdown to HTML across your website, since it is what is used in the WYSIWYG editor.

How to install

npm install markdown-wysiwyg

How it works

It uses HTML5's custom elements to work seamlessly. It depends on TurnDown and MarkDown-It to convert HTML->Markdown and Markdown->HTML respectively. It supports basic MarkDown:

  • Bold/Italic/StrikeThrough
  • Headings
  • Images
  • Links
  • Lists (ul/ol)
  • Tables
  • Horizontal Rules
  • Blockquotes
  • Code (inline/block)

The editor itself is pretty simple and straightforward to use. It cleans the HTML code to avoid.

<markdown-wysiwyg>
# Basic stuff
**Bold** _Italic_ ~~StrikeThrough~~
## Heading 2
### Heading 3
#### Heading 4
##### Heading 5
###### Heading 6

# Lists
Bullet lists:
* Bullet point 1
* Bullet point 2

And numbered lists:
1. Numbered item 1
1. Numbered item 2
1. Numbered item 3

# Links
[Markdown-WYSIWYG](https://github.com/ybouane/markdown-wysiwyg)

# Images
![Google Logo](https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png)

# Tables
| Col 1 | Col 2 | Col 3 |
| --- | --- | --- |
| A | B | C |
| D | E | F |

# Horizontal Rules
* * *

# Blockquotes
> Quote someone deep

# Code
You can put `inline code`, but you can also put:
```
console.log('Block Code')
```
</markdown-wysiwyg>

You can initialize the field by typing (make sure to HTML escape) the markdown code in the tag. You can further modify the code and retrieve the output using the .value attribute:

console.log(document.querySelector('markdown-wysiwyg').value);// Logs the current markdown value

Image upload

By default, the user can type the image url when inserting and image. But you can also allow the user to upload an image. For that, you need to add an onImageUpload property on the HTML element. It needs to run asynchronously and return the URL when the upload has finished. It can also fail and throw an error:

document.querySelector('markdown-wysiwyg').onImageUpload = (file) => {
	return new Promise((resolve, reject) => {
		// Do your upload logic here and call resolve with the image url when the upload finishes
		setTimeout(()=>res(file.name), 2000);
	})
};

Customizations

You can customize the styling of the wysiwyg editor as follows:

markdown-wysiwyg {
	height:500px; /* fixed height */
	--primary-color:#d35400;
	--primary-color-contrast:#FFFFFF;
}
markdown-wysiwyg::part(wrapper) {
	box-shadow:none;
	border:1px solid #d35400;
}
markdown-wysiwyg::part(buttons) {
	background:#d35400;
	--buttons-filter:invert(1);
	--buttons-bg-highlight-color:rgba(255, 255, 255, 0.3);
}

Known issues

Turndown does a great job at converting Markdown to HTML, but there are edge cases where it might fail. This component tries to clean the HTML code beforehand to make it easier for Turndown. Some known issues:

  • Doing in-word bold/italic/strikethrough formatting sometimes fails
  • Multiple line breaks sometimes get removed