Skip to content

Latest commit

 

History

History

astro-loader-google-docs

README.md

astro-loader-google-docs


astro-loader-google-docs


Npm Build Status Coverage Downloads PRs welcome MIT license Sponsor


astro-loader-google-docs is an Astro content loader that uses Google Docs as a data source. It syncs a Google Drive folder with google-docs-sync and stores one entry per document in a content collection.

Features

  • One collection entry per document, rendered with render(entry)
  • Metadata validated by a Zod schema, the covers can be optimised images too
  • Images optimised by Astro
  • Incremental syncs: only the documents that changed are fetched and rendered again

Installation

Requires Node 22.12 or later and Astro 5 or later.

npm install astro-loader-google-docs

The loader needs 3 variables (GOOGLE_OAUTH_CLIENT_ID, GOOGLE_OAUTH_CLIENT_SECRET and GOOGLE_DOCS_TOKEN) in the environment of the build. Astro does not put the .env file in process.env: load it in astro.config.mjs with process.loadEnvFile(). Generate them with npx google-docs-sync-token, see the google-docs-sync documentation.

Usage

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: see organize your documents.

Add the loader to your collection in src/content.config.ts.

import {defineCollection} from "astro:content"
import {
  createGoogleDocsSchema,
  googleDocsLoader,
} from "astro-loader-google-docs"

const docs = defineCollection({
  loader: googleDocsLoader({
    // https://drive.google.com/drive/folders/FOLDER_ID
    folder: "FOLDER_ID",
  }),
  // Optional: makes the covers optimised images
  schema: ({image}) => createGoogleDocsSchema({image}),
})

export const collections = {docs}

Then render the documents like any other collection.

---
import {getCollection, render} from "astro:content"

const docs = await getCollection("docs")
const {Content} = await render(docs[0])
---

<h1>{docs[0].data.name}</h1>
<Content />

The examples/basic site lists the documents of a Drive folder and renders each of them.

Options

googleDocsLoader(options) takes the options of google-docs-sync (folder, outputDir, cacheDir, imagesOptions, debug) and the conversion options of google-docs-to-markdown, with the same names as in gatsby-source-google-docs. The extension is always md: the documents are rendered as Markdown, MDX is not supported.

outputDir and cacheDir are relative to the root of the Astro site. The loader owns outputDir: any other file it contains is deleted, so do not point it to a folder holding your own files. Add outputDir and cacheDir to your .gitignore.

Entries

  • The id of an entry is the path of its file under outputDir, without the extension: index for the root document, guides/install for a nested one.
  • The data of an entry are the metadata of the document: id, name, slug, path, date, createdTime, modifiedTime, breadcrumb, description, cover, related, and the custom metadata of the Drive description.
  • With the comments option, data.comments holds the comments of the document.
  • createGoogleDocsSchema({image}) builds that schema. The loader declares it by default, so the schema of the collection is optional: define it to turn the covers into optimised images.

Images

The images of the documents are written next to them in outputDir, and the Markdown refers to them with relative paths. The loader renders each document relative to its file, so Astro treats them like the images of local Markdown files: they are optimised and fingerprinted at build time. Nothing has to be copied to public/.

Support this project

Cédric Delpoux

Hi, I'm Cédric.
Between my three kids, I maintain open source projects during my free time.
If my work is useful to you, a star, a one-time tip or a monthly sponsorship all help a lot.
Thank you very much!

thanks.dev GitHub Sponsors Ko-fi PayPal