小编Lak*_*n S的帖子

如何从 Gatsby 的远程 git 存储库检索 markdown?

我正在开发的 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)

git gatsby

5
推荐指数
1
解决办法
1007
查看次数

使用带有递归的reduce函数从多级树生成一个平面的id数组?

我正在尝试使用 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)

javascript recursion orgchart ecmascript-6

2
推荐指数
1
解决办法
1899
查看次数

标签 统计

ecmascript-6 ×1

gatsby ×1

git ×1

javascript ×1

orgchart ×1

recursion ×1