github.com/elek/golangci-lint@v1.42.2-0.20211208090441-c05b7fcb3a9a/docs/gatsby-node.js (about)

     1  const path = require(`path`);
     2  const { createFilePath } = require(`gatsby-source-filesystem`);
     3  const fs = require(`fs`);
     4  
     5  const {
     6    normalizeBasePath,
     7    resolveLink,
     8  } = require(`./src/@rocketseat/gatsby-theme-docs-core/util/url`);
     9  const withDefault = require(`./src/@rocketseat/gatsby-theme-docs-core/util/with-default`);
    10  
    11  exports.createPages = ({ graphql, actions: { createPage }, reporter }) => {
    12    reporter.success(`onCreateDocs`);
    13  
    14    const siteConfig = require(`./src/config/site.js`);
    15    const { basePath, baseDir, docsPath, githubUrl } = withDefault(siteConfig);
    16  
    17    const docsTemplate = require.resolve(
    18      `./src/@rocketseat/gatsby-theme-docs/src/templates/docs-query.js`
    19    );
    20    const homeTemplate = require.resolve(
    21      `./src/@rocketseat/gatsby-theme-docs/src/templates/homepage-query.js`
    22    );
    23  
    24    return graphql(
    25      `
    26        {
    27          files: allFile(filter: { extension: { in: ["md", "mdx"] } }) {
    28            edges {
    29              node {
    30                id
    31                relativePath
    32                childMdx {
    33                  frontmatter {
    34                    next
    35                  }
    36                  fields {
    37                    slug
    38                  }
    39                }
    40              }
    41            }
    42          }
    43          sidebar: allSidebarItems {
    44            edges {
    45              node {
    46                label
    47                link
    48                items {
    49                  label
    50                  link
    51                }
    52                id
    53              }
    54            }
    55          }
    56        }
    57      `
    58    ).then((result) => {
    59      if (result.errors) {
    60        reporter.panic(
    61          `docs: there was an error loading the docs folder!`,
    62          result.errors
    63        );
    64        return;
    65      }
    66  
    67      createPage({
    68        path: basePath,
    69        component: homeTemplate,
    70      });
    71  
    72      // Generate prev/next items based on sidebar.yml file
    73      const sidebar = result.data.sidebar.edges;
    74      const listOfItems = [];
    75  
    76      sidebar.forEach(({ node: { label, link, items } }) => {
    77        if (Array.isArray(items)) {
    78          items.forEach((item) => {
    79            listOfItems.push({
    80              label: item.label,
    81              link: resolveLink(item.link, basePath),
    82            });
    83          });
    84        } else {
    85          listOfItems.push({
    86            label,
    87            link: resolveLink(link, basePath),
    88          });
    89        }
    90      });
    91  
    92      // Generate docs pages
    93      const docs = result.data.files.edges;
    94      docs.forEach((doc) => {
    95        const {
    96          childMdx: {
    97            frontmatter: { next },
    98            fields: { slug },
    99          },
   100          relativePath,
   101        } = doc.node;
   102  
   103        const githubEditUrl =
   104          githubUrl &&
   105          `${githubUrl}/tree/master/${baseDir}/${docsPath}/${relativePath}`;
   106  
   107        const currentPageIndex = listOfItems.findIndex(
   108          (page) => page.link === slug
   109        );
   110  
   111        const prevItem = listOfItems[currentPageIndex - 1];
   112        const nextItem = next
   113          ? listOfItems.find((item) => item.link === next)
   114          : listOfItems[currentPageIndex + 1];
   115  
   116        createPage({
   117          path: slug,
   118          component: docsTemplate,
   119          context: {
   120            slug,
   121            prev: prevItem,
   122            next: nextItem,
   123            githubEditUrl,
   124          },
   125        });
   126      });
   127  
   128      reporter.success(`docs pages created`);
   129    });
   130  };
   131  
   132  exports.createSchemaCustomization = ({ actions }) => {
   133    actions.createTypes(`
   134      type MdxFrontmatter @dontInfer {
   135        title: String!
   136        description: String
   137        image: String
   138        disableTableOfContents: Boolean
   139        next: String
   140      }
   141    `);
   142  
   143    actions.createTypes(`
   144      type SidebarItems implements Node {
   145        label: String!
   146        link: String
   147        items: [SidebarItemsItems]
   148      }
   149  
   150      type SidebarItemsItems {
   151        label: String
   152        link: String
   153      }
   154    `);
   155  };
   156  
   157  exports.onPreBootstrap = ({ store, reporter }, themeOptions) => {
   158    const { configPath, docsPath } = withDefault(themeOptions);
   159    const { program } = store.getState();
   160  
   161    const dirs = [
   162      path.join(program.directory, configPath),
   163      path.join(program.directory, docsPath),
   164    ];
   165  
   166    dirs.forEach((dir) => {
   167      if (!fs.existsSync(dir)) {
   168        reporter.success(`docs: intialized the ${dir} directory`);
   169        fs.mkdirSync(dir);
   170      }
   171    });
   172  };
   173  
   174  exports.onCreateNode = (
   175    { node, actions: { createNodeField }, getNode },
   176    themeOptions
   177  ) => {
   178    if (node.internal.type !== `Mdx`) {
   179      return;
   180    }
   181  
   182    const { basePath } = withDefault(themeOptions);
   183  
   184    let value = createFilePath({ node, getNode });
   185    if (value === "index") value = "";
   186  
   187    createNodeField({
   188      name: `slug`,
   189      node,
   190      value: normalizeBasePath(basePath, value),
   191    });
   192  
   193    createNodeField({
   194      name: `id`,
   195      node,
   196      value: node.id,
   197    });
   198  };
   199  
   200  /**
   201  [
   202    {
   203      "node": {
   204        "label": "Home",
   205        "link": "/",
   206        "items": null,
   207        "id": "a2913be3-af3c-5fc9-967e-a058e86b20a9"
   208      }
   209    },
   210    {
   211      "node": {
   212        "label": "With dropdown",
   213        "link": null,
   214        "items": [
   215          { "label": "My Example", "link": "/my-example" },
   216          { "label": "Teste 2", "link": "/teste-2" }
   217        ],
   218        "id": "c7d9606c-4bda-5097-a0df-53108e9f4efd"
   219      }
   220    }
   221  ]
   222  */
   223  
   224  // Ler todo o array e salvar em uma objeto chave/valor
   225  /**
   226   * {
   227   *    '/': {
   228   *       prev: null,
   229   *       next: {
   230   *          label: 'My example',
   231   *          link: '/my-example'
   232   *       }
   233   *    },
   234   *    '/my-example': {
   235   *       prev: {
   236   *          label: 'Home',
   237   *          link: '/'
   238   *       },
   239   *       next: {
   240   *          label: 'Teste 2',
   241   *          link: '/teste-2'
   242   *       }
   243   *    },
   244   *    '/teste-2': {
   245   *       prev: {
   246   *          label: 'My example',
   247   *          link: '/my-example'
   248   *       },
   249   *       next: null
   250   *    }
   251   * }
   252   */