Bridgetown is a wonderful static site generator. It uses markdown files by default, but I recently found out that it can also use an external API to generate resources.
Here's how to integrate it with Airtable:
Setting up
First, get your keys ready:
Create a .env
file in your Bridgetown project root with these keys:
AIRTABLE_API_KEY = "yourAPIkey"
AIRTABLE_BASE_ID = "yourBaseID"
AIRTABLE_TABLE_NAME = "yourTableName"
Don't forget to set these environment variables in your production environment as well.
Finally, add these to your Gemfile and run bundle install
.
gem "airrecord"
gem "dotenv"
Build script
Create a ruby file in your plugins/builders
folder and customise it to map your Airtable fields to your frontmatter
# plugins/builders/airtable.rb
require 'dotenv/load'
require 'airrecord'
class Builders::Airtable < SiteBuilder
def build
# setup keys
api_key = ENV['AIRTABLE_API_KEY']
base_id = ENV['AIRTABLE_BASE_ID']
table_name = ENV['AIRTABLE_TABLE_NAME']
# query records using airrecord
records = Airrecord.table(api_key, base_id, table_name).all
# map airtable fields to frontmatter
records.each do |record|
add_resource :posts, "#{Bridgetown::Utils.slugify(record['Title'])}.md" do
layout :post
title record['Title']
description record['Description']
tags record['Tags']
end
end
end
end
That's it. Your resources will now be generated from Airtable.
Bonus: Deploy to Netlify from Airtable
How do you trigger a new build when the records on Airtable change? Netlify build hooks and Airtable Apps to the rescue.
- Create a build hook on Netlify
- Add a custom app on your Airtable base
- Write a simple script in your app to make a post request:
let netlifyURL = 'https://api.netlify.com/build_hooks/yourBuildHookID'
let response = await fetch(netlifyURL, {method: 'POST'});
This will add a Run
button right inside Airtable that triggers a build every time you run it. Have fun!
Top comments (0)