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

v3: MarkdownRoot body type: "minimal" #3072

Open
chrisjansky opened this issue Jan 31, 2025 · 4 comments
Open

v3: MarkdownRoot body type: "minimal" #3072

chrisjansky opened this issue Jan 31, 2025 · 4 comments
Labels

Comments

@chrisjansky
Copy link

Hello,

I have a custom logic from v2 that parsed MarkdownParsedContent (now PageCollectionItemBase) and constructed a new AST as needed.

I have now encountered a different md structure for MarkdownRoot: the typings suggest it should be of type MDCRoot with type: 'root', but the actual queryCollection outputs body of type: 'minimal' and a different object shape:

"body": {
      "type": "minimal",
      "value": [
        [
          "h1",
          {
            "id": "licensing-basics"
          },
          "Licensing Basics"
        ],
...

Lurking around in source I see the minimal types in module.d.ts but I see no mention in the docs and they are not exported so clearly purely internal:

type MinimalText = string;
type MinimalElement = [string, Record<string, unknown>, ...MinimalNode[]];
type MinimalNode = MinimalElement | MinimalText;
type MinimalTree = {
    type: 'minimal';
    value: MinimalNode[];
};

Do I have any control over how is the format chosen between minimal and MDCRoot with children: Array<MDCNode> as before? Or should the minimal types be exposed to be available for typings?

Thanks

@chrisjansky
Copy link
Author

Update: upon lurking around the module source, I found the internal functions decompressTree et c in src/runtime/internal/abstact-tree to convert the data back to MDCRoot, yet only these are not reachable via import.

Any chance these could be exposed? 🙏

@farnabaz farnabaz added the v3 label Feb 3, 2025
@farnabaz
Copy link
Member

farnabaz commented Feb 3, 2025

As you may already know, minimal format is designed to reduce the dump size and finally the page size. It is yet not supported by @nuxtjs/mdc which is main responsible for rendering this AST, therefore Content module has this decompressTree util.

Any chance these could be exposed? 🙏

We can expose both types and the decompressTree util 👍

@chrisjansky
Copy link
Author

chrisjansky commented Feb 3, 2025

Thanks for the explanation @farnabaz.

For now I solved it like so:

//@ts-ignore: Non-documented function
import { decompressTree } from "../../node_modules/@nuxt/content/dist/runtime/internal/abstract-tree.js"

Perhaps I have a bug to report pertaining to this (I tried to create a minimal Stackblitz reproduction, but I am getting some callSite.getFileName is not a function. (In 'callSite.getFileName()', 'callSite.getFileName' is undefined) error even running the official Nuxt starter template from Stackblitz homepage today, so I gave up after ~10 mins).

When defining a collection like so:

export default defineContentConfig({
 support: defineCollection({
      type: "page",
      source: "support/**/*.md",
    }),
  },
})

And querying it like so:

const { data } = await useAsyncData("some-page", () => queryCollection("support").path("/support/some-page").all())

Typescript infers the type as

interface SupportCollectionItem extends PageCollectionItemBase {}

PageCollectionItemBase should have body: MarkdownRoot;

But in fact {{ data }} in <template> yields:

[
  {
    "id": "support/support/some-page.md",
    "title": "Some Page Title",
    "body": {
      "type": "minimal",
      "value": [
        [
          "p",
          {},
          "This is some text."
        ],
      ...
]

Is this a bug and body is not decompressed as it should be or is there a way to control the decompression via an explicit setting?

Obviously I can decompress it manually, but the ...ItemBase types should reflect that, I think?

Thanks

Copy link
Member

farnabaz commented Feb 3, 2025

Perhaps I have a bug to report pertaining to this (I tried to create a minimal Stackblitz reproduction, but I am getting some

The StackBlitz problem will resolve in v3.1.0. Module will detect WebContainers environment and ask to install sqlite3 package, This is the only to run sqlite in WebContainers.
You can try this with latest commit release

npm i https://pkg.pr.new/@nuxt/content@977d2ea

PageCollectionItemBase should have body: MarkdownRoot;

Indeed you right, we should update this type and use minimal type.

is there a way to control the decompression via an explicit setting?

There isn't any at the time, and I believe there will not any in future. The minimization reduces tremendous amount of size from dump (~40%-66%).

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

No branches or pull requests

2 participants