2.4.3 • Published 4 months ago

gatsby-source-google-docs v2.4.3

Weekly downloads
1,011
License
MIT
Repository
github
Last release
4 months ago

Npm Build Status Coverage Downloads PRs welcome MIT license Paypal


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

  • Google Docs formatting options (headings, bullets, tables, images...)
  • MDX support to use <ReactComponents /> in your documents
  • Gatsby v3 & v4 support
  • gatsby-plugin-image and gatsby-image support
  • Code blocs support
  • Gatsby Cloud support
  • Slug generation from Google Drive tree
  • Crosslinks between pages
  • Related content
  • Custom metadata to enhance documents

Documentation

To preview what you can do, please checkout the documentation website.

💯 100% content of the website is from Google Docs. Please suggest edits to improve it.

Installation

Download gatsby-source-google-docs and gatsby-transformer-remark (or gatsby-plugin-mdx for advanced usage)

yarn add gatsby-source-google-docs gatsby-transformer-remark
  • gatsby-source-google-docs transform Google Docs to Markdown
  • gatsby-transformer-remark transform Markdown to HTML
  • gatsby-plugin-mdx transform Markdown to MDX

Token generation

The package needs 3 .env variables.

GOOGLE_OAUTH_CLIENT_ID=2...m.apps.googleusercontent.com
GOOGLE_OAUTH_CLIENT_SECRET=Q...axL
GOOGLE_DOCS_TOKEN={"access_token":"ya...J0","refresh_token":"1..mE","scope":"https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/documents.readonly","token_type":"Bearer","expiry_date":1598284554759}

gatsby-source-google-docs expose a script to generate it.

  • Open a terminal at the root of your project
  • Type the following command
npx gatsby-source-google-docs-token

Usage

Organize your documents

Go to your Google Drive, create a folder and put some documents inside it.

↳ 🗂 Root folder `template: page`
    ↳ 🗂 en `locale: en` `skip: true`
        ↳ 📝 Home `template: home`
        ↳ 📝 About
        ↳ 🗂 Posts `template: post`
            ↳ 🗂 Drafts `exclude: true`
                ↳ 📝 Draft 1
            ↳ 📝 My year 2020 `date: 2021-01-01`
            ↳ 📝 Post 2 `slug: /custom/slug` `template: special-post`
    ↳ 🗂 fr `locale: fr`
        ↳ 📝 Accueil `template: home`
  • Fill the document (or folder) description field in Google Drive with a YAML object
locale: fr
template: post
category: Category Name
tags: [tag1, tag2]
slug: /custom-slug
date: 2019-01-01

There are special metadata

  • For folders:
    • exclude: true: Exclude the folder and its documents
    • skip: true: Remove the folder from slug but keep its documents
  • For documents:
    • index:true: Use document as the folder index
    • page: false: Prevent page creation when createPages option is set to true
  • Spread metadata into the tree using folders metadata.

⬆️ For the tree example above:

  • Every node will have template: page defined as default excepts if you redefine it later.
  • You need to create 3 different templates: page (default), home, post. Checkout the example template
  • "en" folder will be removed from slug because of skip: true
  • Exclude folders and documents using exclude: true. Perfect to keep drafts documents. One you want to publish a page, juste move the document one level up.

⬆️ For the tree example above:

  • Documents under Drafts will be exclude because of exclude: true.
  • Every metadata will be available in GoogleDocs nodes and you can use everywhere in you Gatsby site

Add an image in your Google Document first page header

slug and breadcrumb fields add automatically generated using the folders tree structure and transformed using kebab-case.

⬆️ For the tree example above: The GoogleDocs node for document My year 2020

{
    path: "/en/posts/my-year-2020" // Original Google Drive path
    slug: "/posts/my-year-2020" // `en` is out because of `skip: true`
    breadcrumb: [
        {name: "Posts", slug: "/posts"},
        {name: "My year 2020", slug: "/posts/my-year-2020"},
    ],
    template: "post" ,// src/templates/post.js
    locale: "fr",
    date: "2021-01-01" // Fixed date !
}

The GoogleDocs node for document Post 2 will have a custom slug

{
    path: "/en/posts/post-2"
    slug: "/custom/slug"
    breadcrumb: [
        {name: "Posts", slug: "/posts"},
        {name: "Post 2", slug: "/custom/slug"},
    ],
    template: "special-post",  // src/templates/special-post.js
    locale: "en",
    date: "2020-09-12" // Google Drive document creation date
}

You also can add metadata (locale, date, template, ...) to your documents.

