npm gray-matter不支持最新的nextjs和node版本

euoag5mw  于 2023-04-11  发布在  其他
关注(0)|答案(1)|浏览(204)

我使用的是最新的nextjs版本。我必须使用灰色物质显示markdown博客,但似乎官方文档https://www.npmjs.com/package/gray-matter上给出的示例也不起作用。在此代码中包括我的文件
Package.json依赖项

"dependencies": {
    "clsx": "1.2.1",
    "gray-matter": "^4.0.3",
    "next": "latest",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "remark": "^14.0.2",
    "remark-html": "^15.0.2",
    "styled-components": "6.0.0-beta.2",
    "styled-jsx": "5.1.0",
    "use-count-up": "3.0.1"
  },
  "devDependencies": {
    "@types/node": "18.0.3",
    "@types/react": "18.0.15",
    "@types/react-dom": "18.0.6",
    "autoprefixer": "^10.4.7",
    "eslint": "8.19.0",
    "eslint-config-next": "latest",
    "postcss": "^8.4.14",
    "prettier": "^2.7.1",
    "prettier-plugin-tailwindcss": "^0.1.12",
    "tailwindcss": "^3.1.6",
    "typescript": "4.7.4"
  }

我正在阅读博客的地方。

import fs from 'fs'
import path from 'path'
import { remark } from 'remark'
import matter from 'gray-matter';
import remarkHtml from 'remark-html'

// const matter = require('gray-matter');

export function getAllPosts() {
  const postsDirectory = path.join(process.cwd(), '/blogs/HFTs')
  const filenames = fs.readdirSync(postsDirectory)

  return filenames.map(filename => {
    const file = fs.readFileSync(path.join(process.cwd(), '/blogs/HFTs', filename), 'utf8')
   
    // get frontmatter
    const { data, content } = matter(file)
    console.log(matter('---\ntitle: Front Matter\n---\nThis is content.'));

    // get slug from filename
    const slug = filename.replace(/\.mdx$/, '')

    // return combined frontmatter and slug; build permalink
    return {
      ...data,
      permalink: `/HFTs/${slug}`,
      slug,
    }
  })
}

官方例子的结果也不起作用。

console.log(grayMatter('---\ntitle: Front Matter\n---\nThis is content.'));
// is printing
{
  content: '---\ntitle: Front Matter\n---\nThis is content.',
  data: {},
  isEmpty: false,
  excerpt: '',
  orig: <Buffer 2d 2d 2d 0a 74 69 74 6c 65 3a 20 46 72 6f 6e 74 20 4d 61 74 74 65 72 0a 2d 2d 2d 0a 54 68 69 73 20 69 73 20 63 6f 6e 74 65 6e 74 2e>
}

数据为空对象。

mrwjdhj3

mrwjdhj31#

问题是我使用的是Turbopack。Turbopack仍然处于实验模式,现在不支持mdx。通过从我的项目中删除Turbopack修复了这个问题。

相关问题