payload-plugin-ploma 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -2
- package/dist/index.d.ts +78 -0
- package/dist/index.js +149 -0
- package/package.json +42 -4
package/README.md
CHANGED
|
@@ -1,3 +1,59 @@
|
|
|
1
|
-
#
|
|
1
|
+
# payload-plugin-ploma
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Publishes articles written in Ploma AI into a Payload collection. The site fetches them from Ploma AI, so no firewall rule is needed.
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
1. In Ploma AI: Settings → Publish to → Payload CMS → Plugin → Generate connection key.
|
|
8
|
+
2. Install the plugin and add it to `payload.config.ts`:
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import { plomaPlugin } from "payload-plugin-ploma";
|
|
12
|
+
|
|
13
|
+
export default buildConfig({
|
|
14
|
+
plugins: [
|
|
15
|
+
plomaPlugin({
|
|
16
|
+
serverUrl: "https://content.datablue.dev",
|
|
17
|
+
connectionKey: process.env.PLOMA_KEY!,
|
|
18
|
+
collection: "posts",
|
|
19
|
+
fields: {
|
|
20
|
+
title: "title",
|
|
21
|
+
slug: "slug",
|
|
22
|
+
excerpt: "excerpt",
|
|
23
|
+
body: "content", // an HTML string field by default
|
|
24
|
+
publishedAt: "publishedAt",
|
|
25
|
+
metaTitle: "meta.title",
|
|
26
|
+
metaDescription: "meta.description",
|
|
27
|
+
featuredImage: "heroImage",
|
|
28
|
+
},
|
|
29
|
+
mediaCollection: "media",
|
|
30
|
+
defaults: { category: "news" }, // anything the collection requires
|
|
31
|
+
urlFor: (doc) => `https://example.com/blog/${doc.slug}`,
|
|
32
|
+
}),
|
|
33
|
+
],
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Posts are created every five minutes on a long-running server. Re-sending an article updates the post with the same slug.
|
|
38
|
+
|
|
39
|
+
## Rich text bodies
|
|
40
|
+
|
|
41
|
+
A rich text field doesn't take a string. Convert the HTML yourself:
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
convertBody: (article, payload) =>
|
|
45
|
+
convertHTMLToLexical({ html: article.html, editorConfig, JSDOM }),
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Serverless hosting
|
|
49
|
+
|
|
50
|
+
A timer doesn't survive on serverless. Set `intervalMinutes: 0` and call the endpoint from a cron:
|
|
51
|
+
|
|
52
|
+
```
|
|
53
|
+
POST https://example.com/api/ploma/sync
|
|
54
|
+
Authorization: Bearer <connection key>
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Drafts
|
|
58
|
+
|
|
59
|
+
By default Payload's own drafts are used (`_status`). Whether articles arrive as drafts is set in Ploma AI. If the collection uses its own status field instead, pass `status: { field: "status", draft: "draft", published: "published" }`.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { Config, Payload } from "payload";
|
|
2
|
+
/** One article as Ploma AI hands it over. */
|
|
3
|
+
export interface PlomaArticle {
|
|
4
|
+
id: string;
|
|
5
|
+
slug: string;
|
|
6
|
+
title: string;
|
|
7
|
+
excerpt: string;
|
|
8
|
+
metaTitle: string;
|
|
9
|
+
metaDescription: string;
|
|
10
|
+
html: string;
|
|
11
|
+
markdown: string;
|
|
12
|
+
featuredImage: {
|
|
13
|
+
url: string;
|
|
14
|
+
alt: string;
|
|
15
|
+
} | null;
|
|
16
|
+
status: "draft" | "publish";
|
|
17
|
+
/** ISO time the post should carry as its date. */
|
|
18
|
+
publishAt: string;
|
|
19
|
+
}
|
|
20
|
+
export interface PlomaPluginOptions {
|
|
21
|
+
/** The Ploma AI address, e.g. https://content.datablue.dev */
|
|
22
|
+
serverUrl: string;
|
|
23
|
+
/** The connection key from Ploma AI: Settings > Publish to > Payload CMS > Plugin. */
|
|
24
|
+
connectionKey: string;
|
|
25
|
+
/** The collection posts are created in, e.g. "posts". */
|
|
26
|
+
collection: string;
|
|
27
|
+
/**
|
|
28
|
+
* Which of the collection's fields each part of an article goes into. A
|
|
29
|
+
* dotted path reaches a nested field, e.g. "meta.title". Leave a part out
|
|
30
|
+
* to skip it. `slug` is how a post is recognised when it is sent again.
|
|
31
|
+
*/
|
|
32
|
+
fields: {
|
|
33
|
+
title?: string;
|
|
34
|
+
slug: string;
|
|
35
|
+
excerpt?: string;
|
|
36
|
+
body: string;
|
|
37
|
+
publishedAt?: string;
|
|
38
|
+
metaTitle?: string;
|
|
39
|
+
metaDescription?: string;
|
|
40
|
+
featuredImage?: string;
|
|
41
|
+
};
|
|
42
|
+
/** What the body field holds. Default "html". */
|
|
43
|
+
bodyFormat?: "html" | "markdown";
|
|
44
|
+
/**
|
|
45
|
+
* Builds the body field's value yourself - needed for a rich text field,
|
|
46
|
+
* whose value isn't a string. Takes over from `bodyFormat`.
|
|
47
|
+
*/
|
|
48
|
+
convertBody?: (article: PlomaArticle, payload: Payload) => unknown | Promise<unknown>;
|
|
49
|
+
/** The upload collection the featured image is saved into. Required with `fields.featuredImage`. */
|
|
50
|
+
mediaCollection?: string;
|
|
51
|
+
/** The media collection's alt text field. Default "alt". */
|
|
52
|
+
mediaAltField?: string;
|
|
53
|
+
/**
|
|
54
|
+
* How a draft is told apart from a live post. "versions" (default) uses
|
|
55
|
+
* Payload's own drafts; give a field to set it to a value instead.
|
|
56
|
+
*/
|
|
57
|
+
status?: "versions" | {
|
|
58
|
+
field: string;
|
|
59
|
+
draft: unknown;
|
|
60
|
+
published: unknown;
|
|
61
|
+
};
|
|
62
|
+
/** Values added to every post, e.g. a required category or author. */
|
|
63
|
+
defaults?: Record<string, unknown>;
|
|
64
|
+
/** The live address of a created post, reported back to Ploma AI. */
|
|
65
|
+
urlFor?: (doc: Record<string, unknown>, article: PlomaArticle) => string | undefined;
|
|
66
|
+
/** Minutes between checks on a long-running server. 0 turns it off. Default 5. */
|
|
67
|
+
intervalMinutes?: number;
|
|
68
|
+
/** Path of the endpoint that runs a check on demand, for a cron on serverless hosting. Default "/ploma/sync". */
|
|
69
|
+
syncPath?: string;
|
|
70
|
+
}
|
|
71
|
+
export interface PlomaSyncResult {
|
|
72
|
+
ok: boolean;
|
|
73
|
+
created: number;
|
|
74
|
+
errors: string[];
|
|
75
|
+
}
|
|
76
|
+
/** Fetches whatever Ploma AI has queued and creates it as posts. */
|
|
77
|
+
export declare function syncPloma(payload: Payload, options: PlomaPluginOptions): Promise<PlomaSyncResult>;
|
|
78
|
+
export declare const plomaPlugin: (options: PlomaPluginOptions) => (config: Config) => Config;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
function setPath(target, path, value) {
|
|
2
|
+
const parts = path.split(".");
|
|
3
|
+
let node = target;
|
|
4
|
+
for (const part of parts.slice(0, -1)) {
|
|
5
|
+
const next = node[part];
|
|
6
|
+
if (typeof next !== "object" || next === null)
|
|
7
|
+
node[part] = {};
|
|
8
|
+
node = node[part];
|
|
9
|
+
}
|
|
10
|
+
node[parts[parts.length - 1]] = value;
|
|
11
|
+
}
|
|
12
|
+
async function callPloma(options, method, path, body) {
|
|
13
|
+
const res = await fetch(`${options.serverUrl.replace(/\/+$/, "")}/api/wp${path}`, {
|
|
14
|
+
method,
|
|
15
|
+
headers: {
|
|
16
|
+
Authorization: `Bearer ${options.connectionKey}`,
|
|
17
|
+
Accept: "application/json",
|
|
18
|
+
"Content-Type": "application/json",
|
|
19
|
+
},
|
|
20
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
21
|
+
});
|
|
22
|
+
const data = (await res.json().catch(() => ({})));
|
|
23
|
+
if (!res.ok)
|
|
24
|
+
throw new Error(typeof data.error === "string" ? data.error : `Ploma AI answered ${res.status}.`);
|
|
25
|
+
return data;
|
|
26
|
+
}
|
|
27
|
+
async function saveImage(payload, options, image) {
|
|
28
|
+
const res = await fetch(image.url);
|
|
29
|
+
if (!res.ok)
|
|
30
|
+
throw new Error(`Couldn't download the image (${res.status}).`);
|
|
31
|
+
const data = Buffer.from(await res.arrayBuffer());
|
|
32
|
+
const name = new URL(image.url).pathname.split("/").pop() || "image";
|
|
33
|
+
const mimetype = res.headers.get("content-type")?.split(";")[0] || "image/jpeg";
|
|
34
|
+
const doc = await payload.create({
|
|
35
|
+
collection: options.mediaCollection,
|
|
36
|
+
data: { [options.mediaAltField ?? "alt"]: image.alt },
|
|
37
|
+
file: { data, mimetype, name, size: data.length },
|
|
38
|
+
});
|
|
39
|
+
return doc.id;
|
|
40
|
+
}
|
|
41
|
+
async function bodyFor(article, options, payload) {
|
|
42
|
+
if (options.convertBody)
|
|
43
|
+
return options.convertBody(article, payload);
|
|
44
|
+
return options.bodyFormat === "markdown" ? article.markdown : article.html;
|
|
45
|
+
}
|
|
46
|
+
async function upsert(payload, options, article) {
|
|
47
|
+
const { fields } = options;
|
|
48
|
+
const data = { ...options.defaults };
|
|
49
|
+
if (fields.title)
|
|
50
|
+
setPath(data, fields.title, article.title);
|
|
51
|
+
setPath(data, fields.slug, article.slug);
|
|
52
|
+
if (fields.excerpt)
|
|
53
|
+
setPath(data, fields.excerpt, article.excerpt);
|
|
54
|
+
setPath(data, fields.body, await bodyFor(article, options, payload));
|
|
55
|
+
if (fields.publishedAt)
|
|
56
|
+
setPath(data, fields.publishedAt, article.publishAt);
|
|
57
|
+
if (fields.metaTitle)
|
|
58
|
+
setPath(data, fields.metaTitle, article.metaTitle);
|
|
59
|
+
if (fields.metaDescription)
|
|
60
|
+
setPath(data, fields.metaDescription, article.metaDescription);
|
|
61
|
+
if (fields.featuredImage && article.featuredImage) {
|
|
62
|
+
if (!options.mediaCollection)
|
|
63
|
+
throw new Error("Set mediaCollection to save the featured image.");
|
|
64
|
+
setPath(data, fields.featuredImage, await saveImage(payload, options, article.featuredImage));
|
|
65
|
+
}
|
|
66
|
+
const live = article.status === "publish";
|
|
67
|
+
const statusOption = options.status ?? "versions";
|
|
68
|
+
if (statusOption !== "versions") {
|
|
69
|
+
setPath(data, statusOption.field, live ? statusOption.published : statusOption.draft);
|
|
70
|
+
}
|
|
71
|
+
else if (live) {
|
|
72
|
+
data._status = "published";
|
|
73
|
+
}
|
|
74
|
+
const draft = statusOption === "versions" && !live;
|
|
75
|
+
const found = await payload.find({
|
|
76
|
+
collection: options.collection,
|
|
77
|
+
where: { [fields.slug]: { equals: article.slug } },
|
|
78
|
+
limit: 1,
|
|
79
|
+
draft: true,
|
|
80
|
+
depth: 0,
|
|
81
|
+
});
|
|
82
|
+
const existing = found.docs?.[0];
|
|
83
|
+
return existing
|
|
84
|
+
? payload.update({ collection: options.collection, id: existing.id, data, draft })
|
|
85
|
+
: payload.create({ collection: options.collection, data, draft });
|
|
86
|
+
}
|
|
87
|
+
let running = false;
|
|
88
|
+
/** Fetches whatever Ploma AI has queued and creates it as posts. */
|
|
89
|
+
export async function syncPloma(payload, options) {
|
|
90
|
+
if (running)
|
|
91
|
+
return { ok: true, created: 0, errors: [] };
|
|
92
|
+
running = true;
|
|
93
|
+
const result = { ok: true, created: 0, errors: [] };
|
|
94
|
+
try {
|
|
95
|
+
const { articles } = (await callPloma(options, "GET", "/articles"));
|
|
96
|
+
for (const article of articles ?? []) {
|
|
97
|
+
try {
|
|
98
|
+
const doc = (await upsert(payload, options, article));
|
|
99
|
+
await callPloma(options, "POST", `/articles/${encodeURIComponent(article.id)}/ack`, {
|
|
100
|
+
status: article.status,
|
|
101
|
+
...(options.urlFor ? { url: options.urlFor(doc, article) } : {}),
|
|
102
|
+
});
|
|
103
|
+
result.created++;
|
|
104
|
+
}
|
|
105
|
+
catch (err) {
|
|
106
|
+
const message = err instanceof Error ? err.message : "Unknown error";
|
|
107
|
+
result.errors.push(`${article.slug}: ${message}`);
|
|
108
|
+
await callPloma(options, "POST", `/articles/${encodeURIComponent(article.id)}/fail`, {
|
|
109
|
+
error: message.slice(0, 500),
|
|
110
|
+
}).catch(() => undefined);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
catch (err) {
|
|
115
|
+
result.errors.push(err instanceof Error ? err.message : "Couldn't reach Ploma AI.");
|
|
116
|
+
}
|
|
117
|
+
finally {
|
|
118
|
+
running = false;
|
|
119
|
+
}
|
|
120
|
+
result.ok = result.errors.length === 0;
|
|
121
|
+
return result;
|
|
122
|
+
}
|
|
123
|
+
export const plomaPlugin = (options) => (config) => {
|
|
124
|
+
const interval = options.intervalMinutes ?? 5;
|
|
125
|
+
return {
|
|
126
|
+
...config,
|
|
127
|
+
endpoints: [
|
|
128
|
+
...(config.endpoints ?? []),
|
|
129
|
+
{
|
|
130
|
+
path: options.syncPath ?? "/ploma/sync",
|
|
131
|
+
method: "post",
|
|
132
|
+
handler: async (req) => {
|
|
133
|
+
// Only someone holding the connection key may trigger a check.
|
|
134
|
+
if (req.headers.get("authorization") !== `Bearer ${options.connectionKey}`) {
|
|
135
|
+
return Response.json({ error: "Not allowed." }, { status: 401 });
|
|
136
|
+
}
|
|
137
|
+
return Response.json(await syncPloma(req.payload, options));
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
],
|
|
141
|
+
onInit: async (payload) => {
|
|
142
|
+
await config.onInit?.(payload);
|
|
143
|
+
if (interval > 0) {
|
|
144
|
+
const timer = setInterval(() => void syncPloma(payload, options), interval * 60_000);
|
|
145
|
+
timer.unref?.();
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "payload-plugin-ploma",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Publishes articles written in Ploma AI into a Payload collection. The site fetches them, so no firewall rule is needed.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"typecheck": "tsc --noEmit"
|
|
14
|
+
},
|
|
15
|
+
"peerDependencies": {
|
|
16
|
+
"payload": "^3.0.0"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"payload": "^3.0.0",
|
|
20
|
+
"typescript": "^5.6.0"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/arun-andiselvam/ai-content-writer.git",
|
|
26
|
+
"directory": "payload-plugin"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"payload",
|
|
30
|
+
"payloadcms",
|
|
31
|
+
"payload-plugin",
|
|
32
|
+
"ploma",
|
|
33
|
+
"ai-content"
|
|
34
|
+
],
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"exports": {
|
|
39
|
+
".": {
|
|
40
|
+
"types": "./dist/index.d.ts",
|
|
41
|
+
"import": "./dist/index.js"
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|