rockchalkwushock / rehype-code-titles

Rehype plugin for parsing code blocks and adding titles to code blocks
https://www.npmjs.com/package/rehype-code-titles
MIT License
21 stars 2 forks source link

Improvement: adding in options for the applied className and the titleSeparator #237

Closed tearingItUp786 closed 1 year ago

tearingItUp786 commented 1 year ago

Hey, there friend,

It would be super helpful if we could update the plugin to accept options such as className and titleSeparator. For example, a user (in this case me) might want to do something like language:title=an-example-title versus language:an-example-title in their input.

Here's what the new interface could look like. I'm more than happy to make a PR as well b/c I had to make changes to your plugin for it to fit my use case 😊.

import { BuildVisitor, visit } from "unist-util-visit";
import type * as H from "hast";
import type { VisitorResult } from "unist-util-visit";

type Options = {
  className?: string;
  titleSeparator?: string;
};

function rehypeConfigurableCodeTitles() {
  return function optionsHof({
    className = "rehype-configurable-code-title",
    titleSeparator = ":",
  }: Options) {
    return function transformer(tree: H.Root) {
      const visitor: BuildVisitor<H.Root, "element"> = (
        node,
        index,
        parent
      ): VisitorResult => {
        // a changed implementation that leverages the options
      }

      visit(tree, "element", visitor);
    };
  };
}

export default rehypeConfigurableCodeTitles;
rockchalkwushock commented 1 year ago

Hey @tearingItUp786 I like the idea may I ask why the separator needed to be changed?

If you are willing to put up a PR that ensures there is backwards compatibility for current users I would be more than happy to add the feature.

tearingItUp786 commented 1 year ago

Yup, more than happy to make a PR! I want the option for the separator because I'm coming from gatsby-remark-code-titles and all my mdx files are using the :title= syntax for the separator and I quite like it!

I'll throw something up right now!

rockchalkwushock commented 1 year ago

Closed by #240