So I am busy building a process in N8N with the intent of creating a draft blog post for myself using OpenAi. Trigger is a webhook where my draft data is sent, then gets sent to openai where it creates a draft for me (Best would be that the output is in markdown and ready for a blog post, then from there I want to send it to hashnode via GraphQl. But I am expoeriencing many issues from OpenAI Output being \n to quotes in it that messes with the variables json etc etc.
Has anyone made anything similar or any sugegstions how to build a working process. Been at it for weeks now
You might need to polish the response using the code node to prepare it for the next node. Can you share your workflow and an example response from OpenAI output?
return items.map(item => {
let content = item.json.message.content;
// 1. Normalize line breaks to \n
content = content.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
// 2. Ensure there is a space after headers (e.g., '#Header' -> '# Header')
content = content.replace(/^(#{1,6})(\S)/gm, '$1 $2');
// 3. Ensure two line breaks between paragraphs
content = content.replace(/([^\n])\n([^\n])/g, '$1\n\n$2');
// 4. Trim trailing spaces on each line
content = content.replace(/[ \t]+$/gm, '');
// 5. Ensure code blocks have proper line breaks before and after
content = content.replace(/```/g, '\n```\n');
// 6. Remove any unnecessary multiple blank lines
content = content.replace(/\n{3,}/g, '\n\n');
// Trim the entire content
content = content.trim();
// Initialize title as null
let title = null;
// Split content into lines
const lines = content.split('\n');
// Check if the first line is a Markdown header
const headerMatch = lines[0].match(/^(#{1,6})\s*(.*)$/);
if (headerMatch) {
// Extract the title without the '#' symbols and any leading/trailing spaces
title = headerMatch[2].trim();
// Remove the first line from the content
lines.shift();
// Rejoin the remaining lines into the cleaned content
content = lines.join('\n').trim();
}
// Replace all double quotes with single quotes to avoid JSON issues
content = content.replace(/"/g, "'");
// Construct the desired JSON structure
const output = {
input: {
title: title,
contentMarkdown: content,
publicationId: "my iD"
}
};
return {
output
};
});
GraphQL Query in Json Format Request:
mutation CreateDraft($input: CreateDraftInput!) {
createDraft(input: $input) {
draft {
id
slug
title
subtitle
author {
username
name
}
tagsV2 {
__typename
}
canonicalUrl
coverImage {
url
}
readTimeInMinutes
content {
markdown
}
updatedAt
seo {
title
description
}
}
}
}
MY Javascript outputs the Variables in Json, this was just part of a test as I will explain below to try and just output the entire Json Variables
The main section causing issues is this section (“contentMarkdown”: “{{ $json.output.input.contentMarkdown }}”) Reason be the content in this section and output from OpenAI looks like This “#Header Title \n\n ##second title \n\n Text”
The issue is when there is characters such as " or \n in my json it cannot be send to Hashnode. But if I only have text with no special characters and add the markdown manually (“contentMarkdown”: “{{ $json.output.input.contentMarkdown1 }}\n\n{{ $json.output.input.contentMarkdown2 }}”) then it works fine.