Add the plugin to your gatsby-config.js file

OptionRequiredTypeDefaultExample
foldertrueStringnull"1Tn1dCbIc"
createPagesfalseBooleanfalsetrue
pageContextfalseArray[]["locale"]
demoteHeadingsfalseBooleantruefalse
imagesOptionsfalseObjectnull{width: 512}
keepDefaultStylefalseBooleanfalsetrue
skipCodesfalseBooleanfalsetrue
skipFootnotesfalseBooleanfalsetrue
skipHeadingsfalseBooleanfalsetrue
skipImagesfalseBooleanfalsetrue
skipListsfalseBooleanfalsetrue
skipQuotesfalseBooleanfalsetrue
skipTablesfalseBooleanfalsetrue
debugfalseBooleanfalsetrue
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.

yarn add gatsby-plugin-sharp gatsby-transformer-sharp gatsby-remark-images
module.exports = {
    plugins: [
        "gatsby-source-google-docs",
        "gatsby-plugin-sharp",
        "gatsby-transformer-sharp",
        {
            resolve: "gatsby-transformer-remark",
            options: {
                plugins: ["gatsby-remark-images"],
            },
        },
    ],
}

Use Code Blocks Google Docs extension to format your code blocks.

To specify the lang, you need to add a fist line in your code block following the format lang:javascript.

To get Syntax highlighting, I recommend using prismjs but it's not mandatory.

yarn add gatsby-remark-prismjs prismjs

Add 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")

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: false metadata for a document to prevent a page creation

Checkout the example template and adapt it to your needs.

You can use pageContext option 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...

Showcase

You are using gatsby-source-google-docs for your website? Thank you! Please add the link bellow:

Contributing

  • ⇄ Pull/Merge requests and ★ Stars are always welcome.
  • For bugs and feature requests, please create an issue.
2.4.3

4 months ago

2.4.2

9 months ago

2.4.1

1 year ago

2.4.0

1 year ago

2.3.0

1 year ago

2.2.3

2 years ago

2.2.2

2 years ago

2.2.1

2 years ago

2.2.0

2 years ago

2.1.3

2 years ago

2.1.2

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago

2.0.3

3 years ago

2.0.4

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

2.0.0-beta.45

3 years ago

2.0.0-beta.44

3 years ago

2.0.0-beta.43

3 years ago

2.0.0-beta.42

3 years ago

2.0.0-beta.41

3 years ago

2.0.0-beta.40

3 years ago

2.0.0-beta.39

3 years ago

2.0.0-beta.38

3 years ago

2.0.0-beta.37

3 years ago

2.0.0-beta.36

3 years ago

2.0.0-beta.35

3 years ago

2.0.0-beta.34

3 years ago

2.0.0-beta.33

3 years ago

2.0.0-beta.32

3 years ago

2.0.0-beta.31

3 years ago

2.0.0-beta.30

3 years ago

2.0.0-beta.29

3 years ago

2.0.0-beta.28

3 years ago

2.0.0-beta.27

3 years ago

2.0.0-beta.26

3 years ago

2.0.0-beta.25

3 years ago

2.0.0-beta.24

3 years ago

2.0.0-beta.23

4 years ago

2.0.0-beta.22

4 years ago

2.0.0-beta.21

4 years ago

2.0.0-beta.20

4 years ago

2.0.0-beta.19

4 years ago

2.0.0-beta.18

4 years ago

2.0.0-beta.17

4 years ago

2.0.0-beta.16

4 years ago

2.0.0-beta.15

4 years ago

2.0.0-beta.14

4 years ago

2.0.0-beta.13

4 years ago

2.0.0-beta.12

4 years ago

2.0.0-beta.11

4 years ago

2.0.0-beta.10

4 years ago

2.0.0-beta.9

4 years ago

2.0.0-beta.8

4 years ago

2.0.0-beta.7

4 years ago

2.0.0-beta.6

4 years ago

2.0.0-beta.5

4 years ago

2.0.0-beta.4

4 years ago

2.0.0-beta.3

4 years ago

2.0.0-beta.2

4 years ago

2.0.0-beta.1

4 years ago

1.14.2

5 years ago

1.14.1

5 years ago

1.14.0

5 years ago

1.13.0

5 years ago

1.12.0

5 years ago

1.11.0

5 years ago

1.10.0

5 years ago

1.9.0

5 years ago

1.8.0

5 years ago

1.7.0

5 years ago

1.6.1

5 years ago

1.6.0

5 years ago

1.5.0

5 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.1.0

5 years ago