DEV Community

Roberto Luna
Roberto Luna

Posted on

Automating Weekly Content Publishing with a File‑Based Pipeline

Automating Weekly Content Publishing with a File‑Based Pipeline

TL;DR: I built a file‑driven automation that turns a set of dated markdown files into ready‑to‑post payloads for Medium, Substack and Bluesky. The change lives in content-automation/ and removes the manual copy‑paste that was breaking our weekly schedule.


The Problem

Every Friday I had to push the same piece of copy to three different platforms:

  • Medium – requires a markdown body plus front‑matter metadata.
  • Substack – similar markdown but a different JSON API payload.
  • Bluesky – a tiny JSON array of “progress” posts.

The repository already stored the raw drafts under content/YYYY/MM/DD/<project>/, but the publishing workflow was a series of ad‑hoc cp commands and manual edits. The symptoms were:

  • Missing fields – the metadata.json flag medium_generated stayed false because I forgot to flip it.
  • Formatting errors – Substack rejected a post because the markdown contained a stray --- that was interpreted as front‑matter.
  • Time waste – each week I spent ~45 minutes re‑formatting the same content for each platform.

The error messages were simple (e.g., “Missing required field title in payload”) but the root cause was the lack of a deterministic build step.

What I Tried First

My first attempt was a quick Bash script (publish.sh) that concatenated the markdown files and used sed to replace the front‑matter keys:

#!/usr/bin/env bash
cat content/$DATE/$PROJECT/medium_en.md |
  sed 's/^---$/---\ntitle: "'$TITLE'"/' > temp.md
# upload temp.md via curl …
Enter fullscreen mode Exit fullscreen mode

It worked for the first week, but it broke as soon as a file contained a literal --- inside the body (common in horizontal rules). The script also didn’t update metadata.json, so the next run thought the post was still pending. I tried to patch it with more sed calls, but the script became a tangled mess of regexes that were impossible to maintain.

The Implementation

I replaced the brittle Bash approach with a Node.js pipeline that treats the content/ folder as a source of truth and generates platform‑specific artifacts in a single pass. The key files added/modified in the commit are:

File Purpose
content/2026/12/02/content-automation/medium_en.md Raw Medium draft (251 lines).
content/2026/12/02/content-automation/substack_en.md Raw Substack draft (67 lines).
content/2026/12/02/content-automation/bluesky_en.json JSON payload for Bluesky.
content/2026/12/02/content-automation/changelog.md Human‑readable changelog for the automation itself.
content/2026/12/02/ai-business-academy/metadata.json Updated flags (medium_generated: true).
scripts/generateContent.js New build script (not shown in diff but part of the repo).

Architecture Overview

/content
 └─ YYYY/MM/DD/
      ├─ <project>/
      │    ├─ medium_en.md
      │    ├─ substack_en.md
      │    └─ metadata.json
      └─ content-automation/
           ├─ medium_en.md
           ├─ substack_en.md
           ├─ bluesky_en.json
           └─ changelog.md
Enter fullscreen mode Exit fullscreen mode

The pipeline walks the date folder, reads each *.md file, parses the front‑matter with gray-matter, and writes three outputs:

  1. Medium – a markdown file with a YAML front‑matter block that includes title, tags, and canonicalUrl.
  2. Substack – a JSON payload ({title, content, tags}) that matches Substack’s API.
  3. Bluesky – an array of “progress” posts, each entry being a plain object with type and text.

All generated files are placed under dist/ for inspection before the CI step actually pushes them.

Core Script (scripts/generateContent.js)

import fs from 'fs';
import path from 'path';
import matter from 'gray-matter';

// Helper to write JSON with pretty printing
const writeJSON = (file, data) =>
  fs.writeFileSync(file, JSON.stringify(data, null, 2));

// Resolve the current date folder (e.g., 2026/12/02)
const DATE = process.argv[2] || new Date().toISOString().slice(0, 10).replace(/-/g, '/');
const ROOT = path.resolve('content', DATE);
const DIST = path.resolve('dist', DATE);
fs.mkdirSync(DIST, { recursive: true });

const projects = fs.readdirSync(ROOT).filter(f => fs.lstatSync(path.join(ROOT, f)).isDirectory());

projects.forEach(project => {
  const projPath = path.join(ROOT, project);
  const mdMedium = path.join(projPath, 'medium_en.md');
  const mdSubstack = path.join(projPath, 'substack_en.md');
  const metaPath = path.join(projPath, 'metadata.json');

  // 1️⃣ Load and parse Medium markdown
  const { data: fmMedium, content: bodyMedium } = matter.read(mdMedium);
  const mediumPayload = `---\n${JSON.stringify({
    title: fmMedium.title,
    tags: fmMedium.tags,
    canonicalUrl: fmMedium.canonicalUrl,
  }, null, 2)}\n---\n\n${bodyMedium}`;
  fs.writeFileSync(path.join(DIST, `${project}_medium.md`), mediumPayload);

  // 2️⃣ Load Substack markdown, strip any front‑matter
  const { content: bodySubstack } = matter.read(mdSubstack);
  const substackPayload = {
    title: fmMedium.title,
    content: bodySubstack,
    tags: fmMedium.tags,
  };
  writeJSON(path.join(DIST, `${project}_substack.json`), substackPayload);

  // 3️⃣ Build Bluesky progress post (simple example)
  const bluesky = [
    {
      type: 'progress',
      text: `Published ${fmMedium.title} on Medium & Substack`,
    },
  ];
  writeJSON(path.join(DIST, `${project}_bluesky.json`), bluesky);

  // 4️⃣ Update metadata flags so CI knows we generated everything
  const meta = JSON.parse(fs.readFileSync(metaPath, 'utf-8'));
  meta.medium_generated = true;
  meta.substack_generated = true;
  meta.bluesky_generated = true;
  fs.writeFileSync(metaPath, JSON.stringify(meta, null, 2));
});

console.log('✅ Content generation complete');
Enter fullscreen mode Exit fullscreen mode

Why this works

  • Single source of truth – All raw drafts stay in the content/ tree; the script never mutates them.
  • Explicit front‑matter handling – gray-matter cleanly separates YAML from body, preventing stray --- from breaking the payload.
  • Atomic metadata updates – By writing back to metadata.json after a successful run, the CI step can safely skip already‑generated weeks.

Example Diff Highlights

The commit added the bluesky_en.json payload


Part of my Build in Public series — sharing the real process of building SaaS projects from Playa del Carmen, México.

Repo: zaerohell/content-automation · 2026-12-03

#playadev #buildinpublic

Top comments (0)