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

Expose RawDocumentData on vFile data field for Markdown/MDX Files #192

Closed
Saeris opened this issue Apr 22, 2022 · 1 comment
Closed

Expose RawDocumentData on vFile data field for Markdown/MDX Files #192

Saeris opened this issue Apr 22, 2022 · 1 comment
Milestone

Comments

@Saeris
Copy link

Saeris commented Apr 22, 2022

It would be useful to expose metadata about the raw source files in the vFile data field so that it can be accessed inside of Remark/Rehype plugins. The data that came to mind is the from the document _raw property specified here, which includes the source file name and directory, which currently gets lost in processing.

Taking a look at the vFile data we get now, all we have is the content string, the current working directory, and the filepath given in the history field appears to be a combination of the cwd, the contentDirPath, and a generated filename (ex: _mdx_bundler_entry_point-<hash>.mdx). This isn't very useful to us since some important data is lost, namely the full file path and name.

For my specific case, I'd like to transform relative image URLs using @jsdevtools/rehype-url-inspector to absolute file paths, then with rehype-img-size I can extract image width + height data. This would help me use next/image to further process raw images. Given the source file's location, I can construct the correct relative path for the linked images myself.

How this could be done is with a default unified plugin, such as:

const addRawDocumentMeta = (): import("unified").Transformer => (_, vfile) => {
  Object.assign(vfile.data, { /* merge document._raw here */ })
};

And add it to the plugins lists here and here.

As for how the _raw metadata gets injected, I'm unsure how that can be done since I'm having difficulty understanding the codebase. Otherwise I'd open a PR to implement this myself.

Seems like this could help with #11

Saeris added a commit to Saeris/contentlayer that referenced this issue Apr 26, 2022
Implementation for the request in contentlayerdev#192 that also partially solves for contentlayerdev#11.

In `@contentlayer/core` I added a new `data` field to the  options argument of `markdownToHtml` and `bundleMDX` which can be used to pass arbitrary data to the resulting document's `vFile` `data` property.

Not knowing how you want to structure utility functions for this library, in the initial implementation I've inlined the `addRawDocumentMeta` remark plugin used to append the vFile inside of both `markdownToHtml` and `bundleMDX`. Please advise me if you'd like this extracted out to somewhere else instead.

In this PR I've only updated the `mapping.ts` file for the `source-files` package, as I'm unsure whether other sources like `source-contentful` expose the same `RawDocumentData` that the filesystem source does. Other sources can pass whatever document metadata is pertinent to them to the markdown processors using this addition to their APIs.
@schickling schickling added this to the 0.2.5 milestone May 10, 2022
@schickling
Copy link
Collaborator

This should be addressed with the 0.2.5 release. 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants