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

mdn-kumascript

v0.0.5

Published

This package processes KumaScript macros in a source string.

Downloads

20

Readme

mdn-kumascript

This package processes KumaScript macros in a source string.

It includes modified versions of the KumaScript macros and KumaScript engine code in https://github.com/mdn/yari. This mdn/yari code is used under the MPL 2.0 license.

Usage

import { render } from "mdn-kumascript";

const source = `<p>The {{cssxref("margin")}} property and the {{experimental_inline}} note and also the {{cssref}} sidebar, and also the {{cssxref("border", "border syntax", "#syntax")}} property, and also the {{embedinteractiveexample("the-link")}}</p>`;
const environment = { frontMatter: {} };

const output = await render(source, environment);
console.log(output);

/*
{
  markup: '<p>The <a href="/Web/CSS/margin"><code>margin</code></a> property and the <abbr class="icon icon-experimental" title="Experimental. Expect behavior to change in the future."><span class="visually-hidden">Experimental</span></abbr> note and also the  sidebar, and also the <a href="/Web/CSS/border#syntax"><code>border syntax</code></a> property, and also the </p>',
  frontMatter: {
    'interactive-example': 'https://interactive-examples.mdn.mozilla.net/the-link'
  },
  errors: []
}
*/

API

This package exports a single asynchronous function render(), which takes two arguments:

  • source: a string that may contain KumaScript macros
  • environment: an object with a single property frontMatter, which is an object containing the page front matter. At the moment, in fact, the front matter is not used as an input parameter, but some macros may need it later.

The function returns a Promise that resolves to an object with three properties:

  • markup: a string representing the original source, but with macros expanded into HTML.
  • frontMatter: the same front matter object that was passed in, with any modifications made by macros
  • errors: an array of nonfatal errors encountered during macro processing.

Internals

This package is really in two parts:

  • the engine, that finds macro calls, arranges for the right macro to be called with the right arguments, and assembles the result.
  • the individual macros

Engine

The engine is ported from https://github.com/mdn/yari. In particular:

  • parser.js, which parses the source to find macro calls and their arguments, is unchanged
  • errors.ts, which implements error messages, is slightly modified to remove errors that seem to depend on the website
  • render.ts, which orchestrates the invocation of macros for a given source, is modified top remove some features we don't need. This module implements the top-level render() function, so is the entry point to the module.
  • templates.ts is mostly rewritten, as it forms the interface between the engine and individual macros, so the Yari version is highly dependent on the EJS implementation used in Yari.

Macros

In this package, individual macros are implements as separate modules under the /macros directory. Each module must export an async function executeMacro(), which takes two arguments:

  • args: an array containing the arguments that were passed to the macro in the source invocation
  • env: an object containing a single property frontMatter, which is an object containing the page front matter (It's quite likely that some macros will eventually need more environment than this, so we will probably extend this env object).

The executeMacro() function returns a Promise that resolves to the expansion of the macro in the source. Macros may also modify the frontMatter argument: if they do, these modifications will appear in the object returned by the top level render() function.

After implementing a macro module, you make it available to the engine by adding its executeMacro() function to the renderers map in the renderers.js module.

Testing

Tests are provided for both the framework and for individual macros. To run tests:

npm run test