Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.

Commit 629c4a6

Browse files
saltysugaregoist
authored andcommitted
feat: refactor page interface (#345)
* set front matter under page instead of page.attributes * replace the term "page attributes" with "page data" * Revert "replace the term "page attributes" with "page data"" This reverts commit 022e71c. * Revert "Revert "replace the term "page attributes" with "page data""" This reverts commit 386c74c. * fix BC break for page.attributes * Update docs * Update docs * Update docs
1 parent 0bd9854 commit 629c4a6

File tree

44 files changed

+209
-243
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+209
-243
lines changed

packages/saber-plugin-feed/lib/index.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,30 +51,26 @@ exports.apply = (api, options = {}) => {
5151

5252
await Promise.all(
5353
[...api.pages.values()].map(async page => {
54-
if (page.attributes.type !== 'post' || page.attributes.draft) {
54+
if (page.type !== 'post' || page.draft) {
5555
return
5656
}
5757

58-
const matchedLocalePath = api.pages.getMatchedLocalePath(
59-
page.attributes.permalink
60-
)
58+
const matchedLocalePath = api.pages.getMatchedLocalePath(page.permalink)
6159
if (localePath !== matchedLocalePath) {
6260
return
6361
}
6462

65-
const content = await api.renderer.renderPageContent(
66-
page.attributes.permalink
67-
)
63+
const content = await api.renderer.renderPageContent(page.permalink)
6864
posts.push({
69-
title: page.attributes.title,
70-
id: page.attributes.permalink,
71-
link: resolveURL(siteConfig.url, page.attributes.permalink),
65+
title: page.title,
66+
id: page.permalink,
67+
link: resolveURL(siteConfig.url, page.permalink),
7268
// Strip HTML tags in excerpt and use it as description (a.k.a. summary)
7369
description:
7470
page.excerpt && page.excerpt.replace(/<(?:.|\n)*?>/gm, ''),
7571
content,
76-
date: page.attributes.updatedAt,
77-
published: page.attributes.createdAt
72+
date: page.updatedAt,
73+
published: page.createdAt
7874
})
7975
})
8076
)

packages/saber-plugin-git-modification-time/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# saber-plugin-git-modification-time
22

3-
> Use the author time of the last commit as page.attributes.updatedAt
3+
> Use the author time of the last commit as `page.updatedAt`
44
55
Why? See [#9785](https://github.com/gatsbyjs/gatsby/issues/9785).
66

7-
**tl;dr** `page.attributes.updatedAt` defaults to `file.mtime` which will change on platforms like Netlify. We restore the value to author time of the last commit instead.
7+
**tl;dr** `page.updatedAt` defaults to `file.mtime` which will change on platforms like Netlify. We restore the value to author time of the last commit instead.
88

99
## Install
1010

packages/saber-plugin-git-modification-time/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ exports.apply = api => {
1515
page.internal.absolute
1616
])
1717
if (stdout) {
18-
page.attributes.updatedAt = new Date(stdout)
18+
page.updatedAt = new Date(stdout)
1919
}
2020
}
2121
})

packages/saber-plugin-git-modification-time/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "saber-plugin-git-modification-time",
33
"version": "0.0.3",
4-
"description": "Use the author time of the last commit as page.attributes.updatedAt",
4+
"description": "Use the author time of the last commit as page.updatedAt",
55
"license": "MIT",
66
"main": "lib/index.js",
77
"files": [

packages/saber-plugin-netlify-redirect/lib/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ exports.apply = api => {
3838
}
3939
}
4040

