Skip to content

Commit 48428e0

Browse files
committed
feat: make Gatsby v4 compatible
1 parent d9b6203 commit 48428e0

File tree

6 files changed

+53
-58
lines changed

6 files changed

+53
-58
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
- **Google Docs** formatting options (headings, bullets, tables, images...)
3737
- `MDX` support to use `<ReactComponents />` in your documents
38-
- **Gatsby** v3 & v2 support
38+
- **Gatsby** v3 & v4 support
3939
- `gatsby-plugin-image` and `gatsby-image` support
4040
- Code blocs support
4141
- **Gatsby Cloud** support

utils/create-schema.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports.createSchema = ({actions}) => {
44
type Cover {
55
title: String
66
alt: String
7-
image: File @link(by: "id", from: "image___NODE")
7+
image: File @link
88
}
99
1010
type BreadcrumbItem {
@@ -18,6 +18,8 @@ exports.createSchema = ({actions}) => {
1818
breadcrumb: [BreadcrumbItem!]!
1919
template: String
2020
cover: Cover
21+
related: [GoogleDocs!] @link
22+
images: [File] @link
2123
}
2224
`
2325
createTypes(typeDefs)

utils/on-create-node-markdown-remark.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

utils/on-create-node.js

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
1-
const {onCreateNodeGoogleDocs} = require("./on-create-node-google-docs")
2-
const {onCreateNodeMarkdownRemark} = require("./on-create-node-markdown-remark")
1+
const _get = require("lodash/get")
32

4-
exports.onCreateNode = async (
5-
{node, actions, store, cache, createNodeId, createContentDigest, reporter},
6-
pluginOptions
7-
) => {
8-
if (node.internal.type === "GoogleDocs") {
9-
await onCreateNodeGoogleDocs({
10-
node,
11-
actions,
12-
store,
13-
cache,
14-
createNodeId,
15-
createContentDigest,
16-
reporter,
17-
pluginOptions,
18-
})
19-
}
3+
exports.onCreateNode = async ({node, getNode}, pluginOptions) => {
4+
if (_get(pluginOptions, "skipImages") === true) return
5+
if (_get(node, "frontmatter.cover") === null) return
6+
7+
const googleDocsNode = getNode(node.parent)
208

21-
if (node.internal.type === "MarkdownRemark") {
22-
await onCreateNodeMarkdownRemark({
23-
node,
24-
actions,
25-
cache,
26-
pluginOptions,
27-
})
9+
if (googleDocsNode) {
10+
const coverImageId = _get(googleDocsNode, "cover.image")
11+
12+
if (coverImageId) {
13+
delete node.frontmatter.cover.image
14+
node.frontmatter.cover.image___NODE = coverImageId
15+
}
2816
}
17+
18+
return
2919
}

utils/source-nodes.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@ const _merge = require("lodash/merge")
22

33
const {fetchDocuments} = require("./google-docs")
44
const {DEFAULT_OPTIONS} = require("./constants")
5+
const {updateImages} = require("./update-images")
56

67
exports.sourceNodes = async (
7-
{actions: {createNode}, createContentDigest, reporter},
8+
{
9+
actions: {createNode},
10+
createContentDigest,
11+
reporter,
12+
store,
13+
cache,
14+
createNodeId,
15+
},
816
pluginOptions
917
) => {
1018
const options = _merge({}, DEFAULT_OPTIONS, pluginOptions)
@@ -38,17 +46,31 @@ exports.sourceNodes = async (
3846
const {document, properties, cover, related} = googleDocument
3947
const markdown = googleDocument.toMarkdown()
4048

41-
createNode({
49+
const node = {
4250
...properties,
4351
document,
4452
cover,
4553
markdown,
46-
related___NODE: related,
54+
related,
55+
}
56+
57+
await updateImages({
58+
node,
59+
createNode,
60+
store,
61+
cache,
62+
createNodeId,
63+
reporter,
64+
pluginOptions,
65+
})
66+
67+
createNode({
68+
...node,
4769
internal: {
4870
type: "GoogleDocs",
4971
mediaType: "text/markdown",
50-
content: markdown,
51-
contentDigest: createContentDigest(markdown),
72+
content: node.markdown,
73+
contentDigest: createContentDigest(node.markdown),
5274
},
5375
dir: process.cwd(), // To make gatsby-remark-images works
5476
})

utils/on-create-node-google-docs.js renamed to utils/update-images.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ const MD_URL_TITLE_REGEX = new RegExp(
1111
"g"
1212
)
1313

14-
exports.onCreateNodeGoogleDocs = async ({
14+
exports.updateImages = async ({
1515
node,
16-
actions: {createNode},
16+
createNode,
1717
store,
1818
cache,
1919
createNodeId,
20-
createContentDigest,
2120
reporter,
2221
pluginOptions,
2322
}) => {
@@ -45,11 +44,7 @@ exports.onCreateNodeGoogleDocs = async ({
4544
}
4645

4746
if (fileNode) {
48-
delete node.cover.image
49-
node.cover.image___NODE = fileNode.id
50-
51-
// fileNode.id is useful to link MarkdownRemark cover nodes
52-
await cache.set(fileNode.relativePath, fileNode.id)
47+
node.cover.image = fileNode.id
5348
}
5449
}
5550

@@ -92,11 +87,12 @@ exports.onCreateNodeGoogleDocs = async ({
9287
)
9388
})
9489

95-
node.images___NODE = filesNodes
90+
const imagesIds = filesNodes
9691
.filter((fileNode) => fileNode)
9792
.map((fileNode) => fileNode.id)
93+
94+
node.images = imagesIds
9895
}
9996

100-
node.internal.content = node.markdown
101-
node.internal.contentDigest = createContentDigest(node.markdown)
97+
return
10298
}

0 commit comments

Comments
 (0)