这是一个 CLI 工具,可以从 Contentful CMS 中提取数据并将其转换为 Markdown 或 YAML 文件,以便与静态站点生成器一起使用。它可以与任何使用 Markdown 和 YAML frontmatter 的静态站点生成器一起使用,但它具有一些特定于 Hugo 的功能。它还包括一个简单的 Express 服务器,可以从 Contentful 接收 Webhooks 以重新触发获取和删除命令(在运行预览环境时很有用)。
安装 Node.js(最低支持版本是 Node v18)
与 NPM
# local install
npm install contentful-hugo
# global install
npm install contentful-hugo -g
与 PNPM
# local install
pnpm install contentful-hugo
# global install
pnpm install contentful-hugo -g
完成配置,然后在终端中运行以下命令
# # initialize the directory
contentful - hugo -- init
# # fetch content from contentful
contentful - hugo [ flags ]
npx contentful - hugo -- init
npx contentful - hugo [ flags ]
# or when using pnpm
pnpm contentful - hugo -- init
pnpm contentful - hugo [ flags ]
旗帜 | 别名 | 描述 |
---|---|---|
--初始化 | 初始化目录。为内容丰富的富文本字段生成配置文件和默认短代码 | |
- 预览 | -P | 在预览模式下运行,从 Contentful 中提取已发布和未发布的条目 |
- 等待 | -W | 等待指定的毫秒数,然后再从 Contentful 中提取数据。 |
--配置 | -C | 指定配置文件的路径。 |
- 服务器 | -S | 在服务器模式下运行以接收来自 Contentful 的 Webhooks |
- 港口 | 指定服务器模式的端口(默认1414) | |
- 干净的 | 删除 singleTypes 和 RepeatableTypes 中指定的任何目录 | |
- 帮助 | 显示帮助 | |
- 版本 | 显示版本号 |
contentful - hugo -- wait = 2000 -- preview -- config = " my_custom_config.js "
# or
contentful - hugo -- wait 2000 -- preview -- config my_custom_config.js
{
"name" : " my-hugo-project " ,
"scripts" : {
"dev" : " contentful-hugo --preview && hugo server " ,
"build" : " contentful-hugo && hugo --minify "
}
}
在此示例中,当您运行npm run dev
时,它将首先使用 contentful-hugo 来提取 Contentful 数据,然后启动 Hugo 服务器。以同样的方式,当您执行命令npm run build
时,它将首先使用 contentful-hugo 来提取 Contentful 数据,然后运行hugo --minify
来构建 Hugo 站点的缩小版本。
在完成配置之前尝试使用此包将在控制台中返回错误
Error: There is an error in your config file, or it does ' t exits.
Check your config for errors or run "contentful-hugo --init" to create a config file.
默认情况下,该库将查找以下环境变量。您还可以使用配置文件覆盖这些值。 (参见配置)
.env 文件:
要声明这些环境变量,请在项目的根目录中创建一个.env
文件。
CONTENTFUL_SPACE = ' <space-id> '
CONTENTFUL_TOKEN = ' <content-accessToken> '
# optional but required for preview mode
CONTENTFUL_PREVIEW_TOKEN = ' <preview-accessToken> '
也可以在命令行中声明环境变量
电源外壳:
$ env: CONTENTFUL_SPACE = " <contentful_space_id> "
$ env: CONTENTFUL_TOKEN = " <contentful_acessToken> "
$ env: CONTENTFUL_PREVIEW_TOKEN = " <contentful_preview_accessToken> "
重击:
export CONTENTFUL_SPACE= " <contentful_space_id> "
export CONTENTFUL_TOKEN= " <contentful_accessToken> "
export CONTENTFUL_PREVIEW_TOKEN= " <contentful_preview_accessToken> "
在开始之前,您需要在存储库的根目录中创建一个配置文件。 Contentful-hugo 默认情况下会搜索以下文件作为配置。
contentful-hugo.config.ts
contentful-hugo.config.js
contentful-hugo.config.yaml
contentful-hugo.yaml
contentful-settings.yaml
您还可以使用--config
标志指定自定义配置文件。 (Javascript 或 YAML 配置文件是当前唯一接受的文件类型)
import { defineConfig } from 'contentful-hugo' ;
export default defineConfig ( {
// fetches from default locale if left blank
locales : [ 'en-US' , 'fr-FR' ] ,
contentful : {
// defaults to CONTENTFUL_SPACE env variable
space : 'space-id' ,
// defaults to CONTENTFUL_TOKEN env variable
token : 'content-deliver-token' ,
// defaults to CONTENTFUL_PREVIEW_TOKEN env variable
previewToken : 'content-preview-token' ,
// defaults to "master"
environment : 'master' ,
} ,
singleTypes : [
{
id : 'homepage' ,
directory : 'content' ,
fileName : '_index' ,
} ,
{
id : 'siteSettings' ,
directory : 'data' ,
fileName : 'settings' ,
fileExtension : 'yaml' , // default is md
} ,
] ,
repeatableTypes : [
{
id : 'posts' ,
directory : 'content/posts' ,
mainContent : 'content' ,
resolveEntries : {
categories : 'fields.slug' ,
author : 'fields.name' ,
relatedPosts : 'sys.id' ,
} ,
} ,
{
id : 'seoFields' ,
isHeadless : true ,
directory : 'content/seo-fields' ,
customFields : {
// these fields will be added to the frontmatter
myCustomField : 'myCustomFieldVal' ,
myOtherCustomField : ( entry ) => {
return entry . fields . whatever ;
} ,
} ,
} ,
{
id : 'reviews' ,
directory : 'content/reviews' ,
mainContent : 'reviewBody' ,
} ,
{
id : 'category' ,
directory : 'content/categories' ,
isTaxonomy : true ,
} ,
] ,
staticContent : [
{
inputDir : 'static_content' ,
outputDir : 'content' ,
} ,
] ,
} ) ;
javascript 配置与 typescript 配置几乎相同。
import { defineConfig } from 'contentful-hugo' ;
export default defineConfig ( {
// stuff goes here
} ) ;
CommonJS 语法也应该有效(我实际上没有对此进行测试,也许它有效,也许它不起作用)
const { defineConfig } = require ( 'contentful-hugo' ) ;
module . exports = defineConfig ( {
// stuff goes here
} ) ;
# contentful-hugo.config.yaml
locales : # fetches from default locale if left blank
- en-US
- fr-FR
contentful :
space : ' space-id ' # defaults to CONTENTFUL_SPACE env variable
token : ' content-deliver-token ' # defaults to CONTENTFUL_TOKEN env variable
previewToken : ' content-preview-token ' # defaults to CONTENTFUL_PREVIEW_TOKEN env variable
environment : ' master ' # defaults to "master"
singleTypes :
# fetches only the most recently updated entry in a particular content type
# Generated file will be named after the fileName setting
- id : homepage
directory : content
fileName : _index
- id : siteSettings
directory : data
fileName : settings
fileExtension : yaml # default is md
repeatableTypes :
# fetches all the entries of a content type and places them in a directory.
# Generated files will be named after their Entry ID in Contentful.
- id : posts
directory : content/posts
fileExtension : md
mainContent : content
resolveEntries : # resolves a reference or asset field to a specific property
categories : fields.slug
author : fields.name
relatedPosts : sys.id
- id : seoFields
isHeadless : true
directory : content/seo-fields
customFields :
# will be added to the frontmatter
myCustomFields : ' myCustomFieldValue '
- id : reviews
directory : content/reviews
mainContent : reviewBody
- id : staff
isHeadless : true
directory : content/staff
- id : category
directory : content/categories
isTaxonomy : true
场地 | 必需的 | 描述 |
---|---|---|
空间 | 选修的 | 内容空间 ID(如果未设置,则默认为 CONTENTFUL_SPACE 环境变量) |
代币 | 选修的 | 内容交付令牌(如果未设置,则默认为 CONTENTFUL_TOKEN 环境变量) |
预览令牌 | 选修的 | 内容预览令牌(如果未设置,则默认为 CONTENTFUL_PREVIEW_TOKEN 环境变量) |
环境 | 选修的 | 内容丰富的环境 ID(如果未设置,则默认为“master”) |
场地 | 必需的 | 描述 |
---|---|---|
ID | 必需的 | 内容内容类型 ID |
目录 | 必需的 | 您想要生成文件的目录 |
文件名 | 必需的 | 生成的文件名 |
文件扩展名 | 选修的 | 可以是“md”、“yml”、“yaml”或“json”(默认为“md”) |
主要内容 | 选修的 | 您想要作为主要 Markdown 内容的字段的字段 ID。 (可以是 markdown、richtext 或字符串字段) |
类型 | 选修的 | 手动设置 frontmatter 中“type”字段的值(请参阅hugo 文档) |
解析条目 | 选修的 | 将指定的引用字段和/或资产字段解析为其属性参数之一 |
覆盖 | 选修的 | 对字段值或字段名称进行自定义覆盖 |
过滤器 | 选修的 | 接受内容搜索参数的对象来过滤结果。查看内容丰富的文档 |
忽略语言环境 | 选修的 | 忽略本地化设置,仅从默认区域设置中提取(默认为 false) |
自定义字段 | 选修的 | 接受字段和值的对象。这些值可以是标准静态值,也可以是接受 Contentful 条目作为参数并返回值的函数 |
场地 | 必需的 | 描述 |
---|---|---|
ID | 必需的 | 内容内容类型 ID |
目录 | 必需的 | 您想要生成文件的目录 |
文件名 | 选修的 | 将指示文件名的条目属性。 (默认情况下,这将是sys.id ) |
文件扩展名 | 选修的 | 可以是“md”、“yml”、“yaml”或“json”(默认为“md”) |
是无头的 | 选修的 | 将内容类型中的所有条目转换为无头叶束(请参阅hugo 文档)。当 isTaxonomy 设置为 true 时,无法设置为 true。 |
isTaxonomy(实验) | 选修的 | 在文件结构中组织条目,允许自定义分类元数据(请参阅hugo 文档)。当 isHeadless 设置为 true 时,无法设置为 true。 |
主要内容 | 选修的 | 您想要作为主要 Markdown 内容的字段的字段 ID。 (可以是 markdown、richtext 或字符串字段) |
类型 | 选修的 | 手动设置 frontmatter 中“type”字段的值(请参阅hugo 文档) |
解析条目 | 选修的 | 将指定的引用字段和/或资产字段解析为其属性之一 |
覆盖 | 选修的 | 对字段值或字段名称进行自定义覆盖 |
过滤器 | 选修的 | 接受内容搜索参数的对象来过滤结果。查看内容丰富的文档 |
忽略语言环境 | 选修的 | 忽略本地化设置,仅从默认区域设置中提取(默认为 false) |
自定义字段 | 选修的 | 接受字段和值的对象。这些值可以是标准静态值,也可以是接受 Contentful 条目作为参数并返回值的函数 |
该配置还有一个locales
设置字段,允许您指定要从中提取的区域设置。该字段可以采用字符串数组、对象数组或组合。
默认情况下,区域设置特定的文件扩展名将用于多个翻译。
const { defineConfig } = require ( 'contentful-hugo' ) ;
// produce en-us.md and fr-fr.md files
module . exports = defineConfig ( {
locales : [ 'en-US' , 'fr-FR' ] ;
// rest of config
} )
// produce en.md and fr.md files
module . exports = defineConfig ( {
locales : [
{
code : 'en-US' ,
mapTo : 'en'
} ,
{
code : 'fr-FR' ,
mapTo : 'fr'
}
]
// rest of config
} )
// produce en-us.md files and fr.md files
module . exports = defineConfig ( {
locales : [
'en-US' ,
{
code : 'fr-FR' ,
mapTo : 'fr'
}
]
// rest of config
} )
在 Contentful Hugo 中配置区域设置后,您将需要更新 Hugo 配置以考虑这些区域设置。有关更多详细信息,请参阅 Hugo 文档。
# config.toml
[ languages ]
[ languages . en-us ]
# language settings
[ languages . fr-fr ]
# language settings
有时,您希望根据区域设置将内容放置在目录中,而不是使用基于文件扩展名的翻译。为此,您只需在目录文件路径中包含[locale]
即可。
当使用特定于语言环境的目录时,特定于语言环境的文件扩展名(即en.md
或fr.md
)会被删除
const { defineConfig } = require ( 'contentful-hugo' ) ;
module . exports = defineConfig ( {
locales : [ 'en' , 'fr' ]
singleTypes : [
{
id : 'settings' ,
fileName : 'settings' ,
fileExtension : 'yaml' ,
directory : 'data/[locale]'
/*
produces:
- data/en/settings.yaml
- data/fr/settings.yaml
*/
}
]
repeatableTypes : [
{
id : 'post' ,
directory : 'content/[locale]/post' ,
/*
produces:
- content/en/post/[entryId].md
- content/fr/post/[entryId].md
*/
} ,
] ,
} ) ;
Contentful Hugo 的建议设置是在版本控制中忽略内容(通常为./content
)和数据(通常为./data
)目录。这是因为 contentful-hugo 将在构建时生成这些目录。但是,如果您的页面未在 Contentful 中管理并且不是在构建时由其他源生成的,这会带来麻烦。
为了处理这个问题,Contentful-Hugo 有一个staticContent
参数。此参数接受可提交到 git 的输入目录 ( inputDir
) 和标准内容或数据目录的输出目录 ( outputDir
)。 inputDir 中的所有项目都将在构建时复制到 outputDir 中,并保留其文件夹结构。abs
例如,在下面的配置中./static_content/posts/my-post.md
将被复制到./content/posts/my-post.md
,并且./static_data/global-settings.yaml
将被复制到./data/global-settings.yaml
。
const { defineConfig } = require ( 'contentful-hugo' ) ;
module . exports = defineConfig ( {
// rest of config
staticContent : [
{
// all items in ./static_content will be copied to ./content
inputDir : 'static_content' ,
outputDir : 'content' ,
} ,
{
// all items in ./static_data will be copied to ./data
inputDir : 'static_data' ,
outputDir : 'data' ,
} ,
] ,
} ) ;
在服务器模式下运行时,Contentful-Hugo 还将监视 inputDir 中的文件更改。
以下是根据任意条件动态更改token
、 previewToken
和environment
选项的示例。
// contentful-hugo.config.js
const { defineConfig } = require ( 'contentful-hugo' ) ;
require ( 'dotenv' ) . config ( ) ; // assuming you have "dotenv" in your dependencies
const myMasterToken = process . env . CONTENTFUL_MASTER_TOKEN ;
const myMasterPreviewToken = process . env . CONTENTFUL_MASTER_PREVIEW_TOKEN ;
const myStagingToken = process . env . CONTENTFUL_STAGING_TOKEN ;
const myStagingPreviewToken = process . env . CONTENTFUL_STAGING_PREVIEW_TOKEN ;
// set some condition
const isStaging = true || false ;
module . exports = defineConfig ( {
contentful : {
space : 'my-space-id' ,
token : isStaging ? myStagingToken : myMasterToken ,
preview : isStaging ? myStagingPreviewToken : myMasterPreviewToken ,
environment : isStaging ? 'staging' : 'master' ,
} ,
// rest of config
} ) ;
const { defineConfig } = require ( 'contentful-hugo' ) ;
// contentful-hugo.config.js
module . exports = defineConfig ( {
repeatableTypes : [
{
id : "trips" ,
directory : "content/trips"
overrides : {
// change the url field name to "slug"
url : {
fieldName : "slug"
}
/*
rename "distanceInKilometers" to "distanceInMiles"
and change the field value from km to mi
*/
distanceInKilometers : {
fieldName : "distanceInMiles" , valueTransformer : ( val ) => {
if ( typeof val === 'number' ) {
return val * 0.621371
}
return 0
}
}
}
}
]
} )
// ALTERNATIVE SYNTAX
module . exports = defineConfig ( {
repeatableTypes : [
{
id : "trips" ,
directory : "content/trips"
overrides : [
{
field : "url" ,
options : {
// change the url field name to "slug" in frontmatter
fieldName : "slug"
}
} ,
{
field : "distanceInKilometers" ,
options : {
// rename "distanceInKilometers" to "distanceInMiles"
fieldName : "distanceInMiles" ,
// convert distance to miles and output the result in frontmatter
valueTransformer : ( val ) => {
if ( typeof val === 'number' ) {
return val * 0.621371
}
return 0
}
}
} ]
}
]
} )
对于 JS 配置文件,您可以使用defineConfig
帮助器,也可以导入ContentfulHugoConfig
类型。
//////////// OPTION 1 ////////////
const { defineConfig } = require ( 'contentful-hugo' ) ;
module . exports = defineConfig ( {
// config goes here
} ) ;
//////////// OPTION 2 ////////////
/**
* @type {import('contentful-hugo').ContentfulHugoConfig}
*/
module . exports = {
// rest of config
} ;
.gitignore
设置示例
# general stuff
.env
node_modules
public
resources
# Contenful Hugo stuff
# temp folder that contentful uses to track files
.contentful-hugo
# since content and data is coming from Contentful
# usually you'll want to ignore those directories
content
data
文件将在配置文件中指定的目录中生成。前面的内容将采用 YAML 格式。单一类型的文件将以配置文件中指定的文件名命名。可重复类型的文件将以其在 Contenful 中的条目 ID 命名,这样可以轻松地将文件链接在一起。
以下字段将始终出现在您的 frontmatter 中:
date : # defaults to sys.createdAt unless you have a field with the id "date" then it get's overwritten
sys :
id : # the entry id
updatedAt : # the last time this entry was updated in Contentful
createdAt : # when the entry was created in Contentful
revision : # the revision number
space : # the space id
contentType : # the content type id
图像和视频等资源附带一些额外信息,可以轻松实现替代文本或布局等依赖于了解图像尺寸的内容。字段如下:
assetFieldName :
assetType : # indicates the asset mime type such as image/png, video/mp4, audio/mp3, ect.
url : # url of the asset
title : # title of the asset written in Contentful
description : # description of the asset written in Contentful
width : # width of the asset (images only)
height : # height of the asset (images only)
如果您使用 Hugo,您可以访问如下信息:
< img
src =" {{ .Params.assetFieldName.url }} "
width =" {{ .Params.assetFieldName.width }} "
/>
对于图像,您可以将参数附加到资产 URL 中,以便使用 Contentful 的图像 api
< img
src =" {{ .Params.assetFieldname.url }}?w=200&h=200&fit=fill "
width =" 200 "
height =" 200 "
/>
同样的信息也将出现在资产数组中,例如画廊:
myGallery :
- assetType : ' image/jpg '
url : ' //link-to-image.jpg '
title : ' Image 1 '
description : ' Image 1 Description '
width : 500
height : 500
- assetType : ' image/jpg '
url : ' //link-to-image-2.jpg '
title : ' Image 2 '
description : ' Image 2 Description '
width : 1920
height : 1080
链接条目将包含其 id 和内容类型 id 字段。
linkedEntry :
id : <contentful-entry-id>
typeId : <content-type-ID>
# example with array of linked entries
relatedArticles :
- id : ' 41UFfIhszbS1kh95bomMj7 '
typeId : ' articles '
- id : ' 85UFfIhsacS1kh71bpqMj7 '
typeId : ' articles '
所有文件均以其在 Contentful 中的条目 ID 命名,从而可以使用 Hugo 中的.Site.GetPage
轻松检索它
// if you have access to the "Page" object
{{ with . Site . GetPage "<path-to-file>/<entry-id>" }}
{{ . Title }}
{{ end }}
// if you don't have access to the "Page" object
// for example in a nested partial
{{ with site. GetPage "<path-to-file>/<entry-id>" }}
{{ . Title }}
{{ end }}
简单的例子
{{ with . Params . myEntryField }}
{{ $ pagePage := print "path/to/entryDir/" . id }}
{{ with site. GetPage $ pagePath }}
{{ . Title }}
{{ . Params . someOtherField }}
{{ end }}
{{ end }}
相关文档:
设置为内容类型的“mainContent”的富文本字段将呈现为 Hugo 的 markdown。
诸如embedded-entry-blocks
之类的动态内容被呈现为包含可用于获取必要数据的参数的短代码。
<!-- example embedded entry -->
<!-- you can use the id, contentType, and parentContentType parameters to fetch the desired data -->
{{< contentful-hugo/embedded-entry id="nTLo2ffSJJp5QrnrO5IU9" contentType="gallery" parentContentType="post" >}}
在获取富文本数据之前,请确保您已运行contentful-hugo --init
,以便您将拥有所有富文本短代码。获得这些短代码后,您可以扩展和修改它们以满足您的需求。
富文本短代码列表包括:
默认情况下,富文本短代码将显示未配置项目的通知。
您可以通过导航到layouts/shortcodes/contentful-hugo/{shortcode-name}.html
来自定义它们
富文本字段将生成反映 API 中 JSON 结构的嵌套数组。每个节点都需要循环并根据 nodeType 字段生成 HTML。
richTextField :
- nodeType : ' paragraph '
data : {}
content :
- data : {}
marks : []
value : ' This is a simple paragraph. '
nodeType : ' text '
- nodeType : ' paragraph '
data : {}
content :
- data : {}
marks : []
value : ' This is a paragraph with '
nodeType : ' text '
- data : {}
marks :
- type : ' italic '
value : ' italicized text. '
nodeType : ' text '
- nodeType : ' embedded-asset-block '
data :
assetType : ' image/jpeg '
url : ' //images.ctfassets.net/some-image-url.jpg '
title : ' Image title will appear here '
description : ' Image description will appear here '
width : 1920
height : 1080
content : []
此外,将使用附加“_plaintext”的字段 ID 生成该字段的纯文本版本。这使您可以快速获取文本本身,而无需任何其他数据。一个简单的用例是使用纯文本输出自动生成网页的元描述。
richTextField_plaintext : ' This is a simple paragraph. This is a paragraph with italicized text. '
解析条目选项允许您指定引用条目或资产中的属性以解析该字段值。例如,假设您有一个在posts
中引用的category
内容类型。通常 contentful-hugo 会给出以下结果
category :
id : some-entry-id
contentType : category
虽然这可以很容易地找到类别,但这种格式不允许您使用 Hugo 的内置分类功能。使用resolveEntries
参数可以解决这个问题。
// from the config file
module . exports = defineConfig ( {
repeatableTypes : [
id : 'posts' ,
directory : 'content/posts' ,
resolveEntries : {
category : 'fields.slug'
}
// alternative syntax
resolveEntries : [
{
field : 'category' ,
resolveTo : 'fields.slug' ,
} ,
] ,
]
} )
现在,类别字段将仅将 slug 显示为值。
category : my-category-slug
解析条目功能适用于参考字段和资产字段,以及多个参考和多个资产字段。
覆盖可用于修改字段名称和字段值。
这是将字段名称从“url”更改为“videoUrl”的简单示例
repeatableTypes: [
{
id : 'youtubeVideo' ,
directory : 'content/_youtubeVideo' ,
isHeadless : true ,
overrides : {
// the name of the url field to videoUrl
url : {
fieldName : 'videoUrl'
}
}
// alternative syntax
overrides : [
{
field : 'url' ,
options : {
fieldName : 'videoUrl' ,
} ,
} ,
] ,
} ,
] ;
overrides
还有一个valueTransformer
选项,允许您操作将出现在 frontmatter 中的字段数据。 valueTransformer
采用一个以字段值作为参数的方法,然后返回将出现在 frontmatter 中的最终结果。 (请注意,由于valueTransformer
必须是一个方法,因此该选项仅适用于 javascript 配置文件)
下面是一个示例,我们将字段名称从“url”更改为“videoId”,然后使用valueTransformer
从 url 中提取视频 id,然后将其放在 frontmatter 中。
repeatableTypes: [
{
id : 'youtubeVideo' ,
directory : 'content/_youtubeVideo' ,
isHeadless : true ,
overrides : {
url : {
fieldName : 'videoId' ,
// "value" is whatever value is currently saved in the field.
// in this case it's a url for a youtube video
valueTransformer : ( value ) => {
if ( ! value ) {
return null ;
}
const url = new URL ( value ) ;
// extract the video id from the url and return it
return url . searchParams . get ( 'v' ) ;
}
}
}
// alternative syntax
overrides : [
{
field : 'url' ,
options : {
fieldName : 'videoId' ,
valueTransformer : ( value ) => {
// transform the value
} ,
} ,
} ,
] ,
} ,
] ;
在包含数组的字段上使用valueTransformer
选项时,请确保在操作值时循环遍历该值。
repeatabledTypes: [
{
id : 'post' ,
directory : 'content/posts' ,
overrides : {
authors : {
valueTransformer : ( authorRefs ) => {
const authors = [ ] ;
for ( const ref of authorRefs ) {
// get the name, photo, and bio of the author
// and add it to the array
authors . push ( {
name : ref . fields . name ,
photo : ref . fields . photo . fields . file . url ,
bio : ref . fields . bio ,
} ) ;
}
return authors ;
} ,
} ,
} ,
} ,
] ;
现在, authors
字段将如下所示:
authors :
- name : Some Name
photo : //images.cfassets.net/path-to-photo.jpg
bio : some bio text
- name : Some other name
photo : //images.cfassets.net/path-to-photo.jpg
bio : some other bio text
正如您所看到的,这可以用来生成与resolveEntries
参数类似的结果,但是resolveEntries
只能返回一个属性,而通过覆盖,您可以对字段值执行任何您想要的操作。
您可以使用filters
选项来输入搜索参数,从而允许您根据条目的某些属性来过滤条目。有关内容搜索参数的更多信息,请访问他们的文档。
请注意,以下搜索参数将被忽略content_type
、 skip
、 order
、 limit
module . exports = defineConfig ( {
singleTypes : [
// get a homepage with a specific entryId
{
id : 'homepage' ,
directory : 'content' ,
fileName : '_index' ,
filters : {
'sys.id' : 'my-homepace-id'
}
}
]
repeatableTypes : [
// only get events that start after 01/01/2020
{
id : 'events' ,
directory : 'content/events' ,
filters : {
'fields.startDate[gte]' : '2020-01-01T00:00:00Z' ,
} ,
} ,
// get posts where author is "John Doe" and contains the tag "flowers"
{
id : 'posts' ,
directory : 'content/posts' ,
filters : {
'fields.author' : 'John Doe' ,
'fields.tags' : 'flowers'
} ,
} ,
] ;
} )
您可以使用customFields
参数向条目添加其他字段。自定义字段的配置可以是静态值,也可以是接受 Contentful 条目作为参数并返回值的方法。
假设我们有一个包含以下字段的作者内容类型:
这是一个示例配置:
module . exports = defineConfig ( {
// rest of config
repeatableTypes : [
{
id : 'author' ,
directory : 'content/authors' ,
customFields : {
// both "myCustomField" and "fullName"
// will be appended to the frontmatter for author entries
myCustomField : 'myCustomFieldValue' ,
fullName : ( entry ) => {
const { firstName , lastName } = entry . fields ;
return ` ${ firstName } ${ lastName } ` ;
} ,
} ,
} ,
] ,
} ) ;
这是该配置将导致的结果
---
firstName : ' John '
lastName : ' Doe '
slug : ' john-doe '
myCustomField : ' myCustomFieldValue ' # custom field
fullName : ' John Doe ' # custom field
---
您还可以将其用于 Hugo 特定字段,例如构建选项
// prevent a content type from appearing in list pages
{
customFields : {
_build : {
render : 'alway' ,
list : 'never' ,
publishResources : true
}
}
}
// prevent a content type from rendering a single page
{
customFields : {
_build : {
render : 'never' ,
list : 'always' ,
publishResources : true
}
}
}
这些是一些已知问题。
--wait
标志添加到脚本中。在下面的示例中,我们额外等待 6 秒contentful-hugo --wait=6000
。hugo server --disableFastRender