Skip to main content

handling-pre-build-airtable-script

Here’s a quick sketch of how you could turn that one-off script into a reusable, parameterized tool that can pull from any Airtable view into any output folder (or even loop over multiple “contexts” at once).


1. Create a config file for your contexts​

Put this in e.g. airtable-contexts.json at the root of your repo:

[
{
"name": "default",
"view": "Grid view",
"outputDir": "docs"
},
{
"name": "alternate",
"view": "Alternate docs view",
"outputDir": "docs-alt"
}
]

Each object maps:

  • view → the Airtable view name or ID
  • outputDir → where to write the resulting .md files

2. A generalized fetch script​

Save this as scripts/fetch-airtable-to-md.js:

#!/usr/bin/env node
import fs from "fs";
import path from "path";
import Airtable from "airtable";

// load your contexts
const contexts = JSON.parse(
fs.readFileSync(path.resolve(__dirname, "../airtable-contexts.json"), "utf8")
);

// basic Airtable setup
const base = new Airtable({
apiKey: process.env.AIRTABLE_API_KEY,
}).base(process.env.AIRTABLE_BASE_ID);

async function fetchAndWrite({ view, outputDir }) {
console.log(`\n➡️ Fetching view "${view}" → ${outputDir}/`);
// ensure output dir exists
fs.mkdirSync(outputDir, { recursive: true });

// pull all records
const records = await base("Docs").select({ view }).all();

for (const rec of records) {
const f = rec.fields;
// build front-matter
const fm = [
"---",
`title: "${f.Title}"`,
f.Slug ? `slug: "${f.Slug}"` : null,
f.Date ? `date: "${new Date(f.Date).toISOString()}"` : null,
f.Tags && f.Tags.length
? ["tags:", ...f.Tags.map((t) => ` - "${t}"`)].join("\n")
: null,
"---",
"",
]
.filter(Boolean)
.join("\n");

// combine and write
const markdown = fm + (f.Content || "");
const filename = (f.Slug || rec.id) + ".md";
fs.writeFileSync(path.join(outputDir, filename), markdown, "utf8");
console.log(` 📝 wrote ${filename}`);
}
}

async function main() {
for (const ctx of contexts) {
await fetchAndWrite(ctx);
}
}

main().catch((err) => {
console.error(err);
process.exit(1);
});

Key points:

  • Reads airtable-contexts.json
  • Loops over each { view, outputDir }
  • Builds YAML + Markdown exactly as before

3. Wire it into your build​

In your package.json:

{
"scripts": {
"prebuild": "node scripts/fetch-airtable-to-md.js",
"build": "docusaurus build",
"start": "docusaurus start"
}
}

Now:

  • npm run prebuild will generate both docs/ and docs-alt/ (or however many contexts you list)
  • npm run build (which invokes prebuild first) will pick up all of them

Tips​

  • Environment variables

    • AIRTABLE_API_KEY
    • AIRTABLE_BASE_ID
  • You can spin up an entirely separate “alternate docs” build by pointing Docusaurus at docs-alt/ in a second site config—no code changes needed.

With this pattern you can add as many contexts as you like, each sourcing from a different Airtable view and dropping into a different folder.