wpheadless-lib 1.1.9 → 1.1.10

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.
@@ -46,7 +46,7 @@ export declare function rewriteYoastUrls<T extends WPYoastHeadJson | undefined>(
46
46
  export declare function getYoastHead(entity?: YoastHolder, opts?: RewriteOpts): WPYoastHeadJson | undefined;
47
47
  export declare function buildYoastRobotsMeta(robots?: WPYoastHeadJson["robots"]): Metadata["robots"] | undefined;
48
48
  export declare function isYoastNoindex(yoast?: WPYoastHeadJson): boolean;
49
- export declare function getYoastSchema(entity?: YoastSchemaHolder, opts?: RewriteOpts): import("wpjsapi-lib").WPYoastSchema;
49
+ export declare function getYoastSchema(entity?: YoastSchemaHolder, opts?: RewriteOpts): unknown;
50
50
  export declare function stripBreadcrumbsFromSchema(schema: unknown): unknown;
51
51
  export declare function getFeaturedMedia(entity?: WithEmbeddedMedia): FeaturedMedia | undefined;
52
52
  export declare function getPlainTextExcerpt(html?: string, maxLength?: number): string | undefined;
package/dist/utils/seo.js CHANGED
@@ -19,6 +19,64 @@ const normalizeWpApiOrigin = (wpApiUrl) => {
19
19
  const cleaned = wpApiUrl.replace(/\/wp-json\/?$/, "");
20
20
  return getOrigin(cleaned);
21
21
  };
22
+ const resolveRewriteOrigins = (opts, canonical) => {
23
+ const targetOrigin = getOrigin(opts.siteUrl);
24
+ const sourceOrigin = normalizeWpApiOrigin(opts.wpApiUrl) || getOrigin(canonical);
25
+ if (!targetOrigin || !sourceOrigin || targetOrigin === sourceOrigin) {
26
+ return null;
27
+ }
28
+ return { targetOrigin, sourceOrigin };
29
+ };
30
+ const IMAGE_EXTENSIONS = [
31
+ ".jpg",
32
+ ".jpeg",
33
+ ".png",
34
+ ".webp",
35
+ ".gif",
36
+ ".svg",
37
+ ".avif",
38
+ ".bmp",
39
+ ".tiff",
40
+ ];
41
+ const isImageUrl = (url) => {
42
+ const pathname = url.pathname.toLowerCase();
43
+ return (pathname.includes("/wp-content/uploads/") ||
44
+ IMAGE_EXTENSIONS.some((ext) => pathname.endsWith(ext)));
45
+ };
46
+ const rewriteSchemaValue = (value, { targetOrigin, sourceOrigin }) => {
47
+ if (Array.isArray(value)) {
48
+ return value.map((item) => rewriteSchemaValue(item, { targetOrigin, sourceOrigin }));
49
+ }
50
+ if (value && typeof value === "object") {
51
+ const obj = value;
52
+ const rewritten = {};
53
+ for (const [key, val] of Object.entries(obj)) {
54
+ rewritten[key] = rewriteSchemaValue(val, { targetOrigin, sourceOrigin });
55
+ }
56
+ return rewritten;
57
+ }
58
+ if (typeof value === "string") {
59
+ let parsed;
60
+ try {
61
+ parsed = new URL(value);
62
+ }
63
+ catch {
64
+ return value;
65
+ }
66
+ if (parsed.origin !== sourceOrigin)
67
+ return value;
68
+ if (isImageUrl(parsed))
69
+ return value;
70
+ return value.split(sourceOrigin).join(targetOrigin);
71
+ }
72
+ return value;
73
+ };
74
+ const rewriteYoastSchemaUrls = (schema, opts, canonical) => {
75
+ const origins = resolveRewriteOrigins(opts, canonical);
76
+ if (!origins)
77
+ return schema;
78
+ return rewriteSchemaValue(schema, origins);
79
+ };
22
80
  /**
23
81
  * Replace Yoast URLs so they match the frontend domain instead of the WP API domain.
24
82
  * It stringifies the object to replace origins and keeps structure intact.
@@ -26,14 +84,14 @@ const normalizeWpApiOrigin = (wpApiUrl) => {
26
84
  export function rewriteYoastUrls(yoast, opts = {}) {
27
85
  if (!yoast)
28
86
  return yoast;
29
- const targetOrigin = getOrigin(opts.siteUrl);
30
- const sourceOrigin = normalizeWpApiOrigin(opts.wpApiUrl) || getOrigin(yoast.canonical);
31
- if (!targetOrigin || !sourceOrigin || targetOrigin === sourceOrigin) {
87
+ const origins = resolveRewriteOrigins(opts, yoast.canonical);
88
+ if (!origins)
32
89
  return yoast;
33
- }
34
90
  try {
35
91
  const serialized = JSON.stringify(yoast);
36
- const replaced = serialized.split(sourceOrigin).join(targetOrigin);
92
+ const replaced = serialized
93
+ .split(origins.sourceOrigin)
94
+ .join(origins.targetOrigin);
37
95
  return JSON.parse(replaced);
38
96
  }
39
97
  catch {
@@ -112,8 +170,10 @@ export function isYoastNoindex(yoast) {
112
170
  return robots.index === false;
113
171
  }
114
172
  export function getYoastSchema(entity, opts) {
115
- const yoast = getYoastHead(entity, opts);
116
- return yoast?.schema || null;
173
+ const yoast = entity?.yoast_head_json;
174
+ if (!yoast?.schema)
175
+ return null;
176
+ return rewriteYoastSchemaUrls(yoast.schema, opts ?? {}, yoast.canonical) || null;
117
177
  }
118
178
  export function stripBreadcrumbsFromSchema(schema) {
119
179
  if (!schema || typeof schema !== "object")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wpheadless-lib",
3
- "version": "1.1.9",
3
+ "version": "1.1.10",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "dist/index.js",