gatsby-source-google-docs
v3.2.2
Published
Gatsby plugin to use Google Docs as a data source
Maintainers
Readme
gatsby-source-google-docs is a Gatsby plugin to use Google Docs as a data source.
- 🖋 Best online WYSIWYG editor
- 🖥 Desktop web app
- 📱 Mobile app
- 🛩 Offline redaction
- 🔥 No need for external CMS
- ✅ No more content in your source code
Features
- Documents synced to real Markdown or MDX files, so
gatsby-transformer-remarkandgatsby-plugin-mdxpick them up - Pages created from the Google Drive tree, with your own templates
gatsby-plugin-imageandgatsby-imagesupport- Incremental builds: only the documents and images that changed are fetched
- Gatsby v5 and Gatsby Cloud support
The sync is done by google-docs-sync and the conversion by google-docs-to-markdown: see them for the Google Docs formatting supported (headings, lists, tables, images, code, footnotes...), the metadata and the shared options.
Documentation
To preview what you can do, please checkout the documentation website.
Two runnable examples live in this repository, both sourcing the folder above:
examples/basic:gatsby-transformer-remarkexamples/mdx:gatsby-plugin-mdx
💯 100% content of the website is from Google Docs. Please suggest edits to improve it.
Installation
Requires Node 22.12 or later and Gatsby 5.3 or later. Gatsby 5 does not support Node 26 yet.
Download gatsby-source-google-docs and gatsby-transformer-remark (or gatsby-plugin-mdx for advanced usage)
npm install gatsby-source-google-docs gatsby-transformer-remarkgatsby-source-google-docstransform Google Docs to Markdown, with google-docs-sync, which you can also use without Gatsbygatsby-transformer-remarktransform Markdown to HTMLgatsby-plugin-mdxtransform Markdown to MDX
Token generation
The plugin needs 3 .env variables (GOOGLE_OAUTH_CLIENT_ID, GOOGLE_OAUTH_CLIENT_SECRET and GOOGLE_DOCS_TOKEN). Run the following command at the root of your project to generate them, the google-docs-sync documentation gives the details:
npx gatsby-source-google-docs-tokenUsage
Organize your documents
Go to your Google Drive, create a folder and put some documents inside it. The folders tree gives the slugs and breadcrumbs, and the description of the folders and documents holds their YAML metadata (template, locale, slug, date, exclude, skip, ...): see organize your documents.
Every metadata is available under frontmatter in the File nodes of the documents.
Add the plugin to your gatsby-config.js file
The plugin takes the options of google-docs-sync (folder, outputDir, cacheDir, extension, imagesOptions, debug) and the conversion options of google-docs-to-markdown (escapeMdxSyntax, demoteHeadings, keepDefaultStyle and the skip* ones), with the same defaults. These ones are specific to Gatsby:
| Option | Required | Type | Default | Example |
| ----------- | -------- | ------- | ------- | ------------ |
| createPages | false | Boolean | false | true |
| pageContext | false | Array | [] | ["locale"] |
module.exports = {
plugins: [
{
resolve: "gatsby-source-google-docs",
options: {
// https://drive.google.com/drive/folders/FOLDER_ID
folder: "FOLDER_ID",
createPages: true,
},
},
"gatsby-transformer-remark",
//
// OR "gatsby-plugin-mdx" for advanced usage using MDX
//
// You need some transformations?
// Checkout https://www.gatsbyjs.com/plugins/?=gatsby-remark
// And pick-up some plugins
],
}gatsby-plugin-sharp, gatsby-transformer-sharp and gatsby-remark-images are required if you want to take advantage of gatsby-image blur-up technique.
npm install gatsby-plugin-sharp gatsby-transformer-sharp gatsby-remark-imagesmodule.exports = {
plugins: [
"gatsby-source-google-docs",
"gatsby-plugin-sharp",
"gatsby-transformer-sharp",
{
resolve: "gatsby-transformer-remark",
options: {
plugins: ["gatsby-remark-images"],
},
},
],
}Write the code blocks as described in google-docs-to-markdown.
To get Syntax highlighting, I recommend using prismjs but it's not mandatory.
npm install gatsby-remark-prismjs prismjsAdd the gatsby-remark-prismjs plugin to your gatsby-config.js
module.exports = {
plugins: [
"gatsby-source-google-docs",
{
resolve: "gatsby-transformer-remark",
options: {
plugins: ["gatsby-remark-prismjs"],
},
},
],
}Import a prismjs theme in your gatsby-browser.js
require("prismjs/themes/prism.css")How documents reach Gatsby
The plugin writes every document to disk as a real markdown file, under
outputDir (content/google-docs by default), with its images next to it: see
the output of google-docs-sync. File nodes
are created for all of them, so gatsby-transformer-remark and
gatsby-plugin-mdx pick them up like any other local file.
outputDiris managed by the plugin: files it did not write are deleted on every build, so point it at a directory of its own and add it to your.gitignore.
This is why MDX works again. Since v4, gatsby-plugin-mdx compiles MDX
through webpack from a file path and
only supports files sourced from the filesystem,
so documents kept in memory could never become MDX. Set extension: "mdx" to
author your documents as MDX.
⚠️ MDX only supports CommonMark, and this plugin generates GitHub Flavored Markdown: without
remark-gfm, tables and~~strikethrough~~are rendered as raw text.remark-gfmis ESM only, so the config has to be agatsby-config.mjsfile — seeexamples/mdx.
⚠️ By default, a document's text is escaped before being written: a stray, unmatched
<or{(a<placeholder>convention, a generic<T>, ...) makes MDX v2 fail the whole build, not just that document. A well-formed tag compiles fine either way —<GatsbyLogo />works whether escaped or not — so this only matters for text that isn't a deliberate component. If every document on your site deliberately embeds live JSX/components as literal text, and you've made sure none of them contain a stray</{otherwise, setescapeMdxSyntax: falseto compile them as intended.
// gatsby-config.mjs
import remarkGfm from "remark-gfm"
const config = {
plugins: [
{
resolve: "gatsby-source-google-docs",
options: {folder: "FOLDER_ID", extension: "mdx", createPages: true},
},
{
resolve: "gatsby-plugin-mdx",
options: {
mdxOptions: {remarkPlugins: [remarkGfm]},
gatsbyRemarkPlugins: ["gatsby-remark-images"],
},
},
],
}
export default configOnly what changed is fetched
A build only fetches the documents and images that changed since the previous one. They are kept in the .google-docs directory next to your gatsby-config.js (the cacheDir option), which is not the Gatsby cache, on purpose: Gatsby empties its own whenever a plugin version, package.json, gatsby-config.js or gatsby-node.js changes, and a continuous integration job never has one. Add .google-docs to your .gitignore, or commit it to give your continuous integration a folder it does not have to fetch. See how the cache works.
Read the comments
With the comments option, the comments of a document are available under frontmatter: frontmatter { comments { content quote author { displayName } } }.
Create templates and pages
Using createPages: true option, pages will be created automatically.
You need to create templates and define wich template to use using YAML metadata.
You can set
page: falsemetadata for a document to prevent a page creation
Every field of your document metadata is available under frontmatter:
export const pageQuery = graphql`
query Page($slug: String!) {
page: markdownRemark(frontmatter: {slug: {eq: $slug}}) {
html
frontmatter {
name
breadcrumb {
name
slug
}
cover {
image {
childImageSharp {
gatsbyImageData
}
}
}
}
}
}
`Checkout the example template and adapt it to your needs.
You can use
pageContextoption if you need extra data into the context of your pages.
If you prefer to create pages manualy, checkout the createPages API et adapt it to your needs.
Trigger production builds
- Go to Google Drive example folder
- Make a copy of Trigger Gatsby Build file using
Right Click -> Make a copy - Open your copy and update the Build Webhook URL in
A2 - Click the Deploy button to trigger a new build
This method works with any hosting services: Gatsby Cloud, Netlify...
Migrating from v2
v3 writes documents to the filesystem instead of keeping them in memory. That
is what makes MDX and the current gatsby-plugin-mdx usable again, but it
changes how documents are queried.
The GoogleDocs node type is gone. Documents are now MarkdownRemark (or
Mdx) nodes, and everything that used to sit on the node is in frontmatter:
- page: googleDocs(slug: {eq: $slug}) {
- name
- cover {
- image {
- childImageSharp {
- gatsbyImageData
- }
- }
- }
- childMarkdownRemark {
- html
- }
- }
+ page: markdownRemark(frontmatter: {slug: {eq: $slug}}) {
+ html
+ frontmatter {
+ name
+ cover {
+ image {
+ childImageSharp {
+ gatsbyImageData
+ }
+ }
+ }
+ }
+ }Other things to know:
allGoogleDocsbecomesallMarkdownRemark(orallMdx). These types are shared with any other markdown your site sources, so filter onfrontmatter: {slug: {ne: null}}if you need only Google Docs documents.relatedis now a list of document ids instead of linked nodes.- Add
outputDir(content/google-docsby default) andcacheDir(.google-docsby default) to your.gitignore. - Node 22 or 24 is required, and
gatsby@^5is now a peer dependency.
Showcase
You are using gatsby-source-google-docs for your website? Thank you!
Please add the link below:
