编辑:这里是an example repo
我认为您可以将 gatsby-source-filesystem 指向您的 html 文件夹并为其中的每个文件创建一个节点。一旦你有了它,你就可以在你的模板中查询它们,就像使用其他 Markdown 节点一样。
假设您在内容文件夹中有 html:
root
|--content
| `--htmls
| |--post1.html
| `--post2.html
|
|--src
| `--templates
| `--blog.js
|
|--gatsby-config.js
`--gatsby-node.js
将 gatsby-source-filesystem 指向您的 html 文件夹:
// gatsby-config.js
{
resolve: `gatsby-source-filesystem`,
options: {
path: `${__dirname}/content/htmls`,
name: `html`,
},
},
然后在gatsby-node.js 中,您可以使用loadNodeContent 读取原始html。从那时起它就非常简单了,只需按照 Gatsby 的文档中关于 creating node 的这个示例进行操作即可。
const { createContentDigest } = require("gatsby-core-utils");
exports.onCreateNode = async ({
node, loadNodeContent, createNodeId, actions
}) => {
// only care about html file
if (node.internal.type !== 'File' || node.internal.mediaType !== 'text/html') return;
const { createNode } = actions;
// read the raw html content
const nodeContent = await loadNodeContent(node);
// set up the new node
const htmlNodeContent = {
id: createNodeId(node.relativePath), // required
content: nodeContent,
name: node.name, // take the file's name as identifier
internal: {
type: 'HTMLContent',
contentDigest: createContentDigest(nodeContent), // required
}
...otherNecessaryMetaDataProps
}
createNode(htmlNodeContent);
}
创建节点后,您可以使用以下命令查询它们
{
allHtmlContent {
edges {
node {
name
content
}
}
}
}
从那时起,几乎将它们视为其他降价节点。如果您需要解析内容(例如定位图像文件等),它会变得更加复杂。在这种情况下,我认为您需要查看rehype 之类的内容。