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

remark-d2

v0.2.3

Published

A remark plugin for compiling d2 code blocks in markdown

Downloads

14

Readme

remark-d2

License: MIT

A remark plugin that turns d2 code blocks into diagrams in Markdown files.

https://github.com/mech-a/remark-d2/assets/11798509/e6c6276f-8839-46af-b29e-243f31cdce24

Features

Fast resizing and titling: metadata tags like width, position, and title are compiled:

      ┎─────── abbrev. ───────┒
```d2 w=150px;position=center;a="test"
d2->is->fast
```

becomes:

<img alt="test" position="center" width="150px" src="/d2/docs/features/0.svg" />
  • width, w
  • height, h
  • position, p
  • title, t
  • alt, a

Change default d2 compile settings and default image attributes easily

const output = await remark()
  .use(remarkD2, {
    defaultD2Opts: ["-t 101", "--dark-theme 200"],
    //            "Orange Creamsicle" on light mode,
    //               "Dark Mauve" on dark mode
    defaultImageAttrs: {
      title: "Wow, colors!",
      alt: "Colorful Diagram",
      width: "700px",
    },
  })

  .processSync(file);

gets you

Try changing color modes and see!

Installation

Ensure that you have d2 installed and accessible on your PATH. Then, install using your favorite package manager:

npm install remark-d2

Usage

In your unified or remark toolchain, you can include the plugin. Note that the VFile passed in must have a path.

import { remark } from "remark";
import remarkD2 from "remark-d2";
import { VFile } from "vfile";
import { readFileSync } from "node:fs";

const file = new VFile({
  path: "docs/intro.md",
  value: readFileSync("docs/intro.md"),
});

const output = await remark().use(remarkD2).processSync(file);

console.log(output.toString());

Integrations

remark-d2 is compatible with Docusaurus out of the box. However, as Docusaurus does not currently support ES Modules, to install remark-d2 follow the workaround mentioned in the Docusaurus docs:

// in `docusaurus.config.js`

// wrap your config in a function:
async function createConfig() {
  // import the plugin here
  const d2 = (await import("remark-d2")).default;
  // return your config
  return {
    title: ...,
    tagline: ...,
    presets: [ // install here or in plugin config
      [
        '@docusaurus/preset-classic', // or any other preset
        {
          docs: {
            remarkPlugins; [d2], // works out of the box
          },
        },
      ],
    ],
  }
};

// export the function (will be automatically called)
module.exports = createConfig

Configuration

Options can be passed in as a parameter in .use. They should be in an Object form.

  • compilePath
    • Relative path where d2 diagrams are exported in respective folders
    • default: static/d2, compatible with Docusaurus
  • ext
    • File extension for d2 diagrams: currently only svg or png
      • Note that png is slower and does not support automatic light/dark mode
    • default: svg
  • linkPath
    • Path prepended to the relative file path in the image URL
      • Useful if your build system removes the parent folder reference assets/ or static/
    • default: /d2, compatible with Docusaurus
  • defaultD2Opts
    • Options passed to d2 CLI. See man d2 for more.
    • default: ["-t=100", "--dark-theme=200"]
  • htmlImage
    • If true, replace all code blocks with HTML <img /> tags instead of Markdown images
      • If a code block has metadata, regardless of htmlImage's value, it will be an HTML image
    • default: false
  • defaultImageAttrs
    • Default attributes for images
      • Only the keys title and alt apply to both Markdown and HTML images. All other attributes are only used if it is an HTML image.
    • default: { alt: "d2 diagram" }

Inspiration

remark-simple-plantuml