All Projects → bevacqua → Insane

bevacqua / Insane

Licence: mit
😾 Lean and configurable whitelist-oriented HTML sanitizer

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Insane

Phenomic
DEPRECATED. Please use Next.js instead.
Stars: ✭ 3,264 (+806.67%)
Mutual labels:  markdown
Prettydoc
Creating Pretty HTML From R Markdown
Stars: ✭ 338 (-6.11%)
Mutual labels:  markdown
Liandi
📕 一款桌面端的 Markdown 块级引用和双向链接笔记应用,支持 Windows、Mac 和 Linux。A desktop Markdown Block-Reference and Bidirectional-Link note-taking application, supports Windows, Mac and Linux.
Stars: ✭ 354 (-1.67%)
Mutual labels:  markdown
Notable
The Markdown-based note-taking app that doesn't suck.
Stars: ✭ 18,866 (+5140.56%)
Mutual labels:  markdown
Markdown Loader
markdown loader for webpack
Stars: ✭ 335 (-6.94%)
Mutual labels:  markdown
J
❌ Multi-format spreadsheet CLI (now merged in http://github.com/sheetjs/js-xlsx )
Stars: ✭ 343 (-4.72%)
Mutual labels:  markdown
Issues
Caret issues
Stars: ✭ 326 (-9.44%)
Mutual labels:  markdown
Emoji Cheat Sheet
A markdown version emoji cheat sheet
Stars: ✭ 5,957 (+1554.72%)
Mutual labels:  markdown
Pico
Pico is a stupidly simple, blazing fast, flat file CMS.
Stars: ✭ 3,494 (+870.56%)
Mutual labels:  markdown
Markdown Preview Plus
Markdown Preview + Community Features
Stars: ✭ 350 (-2.78%)
Mutual labels:  markdown
Stackedit
In-browser Markdown editor
Stars: ✭ 18,744 (+5106.67%)
Mutual labels:  markdown
Sublimetutor
An interactive in-editor keyboard shortcuts tutorial for Sublime Text 3
Stars: ✭ 336 (-6.67%)
Mutual labels:  markdown
Markup.rocks
Pandoc based document editor and converter in your browser.
Stars: ✭ 348 (-3.33%)
Mutual labels:  markdown
Docsify
🃏 A magical documentation site generator.
Stars: ✭ 19,310 (+5263.89%)
Mutual labels:  markdown
Githuber Md
Markdown editor plugin for WordPress.
Stars: ✭ 353 (-1.94%)
Mutual labels:  markdown
Md Writer
✒️ Make Atom a better Markdown/AsciiDoc editor for writers and bloggers
Stars: ✭ 326 (-9.44%)
Mutual labels:  markdown
Mdx Go
⚡️ Lightning fast MDX-based dev server for progressive documentation
Stars: ✭ 340 (-5.56%)
Mutual labels:  markdown
For Editor
for-editor - A markdown editor based on React
Stars: ✭ 358 (-0.56%)
Mutual labels:  markdown
Marky
A markdown editor built with Electron and React
Stars: ✭ 355 (-1.39%)
Mutual labels:  markdown
Marcdown
👻 Lightweight realtime markdown viewer and editor - Simple, clean and beautiful https://liyasthomas.github.io/marcdown
Stars: ✭ 345 (-4.17%)
Mutual labels:  markdown

insane

Lean and configurable whitelist-oriented HTML sanitizer

Works well in browsers, as its footprint size is very small (around ~2kb gzipped). API inspired by sanitize-html (which is around 100kb gzipped).

You would be insane not to use this!

Install

npm install insane --save

Usage

insane('<div>foo<span>bar</span></div>', { allowedTags: ['div'] })
// <- '<div>foo</div>'

Contrary to similar sanitizers, insane drops the whole tree of descendants for elements that aren't allowed tags.

API

insane(html, options?, strict?)

  • html can be an arbitrary HTML string
  • options are detailed below
  • strict means that options won't be based off of insane.defaults if set to true

The parser takes into account that some elements can be self-closing. For safety reasons the sanitizer will only accept a valid URL for background, base, cite, href, longdesc, src, and usemap elements. "Valid URL" means that it begins with either #, /, or any of options.allowedSchemes (followed by :).

options

Sensible defaults are provided. You can override specific options as needed.

allowedSchemes

Defaults to ['http', 'https', 'mailto'].

allowedTags

An array of tags that you'll allow in the resulting HTML.

Example

Only allow spans, discarding the rest of elements.

insane('<div>foo</div><span>bar</span>', {
  allowedTags: ['span']
});
// <- '<span>bar</span>'

allowedAttributes

An object describing the attributes you'll allow for each individual tag name.

Example

Only allow spans, and only allow those spans to have an id (discarding the rest of their attributes).

insane('<span id="bar" class="super">bar</span>', {
  allowedTags: ['span'],
  allowedAttributes: { span: ['id'] }
});
// <- '<span id="bar">bar</span>'

allowedClasses

If 'class' is listed as an allowed attribute, every single class will be allowed. If you don't list 'class' as an allowed attribute, you can provide a class whitelist per tag name.

Example

Only allow spans to have super or bad class names, discarding the rest of them.

insane('<span class="super mean and bad">bar</span>', {
  allowedTags: ['span'],
  allowedClasses: { span: ['super', 'bad'] }
});
// <- '<span class="super bad">bar</span>'

filter

Takes a function(token) that allows you to do additional validation beyond exact tag name and attribute matching. The token object passed to your filter contains the following properties.

  • tag is the lowercase tag name of the element
  • attrs is an object containing every attribute in the element, including those that may not be in the whitelist

If you return a falsy value the element and all of its descendants will not be included in the output. Note that you are allowed to change the attrs, and even add new ones, transforming the output.

Example

Require that <span> elements have an aria-label value.

function filter (token) {
  return token.tag !== 'span' || token.attrs['aria-label'];
}
insane('<span aria-label="a foo">foo</span><span>bar</span>', {
  allowedTags: ['span'],
  allowedAttributes: { span: ['aria-label'] },
  filter: filter
});
// <- '<span aria-label="a foo">foo</span>'

transformText

Takes a function(text) that allows you to modify text content in HTML elements. Runs for every piece of text content. The returned value is used instead of the original text contents.

Defaults

The default configuration is used if you don't provide any. This object is available at insane.defaults. You are free to manipulate the defaults themselves.

{
  "allowedAttributes": {
    "a": ["href", "name", "target"],
    "iframe": ["allowfullscreen", "frameborder", "src"],
    "img": ["src"]
  },
  "allowedClasses": {},
  "allowedSchemes": ["http", "https", "mailto"],
  "allowedTags": [
    "a", "article", "b", "blockquote", "br", "caption", "code", "del", "details", "div", "em",
    "h1", "h2", "h3", "h4", "h5", "h6", "hr", "i", "img", "ins", "kbd", "li", "main", "ol",
    "p", "pre", "section", "span", "strike", "strong", "sub", "summary", "sup", "table",
    "tbody", "td", "th", "thead", "tr", "u", "ul"
  ],
  "filter": null,
  "transformText": null
}

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].