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

@mdxp/rehype-mdx-splitwrap

v0.2.0

Published

Split MDX files on a certain component and wrap the splits

Downloads

1

Readme

Web Slides Made Easy

REHYPE MDX - Split Wrap

npm version npm downloads main codecov

Rehype plugin to split MDX content on a specific component and wrap the resulting splits in another component.
Its main use is to enable automatic slide creation for MDXP.

Most users should not bother with this low level plugin and instead use one of the MDXP starter templates.
However, if you want to create your own MDXP pipeline, this plugin is the core of MDXP and transforms your MDX content, by it splitting on <hr/> tags and wrapping the resulting splits in <Slide/> tags.

Table of Contents

Installation

This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.

NPM

npm install @mdxp/rehype-mdx-splitwrap

YARN

yarn add @mdxp/rehype-mdx-splitwrap

PNPM

pnpm add @mdxp/rehype-mdx-splitwrap

API

This package has a single default export which is the rehype plugin.
The plugin takes the following options:

Usage

In order to use this plugin, you should specify it in the list of rehypePlugins of your mdx compilation pipeline.

Let's say we have the following example.mdx file:

# Split 1
content

---

# Split 2
- a
- b
- c

The following build pipeline in example.js will then compile the mdx file.

import {readFileSync} from 'fs'
import {compileSync} from '@mdx-js/mdx'
import rehypeSplitWrap from '@mdxp/rehype-mdx-splitwrap'

const result = compileSync(
  readFileSync('.sandbox/demo.mdx'),
  {
    jsx: true,
    rehypePlugins: [
        [rehypeSplitWrap, {
            splitComponent: 'hr',
            wrapperComponent: 'div',
            wrapperProps: {className: 'wrapper'},
        }],
    ],
  },
);

console.log(String(result));

The result of this compilation will then yield:

/*@jsxRuntime automatic @jsxImportSource react*/
function MDXContent(props = {}) {
  return (
    <>
      <div className="wrapper">
        <h1>Split 1</h1>
        <p>content</p>
      </div>
      
      <div className="wrapper">
        <h1>Split 2</h1>
        <ul>
          <li>a</li>
          <li>b</li>
          <li>c</li>
        </ul>
      </div>
    </>
  );
}

export default MDXContent;
/*@jsxRuntime automatic @jsxImportSource react*/
function _createMdxContent(props) {
  const _components = Object.assign({
    div: "div",
    h1: "h1",
    p: "p",
    ul: "ul",
    li: "li"
  }, props.components);
  return <>
    <_components.div className="wrapper">
      <_components.h1>{"Split 1"}</_components.h1>{"\n"}
      <_components.p>{"content"}</_components.p>{"\n"}
    </_components.div>
    <_components.div className="wrapper">{"\n"}
      <_components.h1>{"Split 2"}</_components.h1>{"\n"}
      <_components.ul>{"\n"}
        <_components.li>{"a"}</_components.li>{"\n"}
        <_components.li>{"b"}</_components.li>{"\n"}
        <_components.li>{"c"}</_components.li>{"\n"}
      </_components.ul>
    </_components.div>
  </>;
}

function MDXContent(props = {}) {
  const {wrapper: MDXLayout} = props.components || ({});
  return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout> : _createMdxContent(props);
}

export default MDXContent;

Skip Examples

You can tell the plugin to skip processing some parts of your code by adding skip comments.

// Plugin Configuration
{
  splitComponent: 'hr',
  wrapperComponent: 'Wrapper',
}

Skip Start - Stop
You can add "skip start" and "skip stop" comments in order to temporarily disable splitting your content on the specified splitComponent. Note that it is strongly discouraged to stride start and stop comments with your markup, as this might yield unexpected results.

# SPLIT 1

{/* splitwrap-skip start */}

---

{/* splitwrap-skip stop */}

content

---

# SPLIT 2
<>
  <Wrapper>
    <h1>SPLIT 1</h1>
    <hr />
    <p>content</p>
  </Wrapper>
  <Wrapper>
    <h1>SPLIT 2</h1>
  </Wrapper>
</>

Skip Inner
You can add a "skip inner" comment in order to disable splitting inside of a certain component. This is similar to adding "skip start" and "skip stop" as the first and last elements of a component.

# SPLIT 1

<sub>
{/* splitwrap-skip inner */}

content

---

content
</sub>

---

# SPLIT 2
<>
  <Wrapper>
    <h1>SPLIT 1</h1>
    <sub>
      <p>content</p>
      <hr />
      <p>content</p>
    </sub>
  </Wrapper>
  <Wrapper>
    <h1>SPLIT 2</h1>
  </Wrapper>
</>

Skip Outer
You can add a "skip outer" comment in order to disable splitting around a certain component. Additionally, when using a "skip outer" comment for the first or last component of a split, it will be excluded from the wrapperComponent.

# SPLIT 1

<sub>
{/* splitwrap-skip outer */}

content

---

content
</sub>

---

# SPLIT 2
<>
  <Wrapper>
    <h1>SPLIT 1</h1>
  </Wrapper>
  <sub>
    <p>content</p>
    <hr />
    <p>content</p>
  </sub>
  <Wrapper>
    <h1>SPLIT 2</h1>
  </Wrapper>
</>