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

astro-mdx-code-blocks

v0.0.6

Published

An easy way to customize the syntax highlighting of MDX fenced code blocks by providing your own Astro component.

Downloads

117

Readme

🚀 Astro MDX Code Blocks

Use a custom Astro component to render and syntax highlight code snippets in your MDX files.

Demo

View a demo of the integration in action on StackBlitz.

Installation

Due to the extra configuration needed, you must manually install this integration.

This integration depends on the AutoImport and @astrojs/mdx integrations.

npm install -D astro-mdx-code-blocks astro-auto-import @astrojs/mdx

Code Block Component

Create a component in your project that will be used to render fenced code blocks.

Props

The code block component provided to the integration receives the following props.

| Prop | Type | Optional | Description | | ---- | ---- | -------- | ------------| | code | String | No | The raw contents of the fenced code block from the .mdx file. | lang | String | Yes | The language detected from the fenced code block. | | filename | String | Optional | If a // filename.ts is provided at the top of the code block it will be removed and sent in in the filename prop. |

In addition, you can export the following type definition from the integration.

type CodeBlockProps = {
    code: string;
    lang?: string;
    filename?: string;
};
import type { CodeBlockProps } from 'astro-mdx-code-blocks';

Example Component

This example uses Astro's Prism component for syntax highlighting. However, you can use any library you'd like as the component has access to the raw code string.

src/components/CodeBlock.astro

---
import { Prism } from '@astrojs/prism';

const {
    code,
    lang,
    filename,
} = Astro.props;

const hasLang = !!lang;
const hasFileName = !!filename;

const showHeader = hasLang || hasFileName;
---

<figure class="code-block">

    {showHeader && (
        <figcaption class="header">
            {hasFileName && (
                <span class="filename">
                    {filename}
                </span>
            )}
            {hasLang && (
                <span class="lang">
                    {lang}
                </span>
            )}
        </figcaption>
    )}

    <Prism
        code={code}
        lang={lang}
    />

</figure>

Configuration

  • Import the AutoImport and mdx integrations.
  • Import MDXCodeBlocks and mdxCodeBlockAutoImport from astro-mdx-code-blocks.
  • Add AutoImport, MDXCodeBlocks, and mdx to the integrations config option.
  • Use the mdxCodeBlockAutoImport function to provide the AutoImport integration the path to your custom Astro component.
import { defineConfig } from 'astro/config';

import AutoImport from 'astro-auto-import';
import mdx from '@astrojs/mdx';

import MDXCodeBlocks, { mdxCodeBlockAutoImport } from 'astro-mdx-code-blocks';

export default defineConfig({
    // ...
    integrations: [
         AutoImport({
            imports: [
                mdxCodeBlockAutoImport('src/components/CodeBlock.astro')
            ],
        }),
        MDXCodeBlocks(),
        mdx(),
    ],
});

AutoImport must come before MDXCodeBlocks and MDXCodeBlocks must come before mdx.

Usage

Use fenced code blocks in your MDX files as you normally would. As noted above, the integration will pull out certain metadata from the block and provide it to your custom Astro component.

Contributing

Issues and pull requests are welcome.

License

MIT