top of page

All posts

import wixData from "wix-data"; // ============================================================ // LATVORK PUBLISH WORKER // ============================================================ // First stage: // Articles CMS → Wix Blog Draft // // DOES NOT publish automatically yet. // ============================================================ export async function processPublishJob() { let articleItem = null; try { // ---------------------------------------------------- // 1. Find next approved article // ---------------------------------------------------- const result = await wixData .query("Articles") .eq("publishStatus", "approved") .limit(1) .find({ suppressAuth: true }); if ( !result.items || result.items.length === 0 ) { return { success: true, message: "No approved articles waiting for publishing." }; } articleItem = result.items[0]; // ---------------------------------------------------- // 2. Lock article // ---------------------------------------------------- await wixData.update( "Articles", { _id: articleItem._id, publishStatus: "processing" }, { suppressAuth: true } ); // ---------------------------------------------------- // 3. Read article content // ---------------------------------------------------- let article = {}; try { article = JSON.parse( articleItem.content || "{}" ); } catch (error) { throw new Error( "Article content contains invalid JSON." ); } // ---------------------------------------------------- // 4. Validate article // ---------------------------------------------------- if (!article.title) { throw new Error( "Article title is missing." ); } if (!article.introduction) { throw new Error( "Article introduction is missing." ); } if ( !Array.isArray( article.sections ) ) { throw new Error( "Article sections are missing." ); } // ---------------------------------------------------- // 5. Build readable blog content // ---------------------------------------------------- let blogContent = ""; blogContent += `<p>${escapeHtml( article.introduction )}</p>`; for ( const section of article.sections ) { if ( section?.heading ) { blogContent += `<h2>${escapeHtml( section.heading )}</h2>`; } if ( section?.content ) { blogContent += `<p>${escapeHtml( section.content )}</p>`; } } if ( article.conclusion ) { blogContent += `<h2>Conclusion</h2>`; blogContent += `<p>${escapeHtml( article.conclusion )}</p>`; } // ---------------------------------------------------- // FAQ // ---------------------------------------------------- if ( Array.isArray( article.faq ) && article.faq.length ) { blogContent += `<h2>Frequently Asked Questions</h2>`; for ( const faq of article.faq ) { if ( faq?.question ) { blogContent += `<h3>${escapeHtml( faq.question )}</h3>`; } if ( faq?.answer ) { blogContent += `<p>${escapeHtml( faq.answer )}</p>`; } } } // ---------------------------------------------------- // IMAGE CREDIT // ---------------------------------------------------- if ( articleItem.imageStatus === "approved" && articleItem.imageCredit ) { blogContent += `<p><small>Image: ${escapeHtml( articleItem.imageCredit )}</small></p>`; } // ---------------------------------------------------- // 6. TEMPORARY RESULT // ---------------------------------------------------- // // We are NOT calling Wix Blog yet. // // This first test verifies that the article can // safely be converted into publishable content. // // ---------------------------------------------------- console.log( "LATVORK BLOG DRAFT PREVIEW", { title: article.title, slug: article.slug, seoTitle: article.seoTitle, metaDescription: article.metaDescription, contentLength: blogContent.length } ); // ---------------------------------------------------- // 7. Mark as draft-ready // ---------------------------------------------------- await wixData.update( "Articles", { _id: articleItem._id, publishStatus: "draft_ready", publishError: "" }, { suppressAuth: true } ); return { success: true, articleId: articleItem._id, title: article.title, publishStatus: "draft_ready", contentLength: blogContent.length }; } catch (error) { console.error( "LATVORK publish worker failed:", error ); if ( articleItem?._id ) { try { await wixData.update( "Articles", { _id: articleItem._id, publishStatus: "failed", publishError: error?.message || "Publishing failed." }, { suppressAuth: true } ); } catch (updateError) { console.error( "Failed to update publish status:", updateError ); } } return { success: false, articleId: articleItem?._id || null, message: error?.message || "Publishing failed." }; } } // ============================================================ // HTML ESCAPE // ============================================================ function escapeHtml(value) { return String(value || "") .replace( /&/g, "&" ) .replace( /</g, "<" ) .replace( />/g, ">" ) .replace( /"/g, """ ) .replace( /'/g, "'" ); }

Subscribe Form

Thanks for submitting!

  • Facebook
  • Twitter
  • LinkedIn

©2020 by latvorking

bottom of page