javascript 将降价文件作为字符串导入Next.js

mlmc2os5  于 2023-01-16  发布在  Java
关注(0)|答案(4)|浏览(132)

我如何在Next.js中将markdown文件作为字符串导入,以便在客户端和服务器端工作?

lyr7nygr

lyr7nygr1#

您可以配置Next.js webpack加载器来加载markdown文件并将其作为字符串返回,例如:

    • 一月一日**
# Home

This is my **awesome** home!
    • 一米一米一**
import React from 'react';
import markdown from '../docs/home.md';

export default () => {
  return (
    <div>
      <pre>{markdown}</pre>
      <small><i>Import and render markdown using Next.js</i></small>
    </div>
  );
};
    • 一米二米一x**
{
  "name": "example",
  "version": "1.0.0",
  "scripts": {
    "dev": "next",
    "build": "next build",
    "start": "next start"
  },
  "dependencies": {
    "file-loader": "^1.1.6",
    "next": "^4.2.1",
    "raw-loader": "^0.5.1",
    "react": "^16.2.0",
    "react-dom": "^16.2.0"
  }
}
    • 一米三米一x**
module.exports = {
  webpack: (config) => {
    return Object.assign({}, config, {
      externals: Object.assign({}, config.externals, {
        fs: 'fs',
      }),
      module: Object.assign({}, config.module, {
        rules: config.module.rules.concat([
          {
            test: /\.md$/,
            loader: 'emit-file-loader',
            options: {
              name: 'dist/[path][name].[ext]',
            },
          },
          {
            test: /\.md$/,
            loader: 'raw-loader',
          }
        ]),
      }),
    });
  }
};

运行时:

$ npm run dev

会出现如下内容:

使用markdown字符串,你可以做任何你想做的事情。例如,用marksy处理它。

rqcrx0a6

rqcrx0a62#

更快的"Next.js方法"是使用插件next-mdx
文件:https://github.com/vercel/next.js/tree/canary/packages/next-mdx

// next.config.js
const withMDX = require('@zeit/next-mdx')({
  extension: /\.mdx?$/
})
module.exports = withMDX({
  pageExtensions: ['js', 'jsx', 'mdx']
})
wtzytmuj

wtzytmuj3#

只需安装raw-loader

npm install --save raw-loader

然后编辑您的next.config.js

webpack: (config) => {
  config.module.rules.push({
    test: /\.md$/,
    use: 'raw-loader',
  });
  return config;
},
xpcnnkqh

xpcnnkqh4#

更新:不严格要求emit-file-loader,弃用raw-loader以支持资产模块

https://stackoverflow.com/a/47954368/895245的一些更新可能是由于最新的开发:

  • 资产模块被取代raw-loaderhttps://stackoverflow.com/a/47954368/895245不再需要安装任何额外的软件包
  • emit-file-loader似乎不再必要,不确定是否需要它,或者它是否用于更专业的东西

所以我们可以稍微简化一下
pages/index.js

import world from '../world.md'

export default function IndexPage() {
  return <div>hello {world}</div>
}

next.config.js

module.exports = {
  webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
    config.module.rules.push(
      {
        test: /\.md$/,
        // This is the asset module.
        type: 'asset/source',
      }
    )
    return config
  },
}

package.json

{
  "name": "test",
  "version": "1.0.0",
  "scripts": {
    "dev": "next",
    "build": "next build",
    "start": "next start"
  },
  "dependencies": {
    "next": "12.2.0",
    "react": "17.0.2",
    "react-dom": "17.0.2"
  }
}

world.md

my md world

让它从Typescript工作

遗憾的是,boch raw-loader和资产模块需要更多的打字工作:https://github.com/webpack-contrib/raw-loader/issues/56#issuecomment-423640398您必须创建一个文件:
global.d.ts

declare module '*.md'

否则,导入将失败,并显示:

Type error: Cannot find module '../world.md' or its corresponding type declarations.

完整示例:
global.d.ts

declare module '*.md'

页/索引.tsx

import world from '../world.md'

export default function IndexPage() {
  const what: string = 'my'
  // Would fail on build as desired.
  // const what2: int = 'world2'
  return <div>hello {what} {world}</div>
}

next.config.js

module.exports = {
  webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
    config.module.rules.push(
      {
        test: /\.md$/,
        type: 'asset/source',
      }
    )
    return config
  },
}

package.json

{
  "private": true,
  "scripts": {
    "dev": "next",
    "build": "next build",
    "start": "next start",
    "type-check": "tsc"
  },
  "dependencies": {
    "next": "v12.2.0",
    "react": "17.0.2",
    "react-dom": "17.0.2"
  },
  "devDependencies": {
    "@types/node": "12.12.21",
    "@types/react": "17.0.2",
    "@types/react-dom": "17.0.1",
    "typescript": "4.0"
  }
}

world.md

my md world

相关问题