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

ember-whatbars

v0.2.3

Published

A simple template compiler compatible with a subset of HTMLBars.

Downloads

20

Readme

Ember-whatbars

Build Status

This addon renders Ember components embedded in mutable text content with an HTMLBars-like syntax. The primary use case is to provide an Ember-enabled markup language to untrusted users.

TL;DR Example

Suppose that userProvidedContent is

Donald Trump and raccoons both
 * have small hands and
 * look like thieves.

{{youtube-video v='bQueaSlvjCw'}}

WhatBars allows safely rendering this content along with the Ember component:

{{what-bars userProvidedContent enabledComponents sanitizerSuchAsMarkdownIt}}

Motivation

We do not allow users to write raw HTML content in our websites for two reasons:

  • Untrusted users may stage a cross-site scripting (XSS) attack.
  • Even trusted users don't want to write HTML.

Addressing the first bullet point is as simple as sanitizing user input. A popular solution for the second bullet point is to format user content with Markdown. These methods are employeed right here in this README.md.

Sanitizers and markup languages such as Markdown often limit our users more than we want. We can use plugins to add some functionality. For example, markdown-it-video provides a syntax for videos from popular sites. Integrating a markdown-it plugin with Ember (Data) may prove to be difficult, though.

If we trust our user content, we could run the HTMLBars template compiler on the client side:

import Ember from 'ember';

export default Ember.Component.extend({
  layout: Ember.computed('template', function () {
    return Ember.HTMLBars.compile(this.get('template'));
  }),
});

However, this requires adding approximately a megabyte (~200k gzipped) to our application's size by including ember-template-compiler.js in ember-cli-build.js:

  app.import('bower_components/ember/ember-template-compiler.js');

Plus, that method does not allow the templates to change after rendering them.

WhatBars allows untrusted content to use HTMLBars-like syntax to include Ember components without requiring ember-template-compiler.js.

Usage

The what-bars component renders the enabled components that are embeded in the provided content and runs the provided sanitizer on the rest of the content. Only components that are explicitly enabled will be rendered, and the sanitizer function must return an Ember.SafeString.

{{what-bars content enabled sanitizer}}

Basic Example

For example, you might have a user-contribution component:

import Ember from 'ember';
import makeShinyAndSafe from 'whatever-you-want-to-use';

export default Ember.Component.extend({
  classNames: ['user-contribution'],
  enabled:  ['youtube-video', 'jazz-hands-component'],
  content: "",
  sanitizer(contentFragment) {
    const html = makeShinyAndSafe(contentFragment);
    return Ember.String.htmlSafe(html);
  }
});

Then simply use what-bars in the user-contribution template:

{{what-bars content enabled sanitizer class='user-content'}}

Note that enabled may be an array of component names, or an object with component names as keys:

  enabled: {'youtube-video': true, 'jazz-hands-component': true},

The makeShinyAndSafe function could be something like:

import markdownit from 'markdown-it';

function makeShinyAndSafe(str) {
  const md = markdownit({ /* markdown-it options here */ });
  return md.render(str || "");
}

The named arguments are provided to the component as params, the positional arguments are provided as positional, and the block content is provided as block. (This could change when Ember gets splat.) A possible implementation of the youtube-video component is:

import Ember from 'ember';

export default Ember.Component.extend({
  classNames: ['youtube-video'],
  params: {},
  positional: [],
  block: '',
  v: Ember.computed('params', 'positional', function() {
    return this.get('params').v || this.get('positional')[0];
  }),
});

And the corresponding template might be:

<iframe src="https://www.youtube.com/embed/{{v}}" frameborder="0"></iframe>

This addon is under development and its interface may still change.

Installation

  • git clone https://github.com/w-hat/ember-whatbars
  • cd ember-whatbars
  • npm install
  • bower install

Running

Running Tests

  • npm test (Runs ember try:each to test your addon against multiple Ember versions)
  • ember test
  • ember test --server

Building

  • ember build

For more information on using ember-cli, visit https://ember-cli.com/.