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

v2.0.0

Published

Use HTML as a DSL for Code.Movie

Downloads

155

Readme

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

Turn DOM 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 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 additional (optional) options:

function framesFromDom(
  containerElements: Iterable<Element>,
  options?: Options
): InputFrame[];

The function 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. The options object has the following shape:

type Options = {
  windowObject?: Window & typeof globalThis; // defaults to window
  decorationsSelector?: string; // defaults to "mark"
};
  • windowObject points to the window object in case your program's global object is not the the window object. This is useful for work with non-browser DOM environments like jsdom. Defaults to window, which works for use in web browsers.
  • decorationsSelector is the selector for decorations (see below). Defaults to the selector "mark" to target <mark> elements.

The following example shows how the library can be used with jsdom and a custom decorations selector:

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"),
  // Optional options
  {
    // Reference to JSDOM's window object
    windowObject: dom.window,
    // Only target <mark> elements with the class `foo` as decorations
    decorationsSelector: "mark.foo",
  }
);

HTML DSL specifics

The library currently does two things:

  1. it extracts the text content from elements and their children to serve as the frame's content
  2. it turns <mark> elements into decorations, with different decoration types denoted by the class attribute

Even more features are underway and will become available once the core library supports them.

Text extraction

The library extracts the text contents from its target elements and their descendants. The tags and attributes of target element's descendants are usually ignored:

<div class="target">Hello <b>World</p></div>

This markup will result in a frame containing the text Hello World, with the <b> element contributing nothing but its contents.

The only exception to this rule are elements that match the selector passed to the options for framesFromDom() (<mark> elements by default). These elements get turned into decorations.

Decorations

Decorations in Code.Movie can highlight lines, underline errors or place icons the gutter. Elements that match the selector passed to the options for framesFromDom() (<mark> elements by default) processed as decorations according to their class attributes:

| Element's class name contains | Resulting decoration | | ----------------------------- | ---------------------------------------------------------------- | | gutter | Gutter decoration for the line the element is in | | line | Line decoration for all lines that the element is part of | | None of the above | Text decoration for the characters within the element's tags |

The class attribute can contain other strings, but whether or not they contain gutter or line determines whether the decoration ends up as a text, line or gutter decoration. The element's tag name and attributes are used to populate the decoration object's data fields:

// Given <p>[]</p><p>[<mark class="foo">42</mark>]</p><p>[<mark class="foo">23</mark>, 42]</p>
const frames = framesFromDom(document.querySelectorAll("p"));
// Result:
// [
//   {
//     code: "[]",
//     decorations: [],
//   },
//   {
//     code: "[42]",
//     decorations: [
//       {
//         kind: "TEXT",
//         data: {
//           tagName: "mark",
//           class: "foo",
//         },
//         from: 1,
//         to: 3,
//       },
//     ],
//   },
//   {
//     code: "[23, 42]",
//     decorations: [
//       {
//         kind: "TEXT",
//         data: {
//           tagName: "mark",
//           class: "foo",
//         },
//         from: 1,
//         to: 3,
//       },
//     ],
//   },
// ];