我正在使用Gatsbyjs和NetlifyCMS构建一个网站.我已经开始使用这个启动器https://github.com/AustinGreen/gatsby-starter-netlify-cms,我现在正在尝试自定义它.
我想在markdown文件的frontmatter中使用自定义变量,如下所示:
---
templateKey: mirror
nazev: ?ernobílá
title: Black and White
cena: '2700'
price: '108'
thumbnail: /img/img_1659.jpeg
---
Run Code Online (Sandbox Code Playgroud)
我想用GraphQL访问这些数据.我使用gatsby-source-filesystem和gatsby-transform-remark.这是我的查询:
{
allMarkdownRemark {
edges {
node {
frontmatter {
templateKey
nazev
title
cena
price
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我无法让GraphQL读取我自己的变量,它只识别title
和templateKey
(那些已经在启动器中使用过的).我收到此错误:
{
"errors": [
{
"message": "Cannot query field \"nazev\" on type \"frontmatter_2\".",
"locations": [
{
"line": 7,
"column": 11
}
]
},
{
"message": "Cannot query field \"cena\" on type \"frontmatter_2\".",
"locations": [
{
"line": …
Run Code Online (Sandbox Code Playgroud) 尝试使用数据设置分页,其中{{ title }}
in是projects.json中定义的当前<head><title>{{ title }}</title></head>
页面的标题
假设可以这样做:
# main.njk
<head>
<title>{{ title }}</title>
</head>
Run Code Online (Sandbox Code Playgroud)
# page.njk
---
layout: main.njk
pagination:
data: projects
size: 1
alias: project
permalink: "work/{{ project.title | slug }}/"
title: {{ project.title }}
Run Code Online (Sandbox Code Playgroud)
可能误解了一些基本原理,但{{ title }}
结果却是[object, object]
这样。固定链接工作正常...
我想将YAML前端问题用作通用数据管理器的元数据持有者,例如:
---
layout: "user"
title: "Mario Brega"
slug: "mario-brega"
skills:
- fire
- water
- leaf
---
# Here I will be using Markdown
yes I will, _I swear_
Run Code Online (Sandbox Code Playgroud)
它被许多静态生成器用作:
问题:那有标准吗?我可以轻松验证的一些工具,例如,同意所有字符串值应该用引号括起来,booleans不应该,等等.
将YAML前端映射到JSON模式将是一个很大的优势.
我正在努力为我的Gulp流程添加一些简单的Markdown处理,但是我无法完成这些工作.我似乎错过了获取前端内容和确定应用哪个Nunjuck模板之间的步骤.
这是我的Gulp文件中的部分:
gulp.task('pages:md', function() {
gulp.src('./content/**/*.md')
.pipe(frontMatter({ // optional configuration
property: 'frontMatter', // property added to file object
remove: true // should we remove front-matter header?
}))
.pipe(marked({
// optional : marked options
}))
.pipe(nunjucks({
// ?? Feels like I need to specify which template applies based on the front matter "layout" property?
}))
.pipe(gulp.dest('build/'))
});
Run Code Online (Sandbox Code Playgroud)
markdown文件如下所示:
---
title: Title
layout: layout.html
nav_active: home
---
...markdown content...
Run Code Online (Sandbox Code Playgroud)
我觉得它正朝着正确的方向发展,但是能够直观地了解前端数据的去向,以及如何将它暴露给Nunjucks渲染,目前尚不清楚.有帮助吗?
我们知道Jekyll可以使用像前面这样的前面的物质变量tags
,categories
然后用它们来访问它们site.tags
并site.categories
使用液体迭代它们.现在我的问题是我无法使用像author
(因为site.authors
)这样的自定义前端变量这样做,因为Jekyll不会以列表格式存储它.这使得分页非常困难.
我看过的每个解决方案即
需要我硬编码的作者列表来_config.yml
或其他一些.yml
(即_data/authors.yml
).这里的问题是我没有使用固定的作者列表.作者列表需要更新,当我在服务器运行时输入带有前端标签author: exampleAuthor
或作者列表的另一篇文章(如每个帖子的多个作者,当前只能使用categories
和tags
也可以).它可以很好地处理标签和类别,但不能像作者那样使用自定义标签.
最简单的解决方案是使用一个site.authors
列表进行迭代,然后使用ruby插件扩展它.
我没有找到一个为我提供解决方案的插件,并认为这是一个常见的问题,我可能不是第一个拥有的.
然后,我在写我自己的红宝石插件(这是很难在它自己的,因为缺少文档.也许我是哑巴谷歌,但资源,我发现那里非常有限,根本不足以引导您完成整个过程)但必须有一个原因,为什么这么难做,使得所有现有的解决方案都需要对作者列表进行硬编码.yml
(或者.json
,大多数人.yml
因某种原因而去).
这样做是出于对我的问题,因为我只想在与它的作者姓名职位扔后来和操纵.yml
(我的印象中yml
,一旦启动了服务器,如文件不会被编译_config.yml
,正确我,如果我错了)会适得其反,因为它需要你重新启动服务器来编译它们.
甚至非常高级的插件,如jekyll-paginate-v2(我成功地用标签和类别对帖子进行分页)也没有解决方案,因为这个问题描述了.他被建议滥用类别变量按作者分页,这是一种绝望的解决方法.
我已经找到了可以用集合完成的建议,但这似乎不可行.主要是由于它们的实现性质:
它们需要对作者列表进行硬编码(同样,我不希望这样.我没有固定的作者列表.所有作者信息都必须来自前面 -在/_posts
目录.md
文件中的问题)到目前为止,我看不到如何使用集合.但是我愿意接受建议.
编辑:我在Jekylls github页面上发现了这个过时的问题,其中强调人们正在尝试做同样但无济于事.这在过去4年中是否可行?
我正在尝试在我的gatsby-node.js
文件中构建一个支持可选值的类型。我认为这已经完成了[String!]!
。
gatsby-node.js
如何加载我在内部创建的新类型home.js
?
gatsby-node.js:
const path = require('path');
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
const typeDefs = `
type markdownRemark implements Node {
frontmatter: Features
}
type Features {
title: [String!]!
description: [String!]!
}
`;
createTypes(typeDefs);
};
Run Code Online (Sandbox Code Playgroud)
页面/home/home.js:
export const query = graphql`
query HomeQuery($path: String!) {
markdownRemark(frontmatter: { path: { eq: $path } }) {
html
frontmatter {
features {
title
description
}
}
}
} …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用remarkjs生态系统来解析包含 markdown 和 frontmatter 的文件,并将其转换为 HTML。该文件可能如下所示:
---
title: Title
---
# This is a heading
Run Code Online (Sandbox Code Playgroud)
我设法解析了markdown,这可以按照GitHub README文件中的示例来完成,而且我也知道有一个remark-frontmatter
包。然而,仅仅使用前面提到的示例中的包就可以使解析器完全忽略 frontmatter 部分。当然,这部分不应该包含在 HTML 中,但我仍然想使用它。但据我所知,它不是输出的一部分。这让我想到了我的问题:如何使用这些包访问 frontmatter?我知道它正在过程中的某个地方被解析,但是我如何访问它?
ruby 中是否有某种方法可以编辑 Markdown 文件顶部的 YAML Frontmatter,例如 Jekyll 和 Middleman 中使用的那些?
就像是:
def update_yaml
#magic that changes A: 1 to A: 2 in Frontmatter block
end
Run Code Online (Sandbox Code Playgroud)
然后我的降价文件将从
---
A: 1
---
# Title
Words. More words. This is the words part of the file.
Run Code Online (Sandbox Code Playgroud)
到
---
A: 2
---
# Title
Words. More words. This is the words part of the file.
Run Code Online (Sandbox Code Playgroud)
似乎唯一的选择是解析整个文件,然后重写整个文件,只更改所需的部分,但我希望有更好的方法。
我正在尝试在Jekyll网站上创建一个页面,该页面将显示一个自定义变量并列出包含该自定义变量的帖子。
我使用Thiago Rossener创建的模板创建了电影评论博客:Thiago的模板:https : //github.com/thiagorossener/jekflix-template 我的网站:https ://www.howdareyoureview.com/
在每篇文章中,我都在YAML前端定义了与电影详细信息(例如,演员,导演得分等)相关的自定义变量。
例如:
---
layout: post
title: "Baby Driver"
image: 'https://res.cloudinary.com/how-dare-you-review/image/upload/c_fill,h_399,w_760/v1529865791/baby-driver.png'
tags:
- action
score: 72
director: Edgar Wright
written-by: Edgar Wright
staring:
- Ansel Elgort
- Lily James
- Eiza González
- Jon Hamm
- Jamie Foxx
---
Run Code Online (Sandbox Code Playgroud)
我想创建与该模板中已经存在的标签页面完全相同的页面:https : //www.howdareyoureview.com/tags/
除非我想按导演,主演等进行排序,而不是按标签进行排序。标签页面是使用tags.html文件中的以下代码创建的:
---
layout: minimal
title: "#Tags"
permalink: /tags/index.html
description: "Procure por sua #tag favorita."
---
<div class="tags">
{% assign tags_list = site.tags %}
{% if tags_list.first[0] == null %} …
Run Code Online (Sandbox Code Playgroud) 我是Jekyll的新手,想在Post Frontmatter中创建其他变量:
style:
name: post
img: image_name
Run Code Online (Sandbox Code Playgroud)
当我尝试使用像标题这样的变量时,它可以工作
{% page.title %}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用另一个变量时
{% if page.img %}
{{ page.img }}
{% else %}
No image
{% endif %}
Run Code Online (Sandbox Code Playgroud)
那将返回零.即使只是想输出
{{ page.img }}
Run Code Online (Sandbox Code Playgroud)
知道为什么我不能使用在frontmatter中定义的自定义变量吗?