Skip to content

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

Closed
@Saeris

Description

@Saeris

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions