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

preact-markup

v2.1.1

Published

Render HTML/XML using VDOM, with Custom Elements mapped to Components.

Downloads

59,657

Readme

preact-markup NPM travis-ci

A <Markup> component that renders HTML (or XML) using Virtual DOM, mapping a set of element names to Components. Works beautifully with Preact.

Think of this like an HTML5 renderer where Web Components are implemented as Preact Components.

:zap: JSFiddle Demo :zap:

Use Cases

  • Rendering Markdown in VDOM - see preact-markdown
  • Component-base app design and/or layout via HTML
  • Define app structure using a standard HTML CMS
  • Support arbitrary component extensions by allowing safe HTML
  • Build using Custom Elements, implemented using React's API

Overview

The <Markup /> component takes some markup, an optional mapping of custom element names to components, and an optional type of either xml or html.

In it's simplest form, <Markup /> is just a diffing XML/HTML renderer. It only re-renders when you change the markup prop.

import Markup from 'preact-markup';

let html = `<h1>hello</h1> <p>Testing 1 2 3...</p>`;
render(<Markup markup={html} />, document.body);

Note: by default, content is parsed as XML, which may be too strict for your content but is the fastest option. Pass type="html" to parse as HTML.

Custom Elements via Components

The real value of <Markup /> is seen when passing a components prop. This prop is an Object that lets us map any HTML/XML element name to a preact Component. The mapped component is injected and rendered as if it had been referenced from within JSX. HTML attributes defined on the custom element in markup get passed to the mapped Component as props.

import Markup from 'preact-markup';

const Sidebar = ({ title, children }) => (
	<aside class="sidebar">
		<h2>{ title }</h2>
		{ children }
	</aside>
);

let html = `
<h1>Hello, World</h1>
<sidebar title="My Sidebar!">
	<p>Sidebar contents.</p>
</sidebar>
`;
render(<Markup markup={html} components={{ Sidebar }} />, document.body);

When render() is invoked, Our <Sidebar /> component is substituted for the <sidebar> element, which means it gets mounted and rendered like a normal Preact Component. The result is this HTML DOM:

<div class="markup">
	<h1>Hello, World</h1>
	<aside class="sidebar">
		<h2>My Sidebar!</h2>
		<p>Sidebar contents.</p>
	</aside>
</div>

Subsequent render()s diff against that DOM just like a normal JSX rendering flow would.

Optional properties

type - By default, content is parsed as XML. Pass type="html" to use an HTML parser.

trim - Trimming tries to emulate HTML semantics by default, but these differ from JSX semantics. Pass false to retain all whitespace, or all to omit all whitespace.

onError - Suppress XML/HTML parse errors and instead pass them to this function.

allow-scripts - By default, preact-markup sanitizes the rendered HTML by removing script tags. The allow-scripts property re-enables script tags, executing any JavaScript code within them.

Example
let markup = `<em>hello!</em><h1>asdflkj</h1><script>alert("Hello world");</script>`;
render(<Markup markup={markup} allow-scripts />, document.body);

License

MIT