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

@saschazar/mdx-extended-loader

v1.1.0

Published

Easily wrap React components around your MDX files

Downloads

1

Readme

Build Status npm version codecov

📦 MDX extended loader

Easily wrap React components around your MDX files

A Webpack loader for mutating your MDX files to export a wrapping React component by default. Easy to extend as well.

It already includes the same functionality as @mdx-js/loader provides, so there is no need to chain it to your Webpack configuration.

Installation

yarn add @saschazar/mdx-layout-loader

or

npm install --save @saschazar/mdx-layout-loader

(@mdx-js/react is a dependency you will need when transpiling React to JavaScript code, so be sure to take a look at the peerDependencies as well.)

How it works

It works just as any other Webpack loader, although it is mainly targeted towards transpiling mdx and md files.

Initial situation

Given the following project tree, where all the mdx files should be wrapped in a layout from the layouts directory:

MyApp
|
├─ pages
|  ├ index.jsx
|  ├ about.mdx
|  ├─ blog
|     ├ 2019-12-19_first-blog-post.mdx
|
├─ layouts
   ├ index.jsx
   ├ custom.jsx

Goal

With two files in the layouts directory, Webpack should look for mdx files in the project tree and wrap the most suitable layout around each of them.

The file in the pages/blog directory includes a date string in its filename, so this information should be parsed as well.

Enter Webpack

To achieve the goal, Webpack should do the following:

  1. parse information from the filename (date and title)
  2. mix the data with possibly included frontmatter (e.g. layout, etc...)
  3. select a layout and wrap it around its contents

Basically, a simple webpack rule for this use case might look like the following:

{
  // other webpack config options
  module: {
      rules: [
        {
          test: /\.mdx?$/,
          use: [
            {
              loader: 'babel-loader', // needed for transpiling React code
              query: {
                cacheDirectory: true
              }
            },
            {
              loader: '@saschazar/mdx-extended-loader',
              options: {
                extensions: ['jsx', 'js'], // the file suffixes the layouts
                layoutsDir: 'layouts', // relative to process.cwd()
                rehypePlugins: [], // @mdx-js/mdx option
                remarkPlugins: [] // @mdx-js/mdx option
              }
            }
          ]
        },
      ]
    }
  });
}

Options

In order to customize the Webpack flow, the following options may be applied to the configuration.

In addition to the options below, the options object may be extended with the options of the @mdx-js/mdx module.

extensions

array | mandatory | example: ['jsx', 'js']

The file extensions of possible layouts to look for in the layoutsDir directory

layouts

string | mandatory | example: layouts

The relative path to the layouts directory in the project's working directory

parseFilename

boolean | optional | default: true

Whether to attempt to parse the mdx filename for date and title

useDefault

boolean | optional | default: true

Whether to use the index file in the layouts directory as fallback, when no layout key was given in the frontmatter

Credits

Without next-mdx-enhanced, none of this would have happened, or at least in a very different way.

The main reason why I started this project however, was the fact, that I found next-mdx-enhanced a little too opinionated and first and foremost too tightly coupled to Next.js.

So I decided to split it into a Webpack-only loader, together with a filename parsing option. Voilà.

License

Licensed under the MIT license.

Copyright ©️ 2019 - 2020 Sascha Zarhuber