feat: add X parent post article extraction and validation
This commit is contained in:
37
src/lib/article.js
Normal file
37
src/lib/article.js
Normal file
@@ -0,0 +1,37 @@
|
||||
"use strict";
|
||||
|
||||
function normalizeWhitespace(text) {
|
||||
return text.replace(/\s+/g, " ").trim();
|
||||
}
|
||||
|
||||
function extractArticleFromParent(parentPost) {
|
||||
if (!parentPost || typeof parentPost !== "object") {
|
||||
return { ok: false, error: "parent_post_missing" };
|
||||
}
|
||||
|
||||
const article = parentPost.article;
|
||||
if (!article || typeof article.body !== "string") {
|
||||
return { ok: false, error: "not_article" };
|
||||
}
|
||||
|
||||
const rawBody = normalizeWhitespace(article.body);
|
||||
if (!rawBody) {
|
||||
return { ok: false, error: "empty_article" };
|
||||
}
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
article: {
|
||||
xArticleId: article.id || null,
|
||||
parentPostId: parentPost.id,
|
||||
authorId: parentPost.authorId || null,
|
||||
title: article.title || "Untitled",
|
||||
content: rawBody,
|
||||
charCount: rawBody.length,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
extractArticleFromParent,
|
||||
};
|
||||
Reference in New Issue
Block a user