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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@mdjs/core

v0.21.1

Published

Combine Markdown with JavaScript

Downloads

17,347

Readme

/* START - Rocket auto generated - do not touch */
export const sourceRelativeFilePath = '30--tools/20--markdown-javascript/10--overview.rocket.md';
// prettier-ignore
import { html, layout, setupUnifiedPlugins, components, openGraphLayout } from '../../recursive.data.js';
export { html, layout, setupUnifiedPlugins, components, openGraphLayout };
export async function registerCustomElements() {
  // server-only components
  // prettier-ignore
  customElements.define('rocket-social-link', await import('@rocket/components/social-link.js').then(m => m.RocketSocialLink));
  // prettier-ignore
  customElements.define('rocket-header', await import('@rocket/components/header.js').then(m => m.RocketHeader));
  // prettier-ignore
  customElements.define('rocket-main-docs', await import('@rocket/components/main-docs.js').then(m => m.RocketMainDocs));
  // prettier-ignore
  customElements.define('rocket-content-area', await import('@rocket/components/content-area.js').then(m => m.RocketContentArea));
  // hydrate-able components
  // prettier-ignore
  customElements.define('rocket-search', await import('@rocket/search/search.js').then(m => m.RocketSearch));
  // prettier-ignore
  customElements.define('rocket-drawer', await import('@rocket/components/drawer.js').then(m => m.RocketDrawer));
}
export const needsLoader = true;
/* END - Rocket auto generated - do not touch */

export const title = 'Markdown JavaScript';
export const subTitle = 'Executable JavaScript in markdown by annotating code blocks';

Overview

import '@mdjs/mdjs-story/define';
import '@mdjs/mdjs-preview/define';
import { html } from '@mdjs/mdjs-story';

Markdown JavaScript (mdjs) is a format that allows you to use JavaScript with Markdown, to create interactive demos. It does so by "annotating" JavaScript that should be executed in Markdown.

To annotate we use a code block with js client.

```js client
// execute me
```

Web Components

One very good use case for that can be web components. HTML already works in Markdown so all you need is to load a web components definition file.

You could even do so within the same Markdown file.

## This is my-card

Here's an example of the component:

```html preview-story
<my-card>
  <h2>Hello world!</h2>
  <button>Click me!</button>
</my-card>
```

You can even execute some JavaScript:

## This is my-el

<my-el></my-el>

```js client
import { LitElement, html } from 'https://unpkg.com/lit-element?module';

class MyEl extends LitElement {
  render() {
    this.innerHTML = '<p style="color: red">I am alive</p>';
  }
}

customElements.define('my-el', MyEl);
```

Demo Support (Story)

mdjs comes with some additional helpers you can choose to import:

```js client
import '@mdjs/mdjs-story/define';
import '@mdjs/mdjs-preview/define';
```

Once loaded you can use them like so:

Story

The code snippet will actually get executed at that place and you will have a live demo

```js story
export const JsStory = () => html` <demo-wc-card>JS Story</demo-wc-card> `;
```
```html story
<demo-wc-card>HTML Story</demo-wc-card>
```

Full Code Support

```js story
export const JsStory = () => {
  const calculateSomething = 12;
  return html`
    <demo-wc-card .header="\\${`Something: \\${calculateSomething}`}">JS Story</demo-wc-card>
  `;
};
```

Preview Story

Will become a live demo wrapped in a container with a show code button.

```js preview-story
export const JsPreviewStory = () => html` <demo-wc-card>JS Preview Story</demo-wc-card> `;
```
```html preview-story
<demo-wc-card>HTML Preview Story</demo-wc-card>
```

Here is a live example from demo-wc-card.

import 'demo-wc-card/demo-wc-card.js';
export const header = () => {
  return html` <demo-wc-card .header=${'my new header'}></demo-wc-card> `;
};
// not defined for android
// not defined for ios

Story Code

If your preview is followed by a code blocks marked as story-code then those will be shown when switching between multiple platforms

```js preview-story
// will be visible when platform web is selected
export const JsPreviewStory = () => html` <demo-wc-card>JS Preview Story</demo-wc-card> `;
```

```xml story-code
<!-- will be visible when platform android is selected -->
<Button
    android:id="@+id/demoWcCard"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Android Code"
    style="@style/Widget.FooComponents.Demo.Wc.Card"
/>
```

```swift story-code
// will be visible when platform ios is selected
import DemoWc.Card

let card = DemoWcButton()
```

See it in action by opening up the code block and switching platforms

// will be visible when platform web is selected
export const JsPreviewStory = () => html` <demo-wc-card>JS Preview Story</demo-wc-card> `;
<!-- will be visible when platform android is selected -->
<Button
    android:id="@+id/demoWcCard"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Android Code"
    style="@style/Widget.FooComponents.Demo.Wc.Card"
/>
// will be visible when platform ios is selected
import DemoWc.Card

let card = DemoWcButton()

Supported Systems

Storybook

Please check out @open-wc/demoing-storybook for a fully integrated setup.

It includes storybook-addon-markdown-docs which uses mdjs under the hood.

Chrome Extension (currently only for GitHub)

See live demos directly inside GitHub pages, Markdown files, issues, pull requests...

Please check out mdjs-viewer.

Build mdjs

Basic

mdjs offers two more "basic" integrations

mdjsDocPage

Creates a full blown HTML page by passing in the Markdown.

const { mdjsDocPage } = require('@mdjs/core');

const page = await mdjsDocPage(markdownString);
/*
<html>
  ... // load styles/components for mdjs, start stories
  <body>
    <h1>Some Markdown</h1>
  </body>
</html>
*/

mdjsProcess

Pass in multiple Markdown documents and you get back all the JavaScript code and rendered HTML.

const { mdjsProcess } = require('@mdjs/core');

const data = await mdjsProcess(markdownString);
console.log(data);
/*
{
  jsCode: "
    import '@mdjs/mdjs-story/mdjs-story.js';
    ...
  ",
  html: '<h1>Markdown One</h1>',
}
*/

Advanced

mdjs is build to be integrated within the unifiedjs system.

const unified = require('unified');
const markdown = require('remark-parse');
const htmlStringify = require('remark-html');
const mdjsParse = require('@mdjs/core');

const parser = unified().use(markdown).use(mdjsParse).use(htmlStringify);
const result = await parser.process(body);
const { jsCode } = result.data;
console.log(result.contents);
// <h1>This is my-el></h1>
// <my-el></my-el>
console.log(jsCode);
// customElements.define('my-el', class extends HTMLElement {
// ...