Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

transformime

Package Overview
Dependencies
Maintainers
2
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

transformime

Transforms MIMEtypes to HTML Elements

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
205
increased by318.37%
Maintainers
2
Weekly downloads
 
Created
Source

transformime

Build Status

Optimus MIME

Transforms MIMEtype+data to HTML Elements

Intended to be used in context of Jupyter and IPython projects, particularly by display areas.

Installation

npm install transformime

Usage

Transformime works in the browser (via browserify) and with jsdom!

It returns promises for all the HTMLElements.

Transform a single mimetype

> // In node we'll need a DOM to work with
> var document = require('jsdom').jsdom();
> // For browsers, they can pass document around (or an iframe's contentDocument)
> var Transformime = require('transformime').Transformime;
> var transformer = new Transformime();
> var p1 = transformer.transform("<h1>Woo</h1>", "text/html", document);
> p1.then(function(el){
...   console.log(el.innerHTML);
...   console.log(el.textContent)
... });
<h1>Woo</h1>
Woo

Images get handled as base64 encoded data and become embedded elements.

> // Send an image over
> p2 = transformer.transform("R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7", "image/png", document)
> p2.then(function(el){
...     console.log(el.src);
... })
data:image/png;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7

Transform the richest element

> var mimes = {'text/html': "<code>import this</code>", 'text/plain': "import this"}
> var p3 = transformer.transformRichest(mimes, document);
> p3.then(function(bundle){
...    console.log(bundle.mimetype + ": " + bundle.el.innerHTML)
... })
text/html: <code>import this</code>

Transform all elements

> var mimes = {'text/html': "<code>import this</code>", 'text/plain': "import this"}
> var p4 = transformer.transformAll(mimes, document);
> p4.then(function(arr) {
...   arr.forEach(function(bundle) {
...     console.log(bundle.mimetype + ": " + bundle.el.innerHTML);
...   });
... });
text/html: <code>import this</code>
text/plain: import this

Working with iframes

> // Assume document is defined (either in browser or from jsdom) as well as transformer
> // Create an arbitrary iframe and slap it in the body of the document
> var iframe = document.createElement("iframe");
> document.querySelector('body').appendChild(iframe);
> var idoc = iframe.contentDocument;
> var p5 = transformer.transform('<h1>mimetic</h1>', "text/html", idoc);
> p5.then(function(el){
... idoc.querySelector('body').appendChild(el);
... })
> idoc.querySelector('body').innerHTML
'<div><h1>mimetic</h1></div>'

Development

git clone https://github.com/nteract/transformime
cd transformime
npm install
npm run build

Keywords

FAQs

Package last updated on 26 Jul 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc