首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gatsby前沿验证

Gatsby前沿验证
EN

Stack Overflow用户
提问于 2022-04-04 22:17:17
回答 1查看 82关注 0票数 0

关于模式定制,我有几个问题。

数据/类别

代码语言:javascript
复制
[
    {
        "name": "Foo Bar",
        "slug": "foo-bar"
    },
    { 
        "name": "Fuz Baz",
        "slug": "fuz-baz"
    }
]

gatsby-config.js

代码语言:javascript
复制
  ...
  
  mapping: {
    "MarkdownRemark.frontmatter.author": `AuthorsJson.alias`,
    "MarkdownRemark.frontmatter.category": `CategoriesJson.name`
  }

gatsby-node.js

代码语言:javascript
复制
exports.createSchemaCustomization = ({ actions, schema }) => {
    const { createTypes } = actions;
    const typeDefs = [
        `type AuthorsJson implements Node @dontInfer {
            alias: String,
            name: String
            slug: String,
            bio: String,
            profile: String
            posts: [MarkdownRemark] @link(by: "frontmatter.author.alias", from: "alias")
        }`,
        `type CategoriesJson implements Node @dontInfer {
            name: String,
            slug: String,
            posts: [MarkdownRemark] @link(by: "frontmatter.category.name", from: "name")
        }`,
    ];

    createTypes(typeDefs);
}

最基本的问题是,我如何完成正面的验证?

  1. 从上面的问题,我如何强迫每个帖子提供一个类别,并且这个类别必须存在于categories.json中(例如,它们不能组成自己的类别)?如果前端物质是一个数组,如何确保数组中不存在超过3个元素?

谢谢!

备选案文1:

这就是我在gatsby-node.js上的文章

代码语言:javascript
复制
var categories = [];
var aliases = [];

exports.onCreateNode = ({ node, getNode, actions }) => {
  const { createNodeField } = actions

  if(node.internal.type === `CategoriesJson`) {
      categories.push(node.name);
  }

  if(node.internal.type === `AuthorsJson`) {
      aliases.push(node.alias);
  }

  if (node.internal.type === `MarkdownRemark`) {
    const slug = createFilePath({ node, getNode, basePath: `pages` })

    createNodeField({
      node,
      name: `slug`,
      value: slug,
    })

    if (node.frontmatter.author === null || node.frontmatter.author === undefined) {
        throw "Page is missing alias: " + node.fileAbsolutePath;
    }

    if (aliases.indexOf(node.frontmatter.author) == -1) {
        throw "Page has invalid alias: " + node.fileAbsolutePath;
    }

    if (node.frontmatter.category === null || node.frontmatter.category === undefined) {
        throw "Page is missing category: " + node.fileAbsolutePath;
    }

    if (categories.indexOf(node.frontmatter.category) == -1) {
        throw "Page has invalid category ('" + node.frontmatter.category +"'): " + node.fileAbsolutePath;
    }
    
    if (node.frontmatter.tags.length > 3) {
        throw "Page has more than 3 tags: " + node.fileAbsolutePath;
    }
  }
}

有更好的办法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-05 05:48:21

您可以通过以下方法简化所有条件:

代码语言:javascript
复制
let categories = [];
let aliases = [];

exports.onCreateNode = ({ node, getNode, actions }) => {
  const { createNodeField } = actions

  if(node.internal.type === `CategoriesJson`) {
      categories.push(node.name);
  }

  if(node.internal.type === `AuthorsJson`) {
      aliases.push(node.alias);
  }

  if (node.internal.type === `MarkdownRemark`) {
    const slug = createFilePath({ node, getNode, basePath: `pages` })

    createNodeField({
      node,
      name: `slug`,
      value: slug,
    })

    if (!node.frontmatter.author || !aliases.includes(node.frontmatter.author)) {
        throw "Page is missing alias: " + node.fileAbsolutePath;
    }


    if (!node.frontmatter.category || !categories.includes(node.frontmatter.category)) {
        throw "Page is missing category: " + node.fileAbsolutePath;
    }

   
    if (node.frontmatter.tags.length > 3) {
        throw "Page has more than 3 tags: " + node.fileAbsolutePath;
    }
  }
}

我还添加了let,而不是var,它现在已经被废弃了。检查https://www.javascripttutorial.net/es6/difference-between-var-and-let/上的差异,并将indexOf == -1替换为更语义的includes

对于其他人来说,这种方法在我看来是相当稳固的,这是一个很好的方法。

如果前端是一个数组,我如何确保数组中不存在多于3个元素?

默认情况下(而且应该是这样),frontmatter始终是一个对象。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71744353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档