@yourbright/emdash-analytics-plugin 0.1.6 → 0.1.8

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.
Files changed (2) hide show
  1. package/dist/index.js +81 -16
  2. 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 managedMap = await getManagedContentMap(config.siteOrigin);
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,56 @@ 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: { contentId: id },
1219
+ limit: 20
1220
+ });
1221
+ const match = byId.items.map((item) => item.data).find((page) => page.contentCollection === collection);
1222
+ if (match) return match;
1223
+ }
1224
+ if (slug) {
1225
+ const bySlug = await ctx.storage.pages.query({
1226
+ where: { contentSlug: slug },
1227
+ limit: 20
1228
+ });
1229
+ const match = bySlug.items.map((item) => item.data).find((page) => page.contentCollection === collection);
1230
+ if (match) return match;
1231
+ }
1232
+ return null;
1233
+ }
1234
+ async function resolveContentRef(siteOrigin, page, collection, id, slug) {
1235
+ if (collection !== "posts") {
1236
+ return {
1237
+ collection: "posts",
1238
+ id: page.contentId || id || pageStorageId(page.urlPath),
1239
+ slug: page.contentSlug || slug || null,
1240
+ urlPath: page.urlPath,
1241
+ title: page.title,
1242
+ excerpt: void 0,
1243
+ seoDescription: void 0
1244
+ };
1245
+ }
1246
+ const resolved = await resolveManagedContent(
1247
+ collection,
1248
+ page.contentId || id,
1249
+ page.contentSlug || slug || void 0,
1250
+ siteOrigin
1251
+ );
1252
+ if (resolved) {
1253
+ return resolved;
1254
+ }
1255
+ return {
1256
+ collection: "posts",
1257
+ id: page.contentId || id || pageStorageId(page.urlPath),
1258
+ slug: page.contentSlug || slug || null,
1259
+ urlPath: page.urlPath,
1260
+ title: page.title,
1261
+ excerpt: void 0,
1262
+ seoDescription: void 0
1263
+ };
1264
+ }
1200
1265
  async function getFreshQueriesForPage(ctx, config, urlPath) {
1201
1266
  let queries = await getPageQueries(ctx, urlPath);
1202
1267
  const updatedAt = queries[0]?.updatedAt ? Date.parse(queries[0].updatedAt) : 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yourbright/emdash-analytics-plugin",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "Google Search Console and GA4 analytics plugin for EmDash",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",