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

Change frontmatter injection ordering #5687

Merged
merged 12 commits into from
Jan 3, 2023

Conversation

bholmesdev
Copy link
Contributor

@bholmesdev bholmesdev commented Dec 28, 2022

Changes

Give remark and rehype plugins access to user frontmatter via frontmatter injection. This means data.astro.frontmatter is now the complete Markdown or MDX document's frontmatter, rather than an empty object.

This allows plugin authors to modify existing frontmatter, or compute new properties based on other properties. For example, say you want to compute a full image URL based on an imageSrc slug in your document frontmatter:

export function remarkInjectSocialImagePlugin() {
  return function (tree, file) {
    const { frontmatter } = file.data.astro;
    frontmatter.socialImageSrc = new URL(
      frontmatter.imageSrc,
      'https://my-blog.com/',
    ).pathname;
  }
}

Content Collections change

We have changed inject frontmatter to modify frontmatter in our docs to improve discoverability. This is based on support forum feedback where injection is rarely the term used.

To reflect this, the injectedFrontmatter property has been renamed to remarkPluginFrontmatter. This should clarify this plugin is still separate from the data export Content Collections expose today. We also expect remarkPluginFrontmatter to be deprecated once schemas can compute frontmatter based on file contents.

Testing

  • Update frontmatter injection tests to consider frontmatter mutation

Docs

withastro/docs#2250

@bholmesdev bholmesdev requested a review from a team as a code owner December 28, 2022 15:45
@changeset-bot
Copy link

changeset-bot bot commented Dec 28, 2022

🦋 Changeset detected

Latest commit: 3d50fa8

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

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

@github-actions github-actions bot added pkg: astro Related to the core `astro` package (scope) pkg: integration Related to any renderer integration (scope) feat: markdown Related to Markdown (scope) labels Dec 28, 2022
@@ -36,33 +35,6 @@ export function getFileInfo(id: string, config: AstroConfig) {
return { fileId, fileUrl };
}

function isValidAstroData(obj: unknown): obj is MarkdownAstroData {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move frontmatter injection utils to @astrojs/markdown-remark

@github-actions github-actions bot added the pkg: example Related to an example package (scope) label Dec 28, 2022
Copy link
Contributor

@matthewp matthewp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking as we are not ready to merge major changes into main just yet.

@matthewp
Copy link
Contributor

Code looks good! Let's talk about this with the core team before merging next week.

@bholmesdev bholmesdev added the semver: major Change triggers a `major` release label Dec 30, 2022
@bholmesdev bholmesdev force-pushed the feat/injected-frontmatter-ordering branch from f82f700 to c85688c Compare January 2, 2023 15:44
Copy link
Contributor

@aFuzzyBear aFuzzyBear left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was interesting to see how you readjusted the order of the frontmatter process.
So its now safe to say that frontmatter is mutable via the remark tree and the output of that is what is then provided into other contexts

@@ -520,6 +520,20 @@ See https://docs.astro.build/en/guides/server-side-rendering/ for more informati
},
hint: 'See https://docs.astro.build/en/guides/content-collections/ for more information on content schemas.',
},
/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💚 this

Copy link
Contributor

@matthewp matthewp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved by TSC, @bholmesdev just need to fix the conflicts.

@bholmesdev bholmesdev force-pushed the feat/injected-frontmatter-ordering branch from 38a1133 to 3d50fa8 Compare January 3, 2023 20:40
@bholmesdev bholmesdev merged commit e2019be into main Jan 3, 2023
@bholmesdev bholmesdev deleted the feat/injected-frontmatter-ordering branch January 3, 2023 21:31
export { Content, getHeadings, _internal } from ${JSON.stringify(basePath)};
export { Content, getHeadings } from ${JSON.stringify(basePath)};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems frontmatter needed to be added here where _internal was removed.

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) pkg: example Related to an example package (scope) pkg: integration Related to any renderer integration (scope) semver: major Change triggers a `major` release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

make parsed md/mdx frontmatter available to plugins Empty Markdown frontmatter at build time
4 participants