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

@codemovie/code-movie-html

v1.1.0

Published

Use HTML as a DSL for Code.Movie

Downloads

176

Readme

@codemovie/code-movie-html - Use HTML as a DSL for Code.Movie animations

Turn DOM (real DOM nodes as well as jsdom fake DOM nodes) elements into frame objects:

<div class="animation">
<pre class="frame"><code>// How to add two numbers in JS<code></pre>
<pre class="frame"><code>// How to add two numbers in JS
function add() {}</code></pre>
<pre class="frame">// How to add two numbers in JS
function add(a, b) {}</code></pre>
<pre class="frame"><code>// How to add two numbers in JS
function add(a, b) {
  return a + b;
}</code></pre>
<pre class="frame"><code>// How to add two numbers in JS
function add(a, b) {
  return a + b;
}

console.log(add(1, 2)); // > 3</code></pre>
</div>

<script type="module">
  // Turn the contents of <code> elements nested within <pre class="frame">
  // into frame objects. This is what this library is all about.
  import { framesFromDom } from "@codemovie/code-movie-html";
  const frameElements = document.querySelectorAll(".animation pre.frame", "code");
  const frames = framesFromDom(frameElements)

  // Use regular @codemovie/code-movie functionality to turn the frames into
  // animation HTML and CSS
  import { animateHTML } from "@codemovie/code-movie";
  import ecmascript from "@codemovie/code-movie/languages/ecmascript";
  const html = animateHTML(frames, { tabSize: 2, language: ecmascript() });

  // Use @code-movie/code-movie-runtime to add a basic UI to the animation and
  import from "@codemovie/code-movie-runtime";
  const runtime = document.createElement("code-movie-runtime");
  runtime.innerHTML = html;
  runtime.controls = true;
  runtime.keyframes = Object.keys(frames);
  document.body.append(runtime);

  // Hide the source elements
  document.querySelector(".animation").hidden = true;
</script>

Result:

An animated code sample in a loop

For an interactive example, install this package, run npm run demo and open http://localhost:3000/demo/index.html in a non-ancient web browser.

Getting started

Install this library via your favorite package manager:

npm install @codemovie/code-movie-html

You will almost certainly also want to install @codemovie/code-movie to turn the frames into an animation and @codemovie/code-movie-runtime might also be useful. If you want to work server-side or at compile-time, install jsdom as well.

Function framesFromDom

The package exports a single function that you can point to some DOM with an optional selector to target nested elements:

function framesFromDom(containerElements: Iterable<Element>, sourceSelector?: string, windowObject?: Window & typeof globalThis): InputFrame[];

Takes an iterable of elements (things like Arrays, NodeLists and the like) and turns it into an array of input frames compatible with @codemovie/code-movie. If a non-falsy value is provided for sourceSelector, the function will take its content from the first matching descendant of each source element; otherwise the source elements themselves serve as sources.

If your program's global object is not the the window object, you can pass the window object as a third argument. This is useful for work with non-browser DOM environments like JSON:

import { framesFromDom } from "@codemovie/code-movie-html";
import jsdom from "jsdom";

const dom = new jsdom.JSDOM(`<!DOCTYPE html><p>[]</p><p>[42]</p><p>[23, 42]</p>`);
const actual = framesFromDom(
  // List of sources
  dom.window.document.querySelectorAll("p"),
  // Source selector (none; the <p> elements already contain the content)
  "",
  // Reference to JSDOM's window object
  dom.window
);

HTML DSL specifics

The library currently only extracts the text content from elements and their children. More advanced features are on their way.