Convert markdown to mdast tree with gfm extention don't work (Cannot set properties of undefined (setting 'inTable')) #117
-
I try to convert an .md file to a mdast tree(JSON) using a gfm extention in Next.js 13 and I got an Typescript in VSC and Next.js "Cannot set properties of undefined (setting 'inTable')" error in the browser. The code is the next one: "use server"
import fs from 'fs';
import path from 'path';
import matter from 'gray-matter';
import { fromMarkdown } from "mdast-util-from-markdown"
import { gfm } from 'micromark-extension-gfm'
import { gfmFromMarkdown, gfmToMarkdown } from 'mdast-util-gfm'
const postsDirectory = path.join(process.cwd(), 'blogposts');
export async function getPostDataToEdit (id: string) {
const fullPath = path.join(postsDirectory, `${id}.md`);
const fileContents = fs.readFileSync(fullPath, 'utf8');
const matterResult = matter(fileContents);
const tree = fromMarkdown(fileContents,'utf8' , {
extensions: [gfm()],
mdastExtensions: [ gfmFromMarkdown() ]
})
const blogPost: BlogPost & { body: any } = {
id,
title: matterResult.data.title,
date: matterResult.data.date,
published: matterResult.data.published ? "Published" : "Draft",
body: tree,
}
return blogPost;
} I read the documentation of mdast-util-from-markdown and mdast-util-gfm and couldn't solve this. If I remove the extention section from the function "fromMarkdown" works just fine but without idenifying the gfm markdown on the file, just only common markdown. The code without extention section: "use server"
import fs from 'fs';
import path from 'path';
import matter from 'gray-matter';
import { fromMarkdown } from "mdast-util-from-markdown"
import { gfm } from 'micromark-extension-gfm'
import { gfmFromMarkdown, gfmToMarkdown } from 'mdast-util-gfm'
const postsDirectory = path.join(process.cwd(), 'blogposts');
export async function getPostDataToEdit (id: string) {
const fullPath = path.join(postsDirectory, `${id}.md`);
const fileContents = fs.readFileSync(fullPath, 'utf8');
const matterResult = matter(fileContents);
const tree = fromMarkdown(fileContents,'utf8' , {
})
const blogPost: BlogPost & { body: any } = {
id,
title: matterResult.data.title,
date: matterResult.data.date,
published: matterResult.data.published ? "Published" : "Draft",
body: tree,
}
return blogPost;
} Please 🙏, can you help me? Thanks!! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
can you post versions? Can you post |
Beta Was this translation helpful? Give feedback.
You’re probably getting this error because some dependencies are out of date. Make sure every micromark and mdast package is at the latest.