@yourbright/emdash-analytics-plugin 0.1.6 → 0.1.7
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/dist/index.js +87 -16
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -297,6 +297,27 @@ async function getManagedContentMap(siteOrigin) {
|
|
|
297
297
|
void siteOrigin;
|
|
298
298
|
return managed;
|
|
299
299
|
}
|
|
300
|
+
async function resolveManagedContent(collection, id, slug, siteOrigin) {
|
|
301
|
+
if (collection !== "posts") return null;
|
|
302
|
+
const ref = id || slug;
|
|
303
|
+
if (!ref) return null;
|
|
304
|
+
const result = await getEmDashEntry("posts", ref);
|
|
305
|
+
const entry = result.entry;
|
|
306
|
+
if (!entry) return null;
|
|
307
|
+
const entryId = typeof entry.id === "string" ? entry.id : ref;
|
|
308
|
+
const entrySlug = typeof entry.slug === "string" ? entry.slug : null;
|
|
309
|
+
const data = entry.data ?? {};
|
|
310
|
+
const urlPath = `/blog/${entrySlug || entryId}/`;
|
|
311
|
+
return {
|
|
312
|
+
collection: "posts",
|
|
313
|
+
id: entryId,
|
|
314
|
+
slug: entrySlug,
|
|
315
|
+
urlPath,
|
|
316
|
+
title: typeof data.title === "string" ? data.title : entrySlug || entryId,
|
|
317
|
+
excerpt: typeof data.excerpt === "string" ? data.excerpt : void 0,
|
|
318
|
+
seoDescription: typeof data.seo_description === "string" ? data.seo_description : void 0
|
|
319
|
+
};
|
|
320
|
+
}
|
|
300
321
|
function pageStorageId(urlPath) {
|
|
301
322
|
return stableStorageId(urlPath);
|
|
302
323
|
}
|
|
@@ -1048,25 +1069,19 @@ function buildOverviewData(summary, freshness, allPages, topOpportunities, topUn
|
|
|
1048
1069
|
}
|
|
1049
1070
|
async function getContentContext(ctx, collection, id, slug) {
|
|
1050
1071
|
const config = await requireConfig(ctx);
|
|
1051
|
-
const
|
|
1052
|
-
let contentRef = null;
|
|
1053
|
-
if (id) {
|
|
1054
|
-
contentRef = Array.from(managedMap.values()).find(
|
|
1055
|
-
(entry) => entry.collection === collection && entry.id === id
|
|
1056
|
-
) || null;
|
|
1057
|
-
}
|
|
1058
|
-
if (!contentRef && slug) {
|
|
1059
|
-
contentRef = Array.from(managedMap.values()).find(
|
|
1060
|
-
(entry) => entry.collection === collection && entry.slug === slug
|
|
1061
|
-
) || null;
|
|
1062
|
-
}
|
|
1063
|
-
if (!contentRef) {
|
|
1064
|
-
throw new PluginRouteError("NOT_FOUND", "Managed content not found", 404);
|
|
1065
|
-
}
|
|
1066
|
-
const page = await loadPage(ctx, contentRef.urlPath);
|
|
1072
|
+
const page = await findContentPage(ctx, collection, id, slug);
|
|
1067
1073
|
if (!page) {
|
|
1068
1074
|
throw new PluginRouteError("NOT_FOUND", "Analytics data not found for content", 404);
|
|
1069
1075
|
}
|
|
1076
|
+
const contentRef = await resolveContentRef(config.siteOrigin, page, collection, id, slug) || {
|
|
1077
|
+
collection: "posts",
|
|
1078
|
+
id: page.contentId || id || pageStorageId(page.urlPath),
|
|
1079
|
+
slug: page.contentSlug || slug || null,
|
|
1080
|
+
urlPath: page.urlPath,
|
|
1081
|
+
title: page.title,
|
|
1082
|
+
excerpt: void 0,
|
|
1083
|
+
seoDescription: void 0
|
|
1084
|
+
};
|
|
1070
1085
|
const queries = await getFreshQueriesForPage(ctx, config, contentRef.urlPath);
|
|
1071
1086
|
const windows = buildWindows();
|
|
1072
1087
|
const score = scorePage(page, queries);
|
|
@@ -1197,6 +1212,62 @@ async function loadPage(ctx, urlPath) {
|
|
|
1197
1212
|
const page = await ctx.storage.pages.get(pageStorageId(urlPath));
|
|
1198
1213
|
return page ?? null;
|
|
1199
1214
|
}
|
|
1215
|
+
async function findContentPage(ctx, collection, id, slug) {
|
|
1216
|
+
if (id) {
|
|
1217
|
+
const byId = await ctx.storage.pages.query({
|
|
1218
|
+
where: {
|
|
1219
|
+
contentCollection: collection,
|
|
1220
|
+
contentId: id
|
|
1221
|
+
},
|
|
1222
|
+
limit: 1
|
|
1223
|
+
});
|
|
1224
|
+
const match = byId.items[0]?.data;
|
|
1225
|
+
if (match) return match;
|
|
1226
|
+
}
|
|
1227
|
+
if (slug) {
|
|
1228
|
+
const bySlug = await ctx.storage.pages.query({
|
|
1229
|
+
where: {
|
|
1230
|
+
contentCollection: collection,
|
|
1231
|
+
contentSlug: slug
|
|
1232
|
+
},
|
|
1233
|
+
limit: 1
|
|
1234
|
+
});
|
|
1235
|
+
const match = bySlug.items[0]?.data;
|
|
1236
|
+
if (match) return match;
|
|
1237
|
+
}
|
|
1238
|
+
return null;
|
|
1239
|
+
}
|
|
1240
|
+
async function resolveContentRef(siteOrigin, page, collection, id, slug) {
|
|
1241
|
+
if (collection !== "posts") {
|
|
1242
|
+
return {
|
|
1243
|
+
collection: "posts",
|
|
1244
|
+
id: page.contentId || id || pageStorageId(page.urlPath),
|
|
1245
|
+
slug: page.contentSlug || slug || null,
|
|
1246
|
+
urlPath: page.urlPath,
|
|
1247
|
+
title: page.title,
|
|
1248
|
+
excerpt: void 0,
|
|
1249
|
+
seoDescription: void 0
|
|
1250
|
+
};
|
|
1251
|
+
}
|
|
1252
|
+
const resolved = await resolveManagedContent(
|
|
1253
|
+
collection,
|
|
1254
|
+
page.contentId || id,
|
|
1255
|
+
page.contentSlug || slug || void 0,
|
|
1256
|
+
siteOrigin
|
|
1257
|
+
);
|
|
1258
|
+
if (resolved) {
|
|
1259
|
+
return resolved;
|
|
1260
|
+
}
|
|
1261
|
+
return {
|
|
1262
|
+
collection: "posts",
|
|
1263
|
+
id: page.contentId || id || pageStorageId(page.urlPath),
|
|
1264
|
+
slug: page.contentSlug || slug || null,
|
|
1265
|
+
urlPath: page.urlPath,
|
|
1266
|
+
title: page.title,
|
|
1267
|
+
excerpt: void 0,
|
|
1268
|
+
seoDescription: void 0
|
|
1269
|
+
};
|
|
1270
|
+
}
|
|
1200
1271
|
async function getFreshQueriesForPage(ctx, config, urlPath) {
|
|
1201
1272
|
let queries = await getPageQueries(ctx, urlPath);
|
|
1202
1273
|
const updatedAt = queries[0]?.updatedAt ? Date.parse(queries[0].updatedAt) : 0;
|