我正在开发的 Gatsby 网站在该content/posts目录中包含其博客文章。我曾经gatsby-source-filesystem访问这些文件并将它们变成页面。
//gatsby-config.js
{
resolve: `gatsby-source-filesystem`,
options: {
path: `${post_dir}/content/posts/`,
name: "posts"
}
},
Run Code Online (Sandbox Code Playgroud)
这是我的 gatsby-node.js。
//gatsby-node.js
exports.onCreateNode = ({ node, getNode, boundActionCreators }) => {
const { createNodeField } = boundActionCreators;
if (node.internal.type === `MarkdownRemark`) {
const slug = createFilePath({ node, getNode, basePath: `pages` });
const separtorIndex = ~slug.indexOf("--") ? slug.indexOf("--") : 0;
const shortSlugStart = separtorIndex ? separtorIndex + 2 : 0;
createNodeField({
node,
name: `slug`,
value: `${separtorIndex ? "/" : ""}${slug.substring(shortSlugStart)}`
}); …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 js 库orgChart实现用户层次结构。通过getHierarchy()库中的方法正在输出如下所示的对象。
var datascource = {
"id": "1",
"children": [{
"id": "2"
}, {
"id": "3",
"children": [{
"id": "4"
}, {
"id": "5",
"children": [{
"id": "6"
}, {
"id": "7"
}]
}]
}, {
"id": "10"
}, {
"id": "12"
}]
};
Run Code Online (Sandbox Code Playgroud)
我想从树中的 id 生成平面数组。前任://["1", "2", "3", "4", "5", "6", "7", "10", "12"]
我想出了,
function getNestedArraysOfIds(node) {
if (node.children == undefined) {
return [node.id];
} else {
return [node.id,...node.children.map(subnode => (subnode.children==undefined) ? …Run Code Online (Sandbox Code Playgroud)