2

我想要的内容文件夹结构如下:

- content (directory)
-- home.md

-- trip_A (directory)
--- intro.md
--- day_1.md
--- day_2.md

-- trip_B (directory)
--- intro.md
--- day_1.md

理想情况下,我希望将其定义为@gridsome/source-filesystem插件的单个“源”,以便将来添加的子文件夹会自动填充到源中。

但是,看来我可能必须@gridsome/source-filesystem为每个子文件夹手动指定一个单独的源?这是正确的,还是有某种解决方法?

例如:

  • 如何为所有使用typeName类型(即模板页面)Trip但在其各自的父目录中托管每个页面的旅行帖子(无论旅行 A、B、C 或将来的旅行 D)创建通用源,例如/tripA/day_1.html/tripB/day_1.html等等?pathPrefix除非有办法使用动态通配符路由,否则配置似乎只需要一个?
  • 如何使用不同的类型?source-filesystem在这里创建一个具有不同的单独条目似乎是有意义的typeName,除非有另一种方法可以做到这一点?
4

2 回答 2

2

您在 Gridsome 配置中尝试过什么?类似下面的内容应该收集content目录及其任何子文件夹中的所有文件:

{
   use: '@gridsome/source-filesystem',
   options: {
      path: './content/**/*.md',
      typeName: 'Post',  
   }
}

如果您trip_C将来添加一个,它将自动包含在集合中。

path: 'content/*/*.md'或者,如果您不想递归扫描子文件夹,可以在 glob 模式 ( ) 中使用单个星号。

这是有效的,因为您已将所有内容嵌套在content. path如果您想将来自多个顶级目录的文件组合到一个集合中,也可以在字段中指定模式列表。

这有帮助吗?

于 2020-11-18T04:28:04.533 回答
1

我发现这个问题最简单的解决方案是使用函数手动分配网格配置的模板部分中的路径

IE,对于这个插件设置...

var plugins = [
 {
    use: '@gridsome/source-filesystem',
    options: {
      path: '**/*.md',
      baseDir: 'temp/docs',
      typeName: 'Doc'
    }
  }
];

和这个函数来生成路径......

function slugify(node) {
  var text = node.title;
  console.log(node);  
  var text = text.toString().toLowerCase()
    .replace(/\s+/g, '-')           // Replace spaces with -
    .replace(/[^\w\-]+/g, '')       // Remove all non-word chars
    .replace(/\-\-+/g, '-')         // Replace multiple - with single -
    .replace(/^-+/, '')             // Trim - from start of text
    .replace(/-+$/, '');            // Trim - from end of text
    return  node.fileInfo.directory + '/' + text;
}

以及引用模板对象中的函数的这个 Gridsome 配置...

var gridsomeConfig = {
  siteName: siteName,
  icon: {
    favicon: favicon,
    touchicon: favicon
  },
  plugins: plugins,
  templates: {
    Doc:
      (node) => {
        return `/docs/${slugify(node)}/`
      }    
  }
}

并在各个子目录中给出了一些降价文件,即 /api 和 /ui...

您将在 /docs/ui、/docs/api 等处获得 Doc 类型的内容。只需要一个模板(GraphQL 类型 - 意味着所有文档的相同查询)。

于 2021-06-13T22:00:03.307 回答