41-
const allPermalinks = [...api.pages.values()].map(
42-
page => page.attributes.permalink
43-
)
41+
const allPermalinks = [...api.pages.values()].map(page => page.permalink)
4442
for (const permalink of allPermalinks) {
4543
if (permalink.endsWith('.html')) {
4644
const fromPath = permalink.replace(/\.html$/, '')

packages/saber-plugin-query-posts/README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ Then in the layout component `layouts/index.vue`, `page.posts` and `page.paginat
3636
<slot name="default" />
3737
3838
<ul>
39-
<li v-for="post in page.posts" :key="post.attributes.permalink">
40-
<saber-link :to="post.attributes.permalink">
41-
{{ post.attributes.title }}
39+
<li v-for="post in page.posts" :key="post.permalink">
40+
<saber-link :to="post.permalink">
41+
{{ post.title }}
4242
</saber-link>
4343
</li>
4444
</ul>
@@ -49,7 +49,6 @@ Then in the layout component `layouts/index.vue`, `page.posts` and `page.paginat
4949
<saber-link :to="page.pagination.nextLink" v-if="page.pagination.hasNext">
5050
Next Page →
5151
</saber-link>
52-
5352
</div>
5453
</template>
5554
@@ -62,7 +61,7 @@ export default {
6261

6362
## Tags
6463

65-
This plugin will automatically generate tag pages at `/tags/:tag` when you're using `tags` in page attributes, e.g. in a Markdown post:
64+
This plugin will automatically generate tag pages at `/tags/:tag` when you're using `tags` in page data, e.g. in a Markdown post:
6665

6766
```markdown
6867
---
@@ -109,7 +108,7 @@ You can access the tag name in the layout component via `this.page.tag`.
109108

110109
## Categories
111110

112-
This plugin will automatically generate category pages at `/categories/:tag` when you're using `categories` in page attributes, e.g. in a Markdown post:
111+
This plugin will automatically generate category pages at `/categories/:tag` when you're using `categories` in page data, e.g. in a Markdown post:
113112

114113
```markdown
115114
---

packages/saber-plugin-query-posts/lib/index.js

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,31 @@ exports.apply = (api, options = {}) => {
5151
categoriesMap = Object.assign({}, categoriesMap)
5252

5353
for (const page of api.pages.values()) {
54-
if (page.attributes.draft) {
54+
if (page.draft) {
5555
continue
5656
}
5757

58-
const matchedLocalePath = api.pages.getMatchedLocalePath(
59-
page.attributes.permalink
60-
)
58+
const matchedLocalePath = api.pages.getMatchedLocalePath(page.permalink)
6159
if (matchedLocalePath !== currentLocalePath) {
6260
continue
6361
}
6462

65-
if (page.attributes.injectAllPosts) {
63+
if (page.injectAllPosts) {
6664
injectPostsToPages.add(page)
6765
continue
6866
}
6967

70-
if (page.attributes.type === 'post') {
68+
if (page.type === 'post') {
7169
const pagePublicFields = api.pages.getPagePublicFields(page)
7270
allPosts.add(pagePublicFields)
7371

7472
// Group posts for tag pages
75-
const tags = [].concat(page.attributes.tags || [])
73+
const tags = [].concat(page.tags || [])
7674
if (tags.length > 0) {
77-
page.tags = []
75+
page.tagsInfo = []
7876
for (const tag of tags) {
7977
const tagSlug = getIdFromMap(tagsMap, tag)
80-
page.tags.push({
78+
page.tagsInfo.push({
8179
name: tag,
8280
permalink: renderPermalink(permalinks.tag, {
8381
name: tagSlug,
@@ -92,18 +90,18 @@ exports.apply = (api, options = {}) => {
9290

9391
// Group posts for category pages
9492
const categories = []
95-
.concat(page.attributes.categories || [])
93+
.concat(page.categories || [])
9694
.map(v => (Array.isArray(v) ? v : v.split('/')))
9795

9896
if (categories.length > 0) {
99-
page.categories = []
97+
page.categoriesInfo = []
10098
for (const category of categories) {
10199
for (const index of category.keys()) {
102100
const categorySlug = category
103101
.slice(0, index + 1)
104102
.map(name => getIdFromMap(categoriesMap, name))
105103
.join('/')
106-
page.categories.push({
104+
page.categoriesInfo.push({
107105
// The base name of the category
108106
name: category[index],
109107
permalink: renderPermalink(permalinks.category, {
@@ -130,15 +128,13 @@ exports.apply = (api, options = {}) => {
130128
injectToPages(
131129
new Set([
132130
{
133-
attributes: {
134-
isTagPage: true,
135-
layout: 'tag',
136-
permalink: renderPermalink(permalinks.tag, {
137-
name: tag,
138-
slug: tag
139-
}),
131+
isTagPage: true,
132+
layout: 'tag',
133+
permalink: renderPermalink(permalinks.tag, {
134+
name: tag,
140135
slug: tag
141-
},
136+
}),
137+
slug: tag,
142138
internal: {
143139
id: `internal_blog__tag__${tag}`,
144140
// So that this page will be removed before next `onCreatePages` hook in watch mode
@@ -158,15 +154,13 @@ exports.apply = (api, options = {}) => {
158154
injectToPages(
159155
new Set([
160156
{
161-
attributes: {
162-
isCategoryPage: true,
163-
layout: 'category',
164-
permalink: renderPermalink(permalinks.category, {
165-
name: category,
166-
slug: category
167-
}),
157+
isCategoryPage: true,
158+
layout: 'category',
159+
permalink: renderPermalink(permalinks.category, {
160+
name: category,
168161
slug: category
169-
},
162+
}),
163+
slug: category,
170164
internal: {
171165
id: `internal_blog__category__${category}`,
172166
// So that this page will be removed before next `onCreatePages` hook in watch mode
@@ -188,23 +182,23 @@ exports.apply = (api, options = {}) => {
188182
if (pages.size > 0) {
189183
const date = new Date()
190184
const sortedPosts = [...posts].sort((a, b) => {
191-
const aDate = new Date(a.attributes.date || a.attributes.createdAt)
192-
const bDate = new Date(b.attributes.date || b.attributes.createdAt)
185+
const aDate = new Date(a.createdAt)
186+
const bDate = new Date(b.createdAt)
193187
return aDate > bDate ? -1 : 1
194188
})
195189

196190
for (const page of pages) {
197191
const paginatedPosts = paginate(
198192
sortedPosts,
199-
Object.assign({}, paginationOptions, page.attributes.injectAllPosts)
193+
Object.assign({}, paginationOptions, page.injectAllPosts)
200194
)
201195
const totalPages = paginatedPosts.length
202196

203197
for (const [index, posts] of paginatedPosts.entries()) {
204198
const permalink =
205199
index === 0
206-
? page.attributes.permalink
207-
: urlJoin(page.attributes.permalink, `page/${index + 1}`)
200+
? page.permalink
201+
: urlJoin(page.permalink, `page/${index + 1}`)
208202
const newPage = Object.assign({}, page, {
209203
internal: Object.assign({}, page.internal, {
210204
id:
@@ -215,22 +209,17 @@ exports.apply = (api, options = {}) => {
215209
page.internal.parent ||
216210
(index === 0 ? undefined : page.internal.id)
217211
}),
218-
attributes: Object.assign({}, page.attributes, {
219-
permalink,
220-
createdAt: page.attributes.createdAt || date,
221-
updatedAt: page.attributes.updatedAt || date
222-
}),
212+
permalink,
213+
createdAt: page.createdAt || date,
214+
updatedAt: page.updatedAt || date,
223215
posts,
224216
pagination: {
225217
hasPrev: index !== totalPages - 1,
226218
hasNext: index !== 0,
227219
total: totalPages,
228220
current: index + 1,
229-
prevLink: getPaginationLink(
230-
index + 2,
231-
page.attributes.permalink
232-
),
233-
nextLink: getPaginationLink(index, page.attributes.permalink)
221+
prevLink: getPaginationLink(index + 2, page.permalink),
222+
nextLink: getPaginationLink(index, page.permalink)
234223
}
235224
})
236225
Object.assign(newPage, pageProp)

packages/saber-plugin-transformer-html/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ exports.apply = api => {
1010
page.content
1111
)
1212
const { html, blocks } = extractSFCBlocks(body)
13+
Object.assign(page, frontmatter)
1314
page.content = html
1415
page.internal.hoistedTags = blocks
15-
Object.assign(page.attributes, frontmatter)
1616
},
1717
getPageComponent(page) {
1818
return `<template>

packages/saber-plugin-transformer-pug/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ exports.apply = api => {
2121
basedir: dirname
2222
})
2323
const { html: pageContent, blocks } = extractSFCBlocks(html)
24+
Object.assign(page, frontmatter)
2425
page.content = pageContent
2526
page.internal.hoistedTags = blocks
26-
Object.assign(page.attributes, frontmatter)
2727
},
2828
getPageComponent(page) {
2929
return `<template>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
exports.onCreatePage = function(page) {
2-
if (page.attributes.permalink === '/') {
3-
page.attributes.foo = 'ass'
2+
if (page.permalink === '/') {
3+
page.foo = 'ass'
44
}
55
}

0 commit comments

Comments
 (0)