{
  "name": "Build a Free Local n8n LinkedIn Post Generator",
  "nodes": [
    {
      "name": "Topic Form",
      "type": "n8n-nodes-base.formTrigger",
      "typeVersion": 2.2,
      "position": [
        0,
        300
      ],
      "parameters": {
        "formTitle": "LinkedIn Post Generator",
        "formDescription": "Enter a topic and we will research it locally and draft a post.",
        "formFields": {
          "values": [
            {
              "fieldLabel": "Topic",
              "requiredField": true,
              "placeholder": "e.g. Why small businesses should try local AI"
            },
            {
              "fieldLabel": "Audience",
              "placeholder": "e.g. solo creators and small business owners"
            },
            {
              "fieldLabel": "Tone",
              "fieldType": "dropdown",
              "fieldOptions": {
                "values": [
                  {
                    "option": "Conversational"
                  },
                  {
                    "option": "Professional"
                  },
                  {
                    "option": "Playful"
                  },
                  {
                    "option": "Inspirational"
                  }
                ]
              }
            },
            {
              "fieldLabel": "Length",
              "fieldType": "dropdown",
              "fieldOptions": {
                "values": [
                  {
                    "option": "Short (80-120 words)"
                  },
                  {
                    "option": "Medium (150-200 words)"
                  },
                  {
                    "option": "Long (250-300 words)"
                  }
                ]
              }
            },
            {
              "fieldLabel": "Required points",
              "fieldType": "textarea",
              "placeholder": "Anything the post must mention"
            }
          ]
        },
        "responseMode": "lastNode",
        "options": {}
      },
      "id": "05633862-4f93-45e7-a6ad-849bacd95bba"
    },
    {
      "name": "Search SearXNG",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        300,
        300
      ],
      "parameters": {
        "url": "http://searxng:8080/search",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "q",
              "value": "={{ $json['Topic'] }}"
            },
            {
              "name": "format",
              "value": "json"
            },
            {
              "name": "language",
              "value": "en"
            }
          ]
        },
        "options": {
          "timeout": 30000
        }
      },
      "id": "02e8a1d8-0b8d-4fec-a54e-2ae343d2d81d"
    },
    {
      "name": "Build Research Brief",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        600,
        300
      ],
      "parameters": {
        "jsCode": "const topic = $('Topic Form').first().json['Topic'];\nconst results = ($input.first().json.results || []).filter(r => r.title && r.url);\nconst seen = new Set();\nconst top = [];\nfor (const r of results) {\n  if (seen.has(r.url)) continue;\n  seen.add(r.url);\n  top.push({\n    title: String(r.title).trim(),\n    snippet: String(r.content || '').replace(/\\s+/g, ' ').trim().slice(0, 300),\n    url: r.url,\n  });\n  if (top.length === 5) break;\n}\nconst brief = top.map((r, i) => `${i + 1}. ${r.title}\\n   ${r.snippet}\\n   Source: ${r.url}`).join('\\n\\n');\nreturn [{ json: { topic, brief, sources: top.map(r => r.url), resultCount: top.length } }];"
      },
      "id": "899a5331-8e48-4ba1-8cb1-5f5c5e4ca75b"
    },
    {
      "name": "Draft Post with Ollama",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        900,
        300
      ],
      "parameters": {
        "method": "POST",
        "url": "http://ollama:11434/api/chat",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({\"model\": \"qwen2.5:14b-instruct-q5_K_M\", \"stream\": false, \"options\": {\"temperature\": 0.5, \"num_ctx\": 4096}, \"messages\": [{\"role\": \"system\", \"content\": \"You write plain-text LinkedIn posts. Use ONLY facts found in the research notes; never invent statistics, quotes or names. If the notes do not support a claim, leave it out. Output rules: plain text only, no markdown, no asterisks, no headings, no hashtags spam (max 3 hashtags at the end), short paragraphs separated by blank lines, a strong first line hook, and a closing question. Do NOT include sources, links or any preamble in the post. Return only the post.\"}, {\"role\": \"user\", \"content\": ( 'Write a LinkedIn post about: ' + $json.topic + '\\n\\n' + 'Audience: ' + ($('Topic Form').first().json['Audience'] || 'general professionals') + '\\n' + 'Tone: ' + ($('Topic Form').first().json['Tone'] || 'Conversational') + '\\n' + 'Length: ' + ($('Topic Form').first().json['Length'] || 'Medium (150-200 words)') + '\\n' + 'Points to include: ' + ($('Topic Form').first().json['Required points'] || 'none') + '\\n\\n' + 'RESEARCH NOTES:\\n' + $json.brief)}]}) }}",
        "options": {
          "timeout": 600000
        }
      },
      "id": "6c6a3eaf-52ac-4c99-8e7f-d1e4545c5bc6"
    },
    {
      "name": "Clean Draft",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1200,
        300
      ],
      "parameters": {
        "jsCode": "const form = $('Topic Form').first().json;\nconst research = $('Build Research Brief').first().json;\nconst raw = ($input.first().json.message?.content || '').trim();\n\nlet text = raw\n  .replace(/<think>[\\s\\S]*?<\\/think>/gi, '')\n  .replace(/^(here('s| is)[^\\n]*:|sure[^\\n]*:)\\s*/i, '')\n  .replace(/^```[a-z]*\\n?|```$/gim, '')\n  .replace(/\\*\\*(.+?)\\*\\*/g, '$1')\n  .replace(/__(.+?)__/g, '$1')\n  .replace(/^#{1,6}\\s*/gm, '')\n  .replace(/^\\s*[*-]\\s+/gm, '\u2022 ');\n\n// keep any citation block out of the post body\nconst cut = text.search(/^\\s*(sources?|references?|citations?)\\s*:?\\s*$/im);\nif (cut > -1) text = text.slice(0, cut);\ntext = text.replace(/\\n{3,}/g, '\\n\\n').trim();\n\nreturn [{ json: {\n  topic: form['Topic'],\n  post_text: text,\n  source_links: research.sources.join('\\n'),\n  raw_response: raw,\n  created_at: new Date().toISOString(),\n} }];"
      },
      "id": "63682da6-50aa-47a3-9e64-d5e1d3246a03"
    },
    {
      "name": "Create Posts Table",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        1500,
        300
      ],
      "parameters": {
        "operation": "executeQuery",
        "query": "CREATE TABLE IF NOT EXISTS linkedin_posts (id serial PRIMARY KEY, topic text NOT NULL, post_text text NOT NULL, source_links text, raw_response text, created_at timestamptz DEFAULT now());",
        "options": {}
      },
      "id": "40ba0d2f-442d-4ba9-94f7-19cf32d7c8ae"
    },
    {
      "name": "Save Post to Postgres",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        1800,
        300
      ],
      "parameters": {
        "operation": "insert",
        "schema": {
          "__rl": true,
          "mode": "list",
          "value": "public"
        },
        "table": {
          "__rl": true,
          "mode": "name",
          "value": "linkedin_posts"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "topic": "={{ $('Clean Draft').first().json.topic }}",
            "post_text": "={{ $('Clean Draft').first().json.post_text }}",
            "source_links": "={{ $('Clean Draft').first().json.source_links }}",
            "raw_response": "={{ $('Clean Draft').first().json.raw_response }}",
            "created_at": "={{ $('Clean Draft').first().json.created_at }}"
          },
          "matchingColumns": [],
          "schema": []
        },
        "options": {}
      },
      "id": "7bffab99-2d6f-4e57-a234-0d22f614e3c5"
    },
    {
      "name": "Confirmation",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        2100,
        300
      ],
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "a1",
              "name": "message",
              "type": "string",
              "value": "Draft saved to the linkedin_posts table."
            },
            {
              "id": "a2",
              "name": "topic",
              "type": "string",
              "value": "={{ $json.topic }}"
            },
            {
              "id": "a3",
              "name": "post",
              "type": "string",
              "value": "={{ $('Clean Draft').first().json.post_text }}"
            }
          ]
        },
        "options": {}
      },
      "id": "c6e326f3-bba4-424e-92c8-fd5dcf3b7740"
    },
    {
      "name": "1 Form",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -40,
        160
      ],
      "parameters": {
        "content": "## 1. Collect the topic",
        "width": 280,
        "height": 300,
        "color": 7
      },
      "id": "786dbb9f-35ae-4e62-bf1a-a5ffc7df011b"
    },
    {
      "name": "2 Research",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        260,
        160
      ],
      "parameters": {
        "content": "## 2. Research locally (SearXNG)",
        "width": 580,
        "height": 300,
        "color": 4
      },
      "id": "2e69231a-8403-4430-b9e4-7fcfeb56cad0"
    },
    {
      "name": "3 Draft",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        860,
        160
      ],
      "parameters": {
        "content": "## 3. Draft with Ollama",
        "width": 580,
        "height": 300,
        "color": 5
      },
      "id": "37775d87-498d-4907-b6bd-ebc581e346f2"
    },
    {
      "name": "4 Save",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1460,
        160
      ],
      "parameters": {
        "content": "## 4. Save and confirm",
        "width": 760,
        "height": 300,
        "color": 6
      },
      "id": "71068249-3e52-4045-9b7a-f3d62f28779b"
    }
  ],
  "connections": {
    "Topic Form": {
      "main": [
        [
          {
            "node": "Search SearXNG",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search SearXNG": {
      "main": [
        [
          {
            "node": "Build Research Brief",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Research Brief": {
      "main": [
        [
          {
            "node": "Draft Post with Ollama",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Draft Post with Ollama": {
      "main": [
        [
          {
            "node": "Clean Draft",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Clean Draft": {
      "main": [
        [
          {
            "node": "Create Posts Table",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Posts Table": {
      "main": [
        [
          {
            "node": "Save Post to Postgres",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save Post to Postgres": {
      "main": [
        [
          {
            "node": "Confirmation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "availableInMCP": true
  }
}