Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new markdown.frontmatterPlugins config option #3411

Closed
wants to merge 2 commits into from

Conversation

sylvinus
Copy link

Hello!

Currently, when importing Markdown files, one can modify their content via the markdown.remarkPlugins and markdown.rehypePlugins config options.

However there is currently no supported way to do the same with their frontmatter. This could open up interesting usecases:

  • Auto-registering components in imported Markdown files (just append the imports to frontmatter.setup!)
  • Provide a more generic solution to the frontmatterDefaults suggestion here: Simplify Markdown Layouts roadmap#172 (comment)
  • Probably more that I'm missing!

This patch adds a prototype implementation of a new frontmatterPlugins config option.

Let me know what you think!

Thanks,

@changeset-bot
Copy link

changeset-bot bot commented May 19, 2022

⚠️ No Changeset found

Latest commit: fa3ed5c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions bot added pkg: astro Related to the core `astro` package (scope) feat: markdown Related to Markdown (scope) labels May 19, 2022
@mattfelten
Copy link

Any progress on this @sylvinus? I'm really excited for this change

@sylvinus
Copy link
Author

@mattfelten I'm excited for it as well! An official RFC has been created: withastro/roadmap#196 ; Not sure what/when the next steps are.

@delucis
Copy link
Member

delucis commented Jun 28, 2022

The RFC was accepted!

https://github.com/withastro/rfcs/blob/main/proposals/0022-frontmatter-plugins.md

Think the next step would be to get this PR polished and then it can be merged!

@sylvinus
Copy link
Author

By the way in the meantime I've also been using a custom plugin to register all available components in Markdown files:

// src/plugins/inject-components.mjs
import glob from 'fast-glob';
import path from 'path';

export default (dir) => {
  let components = [];
  return {
    name: 'mdInjectComponentsPlugin',
    enforce: 'pre',
    options: async () => {
      components = await glob(dir.replace(/\/$/g, '') + '/**/*');
    },
    transform: (ctx, id) => {
      if (!id.match(/\.md$/)) {
        return false;
      }
      let prepend = "";
      for (const component of components) {
        let name = path.basename(component, path.extname(component));
        prepend += "import "+name+" from '"+component+"';\n";
      }
      ctx = prepend + "\n" + ctx;
      return ctx;
    },
  };
};
// astro.config.mjs
import { defineConfig } from 'astro/config';
import path from 'path';
import {fileURLToPath} from 'url';
import mdInjectComponentsPlugin from "./src/plugins/inject-components.mjs";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

export default defineConfig({
  vite: {
    plugins: [mdInjectComponentsPlugin(path.resolve(__dirname, "./src/components/"))],
  },
});

@matthewp
Copy link
Contributor

Going to close since this branch is fairly stale. Feel free to resubmit when you have it working.

@matthewp matthewp closed this Jul 26, 2022
@sylvinus
Copy link
Author

FYI I probably won't have time to submit a more polished PR. It would be great if a more regular contributor could have a go at it!

@mrienstra
Copy link
Contributor

@delucis, @matthewp, some months have passed and yay, Astro v2! Trying to figure out the status + next steps for this. Aside from rebasing + polishing this PR, maybe the RFC should be reviewed to make sure it's still in alignment with other goals & sufficiently clear?

Context:
RFC PR: withastro/roadmap#196
RFC: https://github.com/withastro/roadmap/blob/main/proposals/0022-frontmatter-plugins.md

The RFC has two unresolved questions, should those be addressed before someone picks up this PR?

(Coming here from #397 (comment))

@delucis
Copy link
Member

delucis commented Jan 29, 2023

Modifying frontmatter as proposed in this RFC is now available!

Some docs: https://docs.astro.build/en/guides/markdown-content/#modifying-frontmatter-programmatically

mrienstra added a commit to mrienstra/withastro-roadmap that referenced this pull request Jan 30, 2023
@mrienstra
Copy link
Contributor

mrienstra commented Jan 30, 2023

Nice, I hadn't seen that yet!

Associated issue: #5099
Associated PR: #5687
Bug fix (merged but not released): #5927

Builds on this foundation: #4176

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: markdown Related to Markdown (scope) pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants