@umituz/web-seo 2.0.21 → 2.1.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.
Files changed (103) hide show
  1. package/dist/domains/seo/components/JsonLd.d.ts +7 -6
  2. package/dist/domains/seo/components/JsonLd.js +26 -77
  3. package/dist/domains/seo/components/SEO.d.ts +7 -0
  4. package/dist/domains/seo/components/SEO.js +10 -33
  5. package/dist/domains/seo/core/constants.d.ts +15 -0
  6. package/dist/domains/seo/core/constants.js +15 -0
  7. package/dist/domains/seo/core/deep-equal.d.ts +8 -0
  8. package/dist/domains/seo/core/deep-equal.js +28 -0
  9. package/dist/domains/seo/core/dom-queue.d.ts +10 -0
  10. package/dist/domains/seo/core/dom-queue.js +37 -0
  11. package/dist/domains/seo/core/escape.d.ts +12 -0
  12. package/dist/domains/seo/core/escape.js +41 -0
  13. package/dist/domains/seo/core/hash.d.ts +9 -0
  14. package/dist/domains/seo/core/hash.js +29 -0
  15. package/dist/domains/seo/core/init.d.ts +15 -2
  16. package/dist/domains/seo/core/init.js +38 -18
  17. package/dist/domains/seo/core/seo-strategies.d.ts +29 -0
  18. package/dist/domains/seo/core/seo-strategies.js +694 -0
  19. package/dist/domains/seo/core/seo-strategy.d.ts +37 -0
  20. package/dist/domains/seo/core/seo-strategy.js +14 -0
  21. package/dist/domains/seo/hooks/useJsonLd.d.ts +6 -7
  22. package/dist/domains/seo/hooks/useJsonLd.js +27 -61
  23. package/dist/domains/seo/hooks/useSEO.d.ts +6 -0
  24. package/dist/domains/seo/hooks/useSEO.js +7 -29
  25. package/dist/domains/seo/ssr/helmet.d.ts +25 -0
  26. package/dist/domains/seo/ssr/helmet.js +23 -0
  27. package/dist/domains/seo/ssr/index.d.ts +10 -0
  28. package/dist/domains/seo/ssr/index.js +8 -0
  29. package/dist/domains/seo/ssr/renderer.d.ts +11 -36
  30. package/dist/domains/seo/ssr/renderer.js +51 -285
  31. package/dist/domains/seo/ssr/serializer.d.ts +5 -60
  32. package/dist/domains/seo/ssr/serializer.js +26 -166
  33. package/dist/domains/seo/types/index.d.ts +0 -4
  34. package/dist/domains/seo/types/schema.d.ts +22 -1
  35. package/dist/domains/seo/utils/dom/dom-context.d.ts +13 -0
  36. package/dist/domains/seo/utils/dom/dom-context.js +62 -0
  37. package/dist/domains/seo/utils/dom/dom-mutator.d.ts +31 -0
  38. package/dist/domains/seo/utils/dom/dom-mutator.js +89 -0
  39. package/dist/domains/seo/utils/dom/index.d.ts +1 -0
  40. package/dist/domains/seo/utils/dom/index.js +1 -0
  41. package/dist/domains/seo/utils/index.d.ts +3 -4
  42. package/dist/domains/seo/utils/index.js +3 -5
  43. package/dist/domains/seo/utils/schema-generators/base.d.ts +25 -0
  44. package/dist/domains/seo/utils/schema-generators/base.js +50 -0
  45. package/dist/domains/seo/utils/schema-generators/factory.d.ts +27 -0
  46. package/dist/domains/seo/utils/schema-generators/factory.js +49 -0
  47. package/dist/domains/seo/utils/schema-generators/generators/article.d.ts +4 -0
  48. package/dist/domains/seo/utils/schema-generators/generators/article.js +39 -0
  49. package/dist/domains/seo/utils/schema-generators/generators/breadcrumb.d.ts +6 -0
  50. package/dist/domains/seo/utils/schema-generators/generators/breadcrumb.js +18 -0
  51. package/dist/domains/seo/utils/schema-generators/generators/course.d.ts +3 -0
  52. package/dist/domains/seo/utils/schema-generators/generators/course.js +70 -0
  53. package/dist/domains/seo/utils/schema-generators/generators/faq.d.ts +3 -0
  54. package/dist/domains/seo/utils/schema-generators/generators/faq.js +21 -0
  55. package/dist/domains/seo/utils/schema-generators/generators/local-business.d.ts +3 -0
  56. package/dist/domains/seo/utils/schema-generators/generators/local-business.js +45 -0
  57. package/dist/domains/seo/utils/schema-generators/generators/organization.d.ts +3 -0
  58. package/dist/domains/seo/utils/schema-generators/generators/organization.js +25 -0
  59. package/dist/domains/seo/utils/schema-generators/generators/person.d.ts +3 -0
  60. package/dist/domains/seo/utils/schema-generators/generators/person.js +27 -0
  61. package/dist/domains/seo/utils/schema-generators/generators/product.d.ts +3 -0
  62. package/dist/domains/seo/utils/schema-generators/generators/product.js +40 -0
  63. package/dist/domains/seo/utils/schema-generators/generators/video.d.ts +3 -0
  64. package/dist/domains/seo/utils/schema-generators/generators/video.js +23 -0
  65. package/dist/domains/seo/utils/schema-generators/generators/website.d.ts +3 -0
  66. package/dist/domains/seo/utils/schema-generators/generators/website.js +30 -0
  67. package/dist/domains/seo/utils/schema-generators/index.d.ts +15 -2
  68. package/dist/domains/seo/utils/schema-generators/index.js +13 -2
  69. package/dist/domains/seo/utils/schema-generators/types.d.ts +142 -0
  70. package/dist/domains/seo/utils/schema-generators/types.js +5 -0
  71. package/dist/domains/seo/utils/validation/hreflang-validator.d.ts +7 -0
  72. package/dist/domains/seo/utils/validation/hreflang-validator.js +13 -0
  73. package/dist/domains/seo/utils/validation/image-validator.d.ts +7 -0
  74. package/dist/domains/seo/utils/validation/image-validator.js +24 -0
  75. package/dist/domains/seo/utils/validation/index.d.ts +10 -2
  76. package/dist/domains/seo/utils/validation/index.js +10 -2
  77. package/dist/domains/seo/utils/validation/jsonld-validator.d.ts +9 -0
  78. package/dist/domains/seo/utils/validation/jsonld-validator.js +74 -0
  79. package/dist/domains/seo/utils/validation/language-code.d.ts +7 -0
  80. package/dist/domains/seo/utils/validation/language-code.js +8 -0
  81. package/dist/domains/seo/utils/validation/length-validator.d.ts +10 -0
  82. package/dist/domains/seo/utils/validation/length-validator.js +40 -0
  83. package/dist/domains/seo/utils/validation/seo-auditor.d.ts +17 -0
  84. package/dist/domains/seo/utils/validation/seo-auditor.js +144 -0
  85. package/dist/domains/seo/utils/validation/text-helpers.d.ts +7 -0
  86. package/dist/domains/seo/utils/validation/text-helpers.js +12 -0
  87. package/dist/domains/seo/utils/validation/url-validator.d.ts +8 -0
  88. package/dist/domains/seo/utils/validation/url-validator.js +26 -0
  89. package/dist/domains/seo/vite/index.d.ts +6 -4
  90. package/dist/domains/seo/vite/index.js +5 -3
  91. package/dist/domains/seo/vite/plugin.d.ts +3 -55
  92. package/dist/domains/seo/vite/plugin.js +28 -186
  93. package/dist/domains/seo/vite/presets.d.ts +14 -0
  94. package/dist/domains/seo/vite/presets.js +33 -0
  95. package/dist/domains/seo/vite/robots-generator.d.ts +9 -0
  96. package/dist/domains/seo/vite/robots-generator.js +36 -0
  97. package/dist/domains/seo/vite/sitemap-generator.d.ts +15 -0
  98. package/dist/domains/seo/vite/sitemap-generator.js +56 -0
  99. package/dist/domains/seo/vite/types.d.ts +29 -0
  100. package/dist/domains/seo/vite/types.js +4 -0
  101. package/dist/index.d.ts +8 -7
  102. package/dist/index.js +7 -6
  103. package/package.json +1 -1
@@ -201,7 +201,28 @@ export interface BookJsonLd extends Thing {
201
201
  bookFormat?: "AudiobookFormat" | "EBook" | "Hardcover" | "Paperback";
202
202
  aggregateRating?: AggregateRating;
203
203
  }
204
- export type JsonLdTypes = ArticleJsonLd | ProductJsonLd | OrganizationJsonLd | LocalBusinessJsonLd | BreadcrumbJsonLd | FAQJsonLd | WebSiteJsonLd | PersonJsonLd | VideoJsonLd | AudioJsonLd | BookJsonLd;
204
+ export interface CourseInstance {
205
+ "@type": "CourseInstance";
206
+ courseMode?: "online" | "onsite" | "blended" | string;
207
+ courseWorkload?: string;
208
+ instructor?: PersonOrOrganization;
209
+ inLanguage?: string;
210
+ startDate?: string;
211
+ endDate?: string;
212
+ }
213
+ export interface CourseJsonLd extends Thing {
214
+ "@type": "Course";
215
+ provider: PersonOrOrganization;
216
+ offers?: Offer | Offer[];
217
+ courseCode?: string;
218
+ educationalLevel?: string;
219
+ inLanguage?: string;
220
+ hasCourseInstance?: CourseInstance | CourseInstance[];
221
+ teaches?: string | string[];
222
+ aggregateRating?: AggregateRating;
223
+ review?: Review[];
224
+ }
225
+ export type JsonLdTypes = ArticleJsonLd | ProductJsonLd | OrganizationJsonLd | LocalBusinessJsonLd | BreadcrumbJsonLd | FAQJsonLd | WebSiteJsonLd | PersonJsonLd | VideoJsonLd | AudioJsonLd | BookJsonLd | CourseJsonLd;
205
226
  export type SchemaType = JsonLdTypes["@type"];
206
227
  export interface SchemaGeneratorOptions {
207
228
  /** Strict mode - throws errors instead of warnings */
@@ -0,0 +1,13 @@
1
+ /**
2
+ * DOMContext implementation.
3
+ *
4
+ * Implements the low-level `setMeta` / `setLink` operations that the SEO
5
+ * strategies call into. Each call is idempotent: it finds the existing
6
+ * <meta>/<link> node by attribute and updates it in place, or creates a
7
+ * new one tagged with `data-seo="true"` so `clearSEO` can remove it.
8
+ *
9
+ * Tagged with `data-seo` so `clearSEO` can later remove only the nodes
10
+ * this library owns, leaving user-added tags untouched.
11
+ */
12
+ import type { DOMContext } from "../../core/seo-strategy.js";
13
+ export declare const createDOMContext: () => DOMContext;
@@ -0,0 +1,62 @@
1
+ /**
2
+ * DOMContext implementation.
3
+ *
4
+ * Implements the low-level `setMeta` / `setLink` operations that the SEO
5
+ * strategies call into. Each call is idempotent: it finds the existing
6
+ * <meta>/<link> node by attribute and updates it in place, or creates a
7
+ * new one tagged with `data-seo="true"` so `clearSEO` can remove it.
8
+ *
9
+ * Tagged with `data-seo` so `clearSEO` can later remove only the nodes
10
+ * this library owns, leaving user-added tags untouched.
11
+ */
12
+ export const createDOMContext = () => {
13
+ const setMeta = (name, content) => {
14
+ if (typeof document === "undefined")
15
+ return;
16
+ if (content === undefined)
17
+ return;
18
+ let element = document.querySelector(`meta[name="${name}"]`)
19
+ ?? document.querySelector(`meta[property="${name}"]`);
20
+ if (!element) {
21
+ element = document.createElement("meta");
22
+ // Decide whether the tag uses name= or property= based on the prefix
23
+ // so a single API works for both classic <meta name> and OG <meta property>.
24
+ if (name.startsWith("og:") || name.startsWith("article:") || name.startsWith("profile:") || name.startsWith("music:") || name.startsWith("product:")) {
25
+ element.setAttribute("property", name);
26
+ }
27
+ else {
28
+ element.setAttribute("name", name);
29
+ }
30
+ element.setAttribute("data-seo", "true");
31
+ document.head.appendChild(element);
32
+ }
33
+ element.setAttribute("content", content);
34
+ };
35
+ const setLink = (attrs) => {
36
+ if (typeof document === "undefined")
37
+ return;
38
+ const rel = attrs.rel;
39
+ if (!rel)
40
+ return;
41
+ // Build a precise selector so we replace only the matching node.
42
+ const parts = [`link[rel="${rel}"]`];
43
+ if (attrs.href)
44
+ parts.push(`[href="${attrs.href}"]`);
45
+ if (attrs.sizes)
46
+ parts.push(`[sizes="${attrs.sizes}"]`);
47
+ if (attrs.type)
48
+ parts.push(`[type="${attrs.type}"]`);
49
+ if (attrs.hreflang)
50
+ parts.push(`[hreflang="${attrs.hreflang}"]`);
51
+ let element = document.querySelector(parts.join(""));
52
+ if (!element) {
53
+ element = document.createElement("link");
54
+ element.setAttribute("data-seo", "true");
55
+ document.head.appendChild(element);
56
+ }
57
+ for (const [key, value] of Object.entries(attrs)) {
58
+ element.setAttribute(key, value);
59
+ }
60
+ };
61
+ return { setMeta, setLink };
62
+ };
@@ -0,0 +1,31 @@
1
+ /**
2
+ * DOM mutator.
3
+ *
4
+ * Public entry point for applying SEO options to the live DOM. All work
5
+ * is delegated to the strategy registry; this file is just a thin
6
+ * orchestrator + the few operations that don't fit the Strategy shape
7
+ * (clear, snapshot).
8
+ */
9
+ import type { LinkTag, MetaTag, SEOptions } from "../../types/index.js";
10
+ /**
11
+ * Apply SEO options to the live DOM.
12
+ *
13
+ * Mutations are batched in a single requestAnimationFrame pass to avoid
14
+ * layout thrashing and to give us a single try/catch boundary.
15
+ */
16
+ export declare const updateSEO: (options: SEOptions) => void;
17
+ /**
18
+ * Remove every node the library has injected and reset the document title.
19
+ * Useful for tests, SPAs that swap the entire head, or "reset" buttons.
20
+ */
21
+ export declare const clearSEO: () => void;
22
+ /**
23
+ * Snapshot the current SEO-relevant head into a plain object.
24
+ * Mostly useful for tests and SSR hydration debugging.
25
+ */
26
+ export declare const getCurrentSEOState: () => {
27
+ title: string;
28
+ lang: string;
29
+ metaTags: MetaTag[];
30
+ linkTags: LinkTag[];
31
+ } | null;
@@ -0,0 +1,89 @@
1
+ /**
2
+ * DOM mutator.
3
+ *
4
+ * Public entry point for applying SEO options to the live DOM. All work
5
+ * is delegated to the strategy registry; this file is just a thin
6
+ * orchestrator + the few operations that don't fit the Strategy shape
7
+ * (clear, snapshot).
8
+ */
9
+ import { queueDOMUpdate } from "../../core/dom-queue.js";
10
+ import { SEO_STRATEGIES } from "../../core/seo-strategies.js";
11
+ import { createDOMContext } from "./dom-context.js";
12
+ /**
13
+ * Apply SEO options to the live DOM.
14
+ *
15
+ * Mutations are batched in a single requestAnimationFrame pass to avoid
16
+ * layout thrashing and to give us a single try/catch boundary.
17
+ */
18
+ export const updateSEO = (options) => {
19
+ if (typeof document === "undefined")
20
+ return;
21
+ queueDOMUpdate(() => {
22
+ const ctx = createDOMContext();
23
+ for (const strategy of SEO_STRATEGIES) {
24
+ if (!strategy.applies(options))
25
+ continue;
26
+ try {
27
+ strategy.applyDOM(options, ctx);
28
+ }
29
+ catch (error) {
30
+ // Surface strategy failures: the DOM is left in whatever state the
31
+ // previous strategy achieved, but that's recoverable on the next
32
+ // render. Silently swallowing would be much worse.
33
+ console.error(`[SEO] Strategy "${strategy.id}" failed:`, error);
34
+ }
35
+ }
36
+ });
37
+ };
38
+ /**
39
+ * Remove every node the library has injected and reset the document title.
40
+ * Useful for tests, SPAs that swap the entire head, or "reset" buttons.
41
+ */
42
+ export const clearSEO = () => {
43
+ if (typeof document === "undefined")
44
+ return;
45
+ document.querySelectorAll('meta[data-seo="true"]').forEach((el) => el.remove());
46
+ document.querySelectorAll('link[data-seo="true"]').forEach((el) => el.remove());
47
+ document.title = "";
48
+ };
49
+ /**
50
+ * Snapshot the current SEO-relevant head into a plain object.
51
+ * Mostly useful for tests and SSR hydration debugging.
52
+ */
53
+ export const getCurrentSEOState = () => {
54
+ if (typeof document === "undefined")
55
+ return null;
56
+ const metaTags = [];
57
+ document.querySelectorAll("meta").forEach((meta) => {
58
+ const name = meta.getAttribute("name");
59
+ const property = meta.getAttribute("property");
60
+ const content = meta.getAttribute("content");
61
+ if (!content)
62
+ return;
63
+ if (name)
64
+ metaTags.push({ name, content });
65
+ else if (property)
66
+ metaTags.push({ property, content });
67
+ });
68
+ const linkTags = [];
69
+ document.querySelectorAll("link").forEach((link) => {
70
+ const rel = link.getAttribute("rel");
71
+ const href = link.getAttribute("href");
72
+ if (!rel || !href)
73
+ return;
74
+ const tag = { rel, href };
75
+ const type = link.getAttribute("type");
76
+ const sizes = link.getAttribute("sizes");
77
+ if (type)
78
+ tag.type = type;
79
+ if (sizes)
80
+ tag.sizes = sizes;
81
+ linkTags.push(tag);
82
+ });
83
+ return {
84
+ title: document.title,
85
+ lang: document.documentElement.lang,
86
+ metaTags,
87
+ linkTags,
88
+ };
89
+ };
@@ -0,0 +1 @@
1
+ export { updateSEO, clearSEO, getCurrentSEOState } from "./dom-mutator.js";
@@ -0,0 +1 @@
1
+ export { updateSEO, clearSEO, getCurrentSEOState } from "./dom-mutator.js";
@@ -1,9 +1,8 @@
1
1
  /**
2
2
  * SEO Utils
3
3
  *
4
- * Utility functions for SEO management
4
+ * Public utility surface for SEO management.
5
5
  */
6
- export { updateSEO, clearSEO, getCurrentSEOState } from "./dom.js";
7
- export * from "./meta-tags/index.js";
6
+ export { updateSEO, clearSEO, getCurrentSEOState } from "./dom/index.js";
8
7
  export { generateArticleSchema, generateProductSchema, generateOrganizationSchema, generateLocalBusinessSchema, generateBreadcrumbSchema, generateFAQSchema, generateWebSiteSchema, generatePersonSchema, generateVideoSchema, } from "./schema-generators/index.js";
9
- export { validateSEO, auditSEO, isValidUrl, validateUrl, validateTitleLength, validateDescriptionLength, validateOpenGraphImage, validateHreflangEntry, truncateForSEO, generateSlug, } from "./validation/index.js";
8
+ export { validateSEO, auditSEO, isValidUrl, validateUrl, validateTitleLength, validateDescriptionLength, validateOpenGraphImage, validateHreflangEntry, truncateToTitle, truncateToDescription, generateSlug, } from "./validation/index.js";
@@ -1,13 +1,11 @@
1
1
  /**
2
2
  * SEO Utils
3
3
  *
4
- * Utility functions for SEO management
4
+ * Public utility surface for SEO management.
5
5
  */
6
6
  // Core DOM operations
7
- export { updateSEO, clearSEO, getCurrentSEOState } from "./dom.js";
8
- // Meta tag utilities
9
- export * from "./meta-tags/index.js";
7
+ export { updateSEO, clearSEO, getCurrentSEOState } from "./dom/index.js";
10
8
  // Schema generators
11
9
  export { generateArticleSchema, generateProductSchema, generateOrganizationSchema, generateLocalBusinessSchema, generateBreadcrumbSchema, generateFAQSchema, generateWebSiteSchema, generatePersonSchema, generateVideoSchema, } from "./schema-generators/index.js";
12
10
  // Validators
13
- export { validateSEO, auditSEO, isValidUrl, validateUrl, validateTitleLength, validateDescriptionLength, validateOpenGraphImage, validateHreflangEntry, truncateForSEO, generateSlug, } from "./validation/index.js";
11
+ export { validateSEO, auditSEO, isValidUrl, validateUrl, validateTitleLength, validateDescriptionLength, validateOpenGraphImage, validateHreflangEntry, truncateToTitle, truncateToDescription, generateSlug, } from "./validation/index.js";
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Schema generator base (Template Method).
3
+ *
4
+ * Each generator runs the same flow:
5
+ * 1. Validate required fields (warn in non-strict, throw in strict).
6
+ * 2. Build the schema body.
7
+ * 3. Return the assembled JSON-LD object.
8
+ *
9
+ * `buildSchema` wraps that flow so individual generators can focus on
10
+ * the schema body that actually differs between types.
11
+ */
12
+ import type { SchemaGeneratorOptions } from "../../types/schema.js";
13
+ type RequiredField = string | number | boolean | object | null | undefined;
14
+ export declare const validateRequired: (value: RequiredField, fieldName: string, strict?: boolean) => void;
15
+ export interface BuildContext<T> {
16
+ options: Required<SchemaGeneratorOptions>;
17
+ required(field: RequiredField, name: string): void;
18
+ set(input: Partial<T>): void;
19
+ }
20
+ export declare const createBuilder: <TInput, TOutput>(build: (input: TInput, ctx: BuildContext<TOutput>) => void) => (input: TInput, options?: SchemaGeneratorOptions) => TOutput;
21
+ export declare const withContext: <T extends string>(type: T) => {
22
+ "@context": string;
23
+ "@type": T;
24
+ };
25
+ export {};
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Schema generator base (Template Method).
3
+ *
4
+ * Each generator runs the same flow:
5
+ * 1. Validate required fields (warn in non-strict, throw in strict).
6
+ * 2. Build the schema body.
7
+ * 3. Return the assembled JSON-LD object.
8
+ *
9
+ * `buildSchema` wraps that flow so individual generators can focus on
10
+ * the schema body that actually differs between types.
11
+ */
12
+ import { SCHEMA_CONTEXT } from "../../core/constants.js";
13
+ const isMissing = (value) => {
14
+ if (value === null || value === undefined)
15
+ return true;
16
+ if (typeof value === "string")
17
+ return value.length === 0;
18
+ if (Array.isArray(value))
19
+ return value.length === 0;
20
+ return false;
21
+ };
22
+ export const validateRequired = (value, fieldName, strict = false) => {
23
+ if (isMissing(value)) {
24
+ const message = `[SEO] Schema validation: Required field "${fieldName}" is missing or empty`;
25
+ if (strict)
26
+ throw new Error(message);
27
+ console.warn(message);
28
+ }
29
+ };
30
+ export const createBuilder = (build) => (input, options) => {
31
+ const opts = { strict: false, validate: true, ...options };
32
+ const body = {};
33
+ const ctx = {
34
+ options: opts,
35
+ required(value, name) {
36
+ if (!opts.validate)
37
+ return;
38
+ validateRequired(value, name, opts.strict);
39
+ },
40
+ set(partial) {
41
+ Object.assign(body, partial);
42
+ },
43
+ };
44
+ build(input, ctx);
45
+ return body;
46
+ };
47
+ export const withContext = (type) => ({
48
+ "@context": SCHEMA_CONTEXT,
49
+ "@type": type,
50
+ });
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Schema generator factory.
3
+ *
4
+ * Lets callers dispatch on `@type` instead of remembering each generator
5
+ * function name. Useful for plugins or frameworks that build schemas
6
+ * dynamically from a config object.
7
+ */
8
+ import type { JsonLdTypes, SchemaGeneratorOptions } from "../../types/schema.js";
9
+ import type { ArticleSchemaInput } from "./types.js";
10
+ import type { FAQItemInput } from "./types.js";
11
+ import type { LocalBusinessSchemaInput } from "./types.js";
12
+ import type { OrganizationSchemaInput } from "./types.js";
13
+ import type { PersonSchemaInput } from "./types.js";
14
+ import type { ProductSchemaInput } from "./types.js";
15
+ import type { VideoSchemaInput } from "./types.js";
16
+ import type { WebSiteSchemaInput } from "./types.js";
17
+ import type { CourseSchemaInput } from "./types.js";
18
+ export type SchemaKind = "Article" | "NewsArticle" | "BlogPosting" | "Product" | "Organization" | "LocalBusiness" | "BreadcrumbList" | "FAQPage" | "WebSite" | "Person" | "VideoObject" | "Course";
19
+ type GeneratorInput<K extends SchemaKind> = K extends "Article" | "NewsArticle" | "BlogPosting" ? ArticleSchemaInput : K extends "Product" ? ProductSchemaInput : K extends "Organization" ? OrganizationSchemaInput : K extends "LocalBusiness" ? LocalBusinessSchemaInput : K extends "FAQPage" ? FAQItemInput[] : K extends "WebSite" ? WebSiteSchemaInput : K extends "Person" ? PersonSchemaInput : K extends "VideoObject" ? VideoSchemaInput : K extends "BreadcrumbList" ? Array<{
20
+ name: string;
21
+ item: string;
22
+ position?: number;
23
+ }> : K extends "Course" ? CourseSchemaInput : never;
24
+ export declare const schemaFactory: {
25
+ generate<K extends SchemaKind>(kind: K, input: GeneratorInput<K>, options?: SchemaGeneratorOptions): JsonLdTypes;
26
+ };
27
+ export {};
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Schema generator factory.
3
+ *
4
+ * Lets callers dispatch on `@type` instead of remembering each generator
5
+ * function name. Useful for plugins or frameworks that build schemas
6
+ * dynamically from a config object.
7
+ */
8
+ import { generateArticleSchema } from "./generators/article.js";
9
+ import { generateBreadcrumbSchema } from "./generators/breadcrumb.js";
10
+ import { generateFAQSchema } from "./generators/faq.js";
11
+ import { generateLocalBusinessSchema } from "./generators/local-business.js";
12
+ import { generateOrganizationSchema } from "./generators/organization.js";
13
+ import { generatePersonSchema } from "./generators/person.js";
14
+ import { generateProductSchema } from "./generators/product.js";
15
+ import { generateVideoSchema } from "./generators/video.js";
16
+ import { generateWebSiteSchema } from "./generators/website.js";
17
+ import { generateCourseSchema } from "./generators/course.js";
18
+ export const schemaFactory = {
19
+ generate(kind, input, options) {
20
+ switch (kind) {
21
+ case "Article":
22
+ case "NewsArticle":
23
+ case "BlogPosting":
24
+ return generateArticleSchema(input, options);
25
+ case "Product":
26
+ return generateProductSchema(input, options);
27
+ case "Course":
28
+ return generateCourseSchema(input, options);
29
+ case "Organization":
30
+ return generateOrganizationSchema(input, options);
31
+ case "LocalBusiness":
32
+ return generateLocalBusinessSchema(input, options);
33
+ case "BreadcrumbList":
34
+ return generateBreadcrumbSchema(input, options);
35
+ case "FAQPage":
36
+ return generateFAQSchema(input, options);
37
+ case "WebSite":
38
+ return generateWebSiteSchema(input, options);
39
+ case "Person":
40
+ return generatePersonSchema(input, options);
41
+ case "VideoObject":
42
+ return generateVideoSchema(input, options);
43
+ default: {
44
+ const _exhaustive = kind;
45
+ throw new Error(`[SEO] Unknown schema kind: ${String(_exhaustive)}`);
46
+ }
47
+ }
48
+ },
49
+ };
@@ -0,0 +1,4 @@
1
+ import type { ArticleJsonLd, SchemaGeneratorOptions } from "../../../types/schema.js";
2
+ import type { ArticleSchemaInput } from "../types.js";
3
+ export declare const generateArticleSchema: (input: ArticleSchemaInput, options?: SchemaGeneratorOptions) => ArticleJsonLd;
4
+ export type { ArticleSchemaInput, SchemaGeneratorOptions };
@@ -0,0 +1,39 @@
1
+ import { createBuilder, withContext } from "../base.js";
2
+ export const generateArticleSchema = createBuilder((input, ctx) => {
3
+ ctx.required(input.headline, "headline");
4
+ ctx.required(input.image, "image");
5
+ ctx.required(input.datePublished, "datePublished");
6
+ ctx.required(input.authorName, "authorName");
7
+ ctx.required(input.publisherName, "publisherName");
8
+ ctx.required(input.publisherLogo, "publisherLogo");
9
+ ctx.set(withContext(input.type ?? "Article"));
10
+ ctx.set({
11
+ headline: input.headline,
12
+ name: input.headline, // required by base Thing schema
13
+ description: input.description,
14
+ image: input.image,
15
+ datePublished: input.datePublished,
16
+ dateModified: input.dateModified,
17
+ author: {
18
+ "@type": input.authorType ?? "Person",
19
+ name: input.authorName,
20
+ url: input.authorUrl,
21
+ },
22
+ publisher: {
23
+ "@context": "https://schema.org",
24
+ "@type": "Organization",
25
+ name: input.publisherName,
26
+ logo: input.publisherLogo,
27
+ url: input.publisherUrl,
28
+ },
29
+ url: input.url,
30
+ articleSection: input.articleSection,
31
+ wordCount: input.wordCount,
32
+ keywords: Array.isArray(input.keywords) ? input.keywords.join(", ") : input.keywords,
33
+ inLanguage: input.inLanguage,
34
+ mainEntityOfPage: {
35
+ "@type": "WebPage",
36
+ "@id": input.url,
37
+ },
38
+ });
39
+ });
@@ -0,0 +1,6 @@
1
+ import type { BreadcrumbJsonLd, SchemaGeneratorOptions } from "../../../types/schema.js";
2
+ export declare const generateBreadcrumbSchema: (items: Array<{
3
+ name: string;
4
+ item: string;
5
+ position?: number;
6
+ }>, options?: SchemaGeneratorOptions) => BreadcrumbJsonLd;
@@ -0,0 +1,18 @@
1
+ import { createBuilder, withContext } from "../base.js";
2
+ export const generateBreadcrumbSchema = (items, options) => createBuilder((input, ctx) => {
3
+ if (ctx.options.validate && input.length === 0) {
4
+ const message = "[SEO] Breadcrumb schema requires at least one item";
5
+ if (ctx.options.strict)
6
+ throw new Error(message);
7
+ console.warn(message);
8
+ }
9
+ ctx.set(withContext("BreadcrumbList"));
10
+ ctx.set({
11
+ itemListElement: input.map((item, index) => ({
12
+ "@type": "ListItem",
13
+ position: item.position ?? index + 1,
14
+ name: item.name,
15
+ item: item.item,
16
+ })),
17
+ });
18
+ })(items, options);
@@ -0,0 +1,3 @@
1
+ import type { CourseJsonLd } from "../../../types/schema.js";
2
+ import type { CourseSchemaInput } from "../types.js";
3
+ export declare const generateCourseSchema: (input: CourseSchemaInput, options?: import("../../../types/schema.js").SchemaGeneratorOptions) => CourseJsonLd;
@@ -0,0 +1,70 @@
1
+ import { createBuilder, withContext } from "../base.js";
2
+ export const generateCourseSchema = createBuilder((input, ctx) => {
3
+ ctx.required(input.name, "name");
4
+ ctx.required(input.description, "description");
5
+ ctx.required(input.providerName, "providerName");
6
+ ctx.set(withContext("Course"));
7
+ ctx.set({
8
+ name: input.name,
9
+ description: input.description,
10
+ provider: {
11
+ "@type": "Organization",
12
+ name: input.providerName,
13
+ url: input.providerUrl,
14
+ },
15
+ });
16
+ if (input.image)
17
+ ctx.set({ image: input.image });
18
+ if (input.url)
19
+ ctx.set({ url: input.url });
20
+ if (input.courseCode)
21
+ ctx.set({ courseCode: input.courseCode });
22
+ if (input.educationalLevel)
23
+ ctx.set({ educationalLevel: input.educationalLevel });
24
+ if (input.inLanguage)
25
+ ctx.set({ inLanguage: input.inLanguage });
26
+ if (input.teaches)
27
+ ctx.set({ teaches: input.teaches });
28
+ if (input.price !== undefined && input.priceCurrency) {
29
+ ctx.set({
30
+ offers: {
31
+ "@type": "Offer",
32
+ price: input.price.toString(),
33
+ priceCurrency: input.priceCurrency,
34
+ availability: "https://schema.org/InStock",
35
+ url: input.url,
36
+ },
37
+ });
38
+ }
39
+ if (input.aggregateRating) {
40
+ ctx.set({
41
+ aggregateRating: {
42
+ "@type": "AggregateRating",
43
+ ratingValue: input.aggregateRating.ratingValue,
44
+ reviewCount: input.aggregateRating.reviewCount,
45
+ bestRating: 5,
46
+ worstRating: 1,
47
+ },
48
+ });
49
+ }
50
+ if (input.instances && input.instances.length > 0) {
51
+ const instances = input.instances.map((inst) => ({
52
+ "@type": "CourseInstance",
53
+ courseMode: inst.courseMode ?? "online",
54
+ courseWorkload: inst.courseWorkload,
55
+ inLanguage: inst.inLanguage ?? input.inLanguage,
56
+ startDate: inst.startDate,
57
+ endDate: inst.endDate,
58
+ ...(inst.instructorName
59
+ ? {
60
+ instructor: {
61
+ "@type": "Person",
62
+ name: inst.instructorName,
63
+ url: inst.instructorUrl,
64
+ },
65
+ }
66
+ : {}),
67
+ }));
68
+ ctx.set({ hasCourseInstance: instances });
69
+ }
70
+ });
@@ -0,0 +1,3 @@
1
+ import type { FAQJsonLd, SchemaGeneratorOptions } from "../../../types/schema.js";
2
+ import type { FAQItemInput } from "../types.js";
3
+ export declare const generateFAQSchema: (faqs: FAQItemInput[], options?: SchemaGeneratorOptions) => FAQJsonLd;
@@ -0,0 +1,21 @@
1
+ import { createBuilder, withContext } from "../base.js";
2
+ export const generateFAQSchema = (faqs, options) => createBuilder((input, ctx) => {
3
+ ctx.required(input, "faqs");
4
+ if (ctx.options.validate && input.length === 0) {
5
+ const message = "[SEO] FAQ schema requires at least one FAQ item";
6
+ if (ctx.options.strict)
7
+ throw new Error(message);
8
+ console.warn(message);
9
+ }
10
+ ctx.set(withContext("FAQPage"));
11
+ ctx.set({
12
+ mainEntity: input.map((faq) => ({
13
+ "@type": "Question",
14
+ name: faq.question,
15
+ acceptedAnswer: {
16
+ "@type": "Answer",
17
+ text: faq.answer,
18
+ },
19
+ })),
20
+ });
21
+ })(faqs, options);
@@ -0,0 +1,3 @@
1
+ import type { LocalBusinessJsonLd } from "../../../types/schema.js";
2
+ import type { LocalBusinessSchemaInput } from "../types.js";
3
+ export declare const generateLocalBusinessSchema: (input: LocalBusinessSchemaInput, options?: import("../../../types/schema.js").SchemaGeneratorOptions) => LocalBusinessJsonLd;
@@ -0,0 +1,45 @@
1
+ import { createBuilder, withContext } from "../base.js";
2
+ export const generateLocalBusinessSchema = createBuilder((input, ctx) => {
3
+ ctx.required(input.name, "name");
4
+ ctx.required(input.address, "address");
5
+ ctx.set(withContext(input.type ?? "LocalBusiness"));
6
+ ctx.set({
7
+ name: input.name,
8
+ description: input.description,
9
+ url: input.url,
10
+ logo: input.logo,
11
+ address: { "@type": "PostalAddress", ...input.address },
12
+ priceRange: input.priceRange,
13
+ });
14
+ if (input.geo) {
15
+ ctx.set({
16
+ geo: {
17
+ "@type": "GeoCoordinates",
18
+ latitude: input.geo.latitude,
19
+ longitude: input.geo.longitude,
20
+ },
21
+ });
22
+ }
23
+ if (input.openingHours) {
24
+ ctx.set({
25
+ openingHoursSpecification: input.openingHours.map((oh) => ({
26
+ "@type": "OpeningHoursSpecification",
27
+ dayOfWeek: oh.dayOfWeek,
28
+ opens: oh.opens,
29
+ closes: oh.closes,
30
+ })),
31
+ });
32
+ }
33
+ if (input.contactPoint) {
34
+ ctx.set({
35
+ contactPoint: input.contactPoint.map((cp) => ({
36
+ "@type": "ContactPoint",
37
+ telephone: cp.telephone,
38
+ contactType: cp.contactType,
39
+ email: cp.email,
40
+ })),
41
+ });
42
+ }
43
+ if (input.sameAs)
44
+ ctx.set({ sameAs: input.sameAs });
45
+ });
@@ -0,0 +1,3 @@
1
+ import type { OrganizationJsonLd } from "../../../types/schema.js";
2
+ import type { OrganizationSchemaInput } from "../types.js";
3
+ export declare const generateOrganizationSchema: (input: OrganizationSchemaInput, options?: import("../../../types/schema.js").SchemaGeneratorOptions) => OrganizationJsonLd;