@easy-cms/plugin-seo 0.17.0 → 0.18.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 (3) hide show
  1. package/dist/index.d.ts +176 -33
  2. package/dist/index.js +543 -145
  3. package/package.json +7 -4
package/dist/index.d.ts CHANGED
@@ -1,6 +1,132 @@
1
- import { EasyCMS, AuthUser, Field, Label, Plugin } from '@easy-cms/core';
1
+ import { EasyCMS, AuthUser, Field, Label as Label$1, Plugin } from '@easy-cms/core';
2
+
3
+ interface IndexNowOptions {
4
+ /**
5
+ * Your IndexNow key: 8–128 letters, digits or dashes, e.g. a UUID. The site must serve it at
6
+ * `/<key>.txt`; the standalone server does, and Nuxt or Next.js apps use `indexNowKeyFile()`.
7
+ */
8
+ readonly key: string;
9
+ /** Where to send changed URLs. Default `https://api.indexnow.org/indexnow` (Bing, Yandex…). */
10
+ readonly endpoint?: string;
11
+ /** How long to gather changes before sending them together, in ms. Default 5000. */
12
+ readonly delay?: number;
13
+ /** For tests: the `fetch` to send with. */
14
+ readonly fetch?: typeof fetch;
15
+ }
16
+ /**
17
+ * The key file for a request's path: the key when the path is `/<key>.txt`, otherwise
18
+ * `undefined`. Serve it as plain text from your app's root.
19
+ */
20
+ declare function indexNowKeyFile(cms: {
21
+ readonly config: {
22
+ readonly endpoints: readonly {
23
+ readonly handler: unknown;
24
+ }[];
25
+ };
26
+ }, pathname: string): string | undefined;
2
27
 
3
28
  type Doc$1 = Record<string, unknown>;
29
+ interface LlmsOptions {
30
+ /** The heading of `llms.txt`. Default: the site name from a global with SEO fields. */
31
+ readonly title?: string;
32
+ /** One or two sentences about the site, shown as the summary. */
33
+ readonly description?: string;
34
+ /** Newest pages per collection in `llms.txt`. Default 100. */
35
+ readonly limit?: number;
36
+ /** The content locale of `llms.txt` and `llms-full.txt`. Default: the default locale. */
37
+ readonly locale?: string;
38
+ /**
39
+ * The Markdown version of a page, for `llms.txt` links (e.g. `/posts/hello.md`). Default:
40
+ * the page's address from `generateURL`.
41
+ */
42
+ readonly markdownURL?: (args: {
43
+ doc: Doc$1;
44
+ locale: string | null;
45
+ collection?: string;
46
+ global?: string;
47
+ }) => string | null | undefined;
48
+ }
49
+ type Label = string | Readonly<Record<string, string>>;
50
+ interface FieldLike {
51
+ readonly name: string;
52
+ readonly type: string;
53
+ readonly fields?: readonly FieldLike[];
54
+ readonly blocks?: readonly {
55
+ readonly slug: string;
56
+ readonly fields: readonly FieldLike[];
57
+ }[];
58
+ }
59
+ /** The part of the Local API the helpers use; a structural type, so core isn't imported. */
60
+ interface SeoCMS {
61
+ readonly config: {
62
+ readonly admin: {
63
+ readonly siteUrl?: string;
64
+ };
65
+ readonly serverURL?: string;
66
+ readonly localization: {
67
+ readonly locales: readonly string[];
68
+ readonly defaultLocale: string;
69
+ } | null;
70
+ readonly endpoints: readonly {
71
+ readonly handler: unknown;
72
+ }[];
73
+ readonly collections?: readonly {
74
+ readonly slug: string;
75
+ readonly useAsTitle?: string;
76
+ readonly labels?: {
77
+ readonly singular?: Label;
78
+ readonly plural?: Label;
79
+ };
80
+ readonly fields: readonly FieldLike[];
81
+ }[];
82
+ readonly globals?: readonly {
83
+ readonly slug: string;
84
+ readonly label?: Label;
85
+ readonly fields: readonly FieldLike[];
86
+ }[];
87
+ };
88
+ find(collection: string, options: Record<string, unknown>): Promise<{
89
+ docs: Doc$1[];
90
+ hasNextPage: boolean;
91
+ }>;
92
+ findGlobal(global: string, options: Record<string, unknown>): Promise<Doc$1>;
93
+ }
94
+
95
+ interface LlmsTxtOptions {
96
+ /** The site's public address, to make links absolute. Default `admin.siteUrl`, then `serverURL`. */
97
+ readonly siteUrl?: string;
98
+ }
99
+ interface LlmsFullTxtOptions extends LlmsTxtOptions {
100
+ /** Stop adding pages after this many bytes. Default about 5 MB. */
101
+ readonly maxBytes?: number;
102
+ }
103
+ interface DocMarkdownOptions {
104
+ readonly collection?: string;
105
+ readonly global?: string;
106
+ /** The document, as read for visitors (fetch with `depth` 1 for image URLs). */
107
+ readonly doc: Doc$1;
108
+ /** The page's address, shown under the title. */
109
+ readonly url?: string;
110
+ }
111
+ /**
112
+ * `llms.txt` (llmstxt.org): a short Markdown index of the site for AI assistants: its title and
113
+ * summary, then the newest pages of each collection with their descriptions. Lists the same
114
+ * pages as the sitemap: published, visible to visitors, not marked "noindex".
115
+ */
116
+ declare function llmsTxt(cms: SeoCMS, options?: LlmsTxtOptions): Promise<string>;
117
+ /**
118
+ * `llms-full.txt`: the Markdown of every page `llms.txt` would list (without its limit), newest
119
+ * first, in one file for assistants that read a whole site at once. Stops at `maxBytes`.
120
+ */
121
+ declare function llmsFullTxt(cms: SeoCMS, options?: LlmsFullTxtOptions): Promise<string>;
122
+ /**
123
+ * The Markdown version of one page, e.g. for `/posts/hello.md`: its title, description and
124
+ * dates, then its rich text, long text and the text in its blocks, in the order of the fields.
125
+ * The plugin's `markdown` option replaces this for a collection or global.
126
+ */
127
+ declare function docMarkdown(cms: SeoCMS, options: DocMarkdownOptions): string;
128
+
129
+ type Doc = Record<string, unknown>;
4
130
  type Value = string | null | undefined;
5
131
  interface SeoMetaOptions {
6
132
  /**
@@ -14,7 +140,7 @@ interface SeoMetaOptions {
14
140
  * The page's canonical address: absolute, or a path on the site. As a function it gets the
15
141
  * locale too, which adds `hreflang` links to the page in every locale.
16
142
  */
17
- readonly url?: string | ((doc: Doc$1, locale?: string) => Value);
143
+ readonly url?: string | ((doc: Doc, locale?: string) => Value);
18
144
  /** The page's content locale, e.g. `en`. Shown as `og:locale`. */
19
145
  readonly locale?: string;
20
146
  /** All content locales, for `hreflang`. Default: `config.localization.locales`. */
@@ -22,17 +148,17 @@ interface SeoMetaOptions {
22
148
  /** The locale for `x-default`. Default: `config.localization.defaultLocale`, then the first. */
23
149
  readonly defaultLocale?: string;
24
150
  /** The title when `meta.title` is empty. Default: the document's `title`. */
25
- readonly title?: (doc: Doc$1) => Value;
151
+ readonly title?: (doc: Doc) => Value;
26
152
  /** The description when `meta.description` is empty. Default: `excerpt`, then `description`. */
27
- readonly description?: (doc: Doc$1) => Value;
153
+ readonly description?: (doc: Doc) => Value;
28
154
  /** Shown as `og:site_name`. */
29
155
  readonly siteName?: string;
30
156
  /** `article` for posts: adds the published and modified times, and BlogPosting JSON-LD. */
31
157
  readonly type?: 'website' | 'article';
32
158
  /** When the article was published. Default: `publishedAt`, then `createdAt`. */
33
- readonly publishedTime?: (doc: Doc$1) => Value;
159
+ readonly publishedTime?: (doc: Doc) => Value;
34
160
  /** The article's author, e.g. `(post) => post.author?.name`. */
35
- readonly author?: string | ((doc: Doc$1) => Value);
161
+ readonly author?: string | ((doc: Doc) => Value);
36
162
  /** Schema.org type of an article's JSON-LD. Default `BlogPosting`. */
37
163
  readonly articleType?: 'Article' | 'BlogPosting' | 'NewsArticle';
38
164
  }
@@ -138,7 +264,7 @@ interface SeoMeta {
138
264
  * return meta.next // Next.js generateMetadata()
139
265
  * ```
140
266
  */
141
- declare function seoMeta(doc: Doc$1, options?: SeoMetaOptions): SeoMeta;
267
+ declare function seoMeta(doc: Doc, options?: SeoMetaOptions): SeoMeta;
142
268
  interface SiteJsonLdOptions {
143
269
  /** The organization or site name. */
144
270
  readonly name: string;
@@ -173,7 +299,36 @@ interface RobotsTxtOptions {
173
299
  readonly disallowAll?: boolean;
174
300
  /** More paths to keep out of search, e.g. `['/search']`. */
175
301
  readonly disallow?: readonly string[];
302
+ /**
303
+ * AI crawlers by what they do (all allowed by default): `training` collects text to train
304
+ * models, `search` indexes pages for AI search and answers that cite them, `user` opens a
305
+ * page when someone asks an assistant to. E.g. `{ training: false }` keeps your content out
306
+ * of training but lets AI answers link to you.
307
+ */
308
+ readonly ai?: {
309
+ readonly training?: boolean;
310
+ readonly search?: boolean;
311
+ readonly user?: boolean;
312
+ };
313
+ /** Rules for other crawlers. The admin and API rules are added to each one. */
314
+ readonly rules?: readonly RobotsRule[];
315
+ }
316
+ interface RobotsRule {
317
+ /** One crawler's user agent, or several. */
318
+ readonly userAgent: string | readonly string[];
319
+ readonly allow?: readonly string[];
320
+ /** Paths to keep it out of; `['/']` for the whole site. */
321
+ readonly disallow?: readonly string[];
176
322
  }
323
+ /**
324
+ * Known AI crawlers, by what they do, from each company's documentation. Updated in each
325
+ * release; add others with `rules`.
326
+ */
327
+ declare const AI_CRAWLERS: {
328
+ readonly training: readonly ["GPTBot", "ClaudeBot", "Google-Extended", "Applebot-Extended", "CCBot", "Meta-ExternalAgent", "Bytespider", "cohere-training-data-crawler"];
329
+ readonly search: readonly ["OAI-SearchBot", "Claude-SearchBot", "PerplexityBot"];
330
+ readonly user: readonly ["ChatGPT-User", "Claude-User", "Perplexity-User"];
331
+ };
177
332
  /**
178
333
  * A `robots.txt` that keeps search engines out of the admin and the API (except uploaded
179
334
  * files, so share images still work) and points them to the sitemap.
@@ -228,12 +383,22 @@ interface SeoPluginOptions {
228
383
  /** One value per content locale. Default: true when the config has `localization`. */
229
384
  readonly localized?: boolean;
230
385
  /** Label of the group. Default "SEO". */
231
- readonly label?: Label;
386
+ readonly label?: Label$1;
232
387
  /**
233
388
  * `robots.txt` for the standalone server (`easy-cms serve`), or `false` for none. Nuxt and
234
389
  * Next.js apps serve their own with `robotsTxt()`.
235
390
  */
236
391
  readonly robots?: Omit<RobotsTxtOptions, 'config'> | false;
392
+ /**
393
+ * `llms.txt` and `llms-full.txt` for AI assistants: the title, summary, links to Markdown
394
+ * versions and how many pages. The standalone server serves them; Nuxt and Next.js apps use
395
+ * `llmsTxt()` and `llmsFullTxt()`. `false` turns the standalone routes off.
396
+ */
397
+ readonly llms?: LlmsOptions | false;
398
+ /** Markdown of a page in a collection or global, by slug, instead of the automatic one. */
399
+ readonly markdown?: Readonly<Record<string, (doc: Record<string, unknown>) => string>>;
400
+ /** Tell Bing and other IndexNow search engines about changed pages as they are published. */
401
+ readonly indexNow?: IndexNowOptions;
237
402
  }
238
403
  /**
239
404
  * Adds a `meta` group (title, description, image) to the chosen collections and globals,
@@ -252,28 +417,6 @@ declare const DEFAULT_DESCRIPTION_LENGTH: {
252
417
  readonly max: 150;
253
418
  };
254
419
 
255
- type Doc = Record<string, unknown>;
256
- /** The part of the Local API `sitemap()` uses; a structural type, so core isn't imported. */
257
- interface SitemapCMS {
258
- readonly config: {
259
- readonly admin: {
260
- readonly siteUrl?: string;
261
- };
262
- readonly serverURL?: string;
263
- readonly localization: {
264
- readonly locales: readonly string[];
265
- readonly defaultLocale: string;
266
- } | null;
267
- readonly endpoints: readonly {
268
- readonly handler: unknown;
269
- }[];
270
- };
271
- find(collection: string, options: Record<string, unknown>): Promise<{
272
- docs: Doc[];
273
- hasNextPage: boolean;
274
- }>;
275
- findGlobal(global: string, options: Record<string, unknown>): Promise<Doc>;
276
- }
277
420
  /** One page in the sitemap, in the shape of Next.js `MetadataRoute.Sitemap`. */
278
421
  interface SitemapEntry {
279
422
  readonly url: string;
@@ -304,11 +447,11 @@ declare const SITEMAP_LIMIT = 50000;
304
447
  * published ones, as the read access allows), with a URL from `generateURL` and not hidden with
305
448
  * "noindex". Returns the shape Next.js `app/sitemap.ts` expects.
306
449
  */
307
- declare function sitemap(cms: SitemapCMS, options?: SitemapOptions): Promise<SitemapEntry[]>;
450
+ declare function sitemap(cms: SeoCMS, options?: SitemapOptions): Promise<SitemapEntry[]>;
308
451
  /**
309
452
  * The sitemap as XML. Over 50,000 URLs, it is an index of pages `<base>?page=1`, `?page=2`…,
310
453
  * and `page` renders one of them.
311
454
  */
312
- declare function sitemapXml(cms: SitemapCMS, options?: SitemapXmlOptions): Promise<string>;
455
+ declare function sitemapXml(cms: SeoCMS, options?: SitemapXmlOptions): Promise<string>;
313
456
 
314
- export { DEFAULT_DESCRIPTION_LENGTH, DEFAULT_TITLE_LENGTH, type Generate, type GenerateArgs, type JsonLd, type NextSeoMetadata, type NuxtSeoHead, type NuxtSeoMeta, type RobotsTxtOptions, SITEMAP_LIMIT, type SeoImage, type SeoMeta, type SeoMetaOptions, type SeoPluginOptions, type SiteJsonLdOptions, type SitemapEntry, type SitemapOptions, type SitemapXmlOptions, jsonLdScript, robotsTxt, seoMeta, seoPlugin, siteJsonLd, sitemap, sitemapXml };
457
+ export { AI_CRAWLERS, DEFAULT_DESCRIPTION_LENGTH, DEFAULT_TITLE_LENGTH, type DocMarkdownOptions, type Generate, type GenerateArgs, type IndexNowOptions, type JsonLd, type LlmsFullTxtOptions, type LlmsOptions, type LlmsTxtOptions, type NextSeoMetadata, type NuxtSeoHead, type NuxtSeoMeta, type RobotsRule, type RobotsTxtOptions, SITEMAP_LIMIT, type SeoImage, type SeoMeta, type SeoMetaOptions, type SeoPluginOptions, type SiteJsonLdOptions, type SitemapEntry, type SitemapOptions, type SitemapXmlOptions, docMarkdown, indexNowKeyFile, jsonLdScript, llmsFullTxt, llmsTxt, robotsTxt, seoMeta, seoPlugin, siteJsonLd, sitemap, sitemapXml };
package/dist/index.js CHANGED
@@ -1,3 +1,82 @@
1
+ // src/indexnow.ts
2
+ var INDEXNOW_KEY = /^[A-Za-z0-9-]{8,128}$/;
3
+ var DEFAULT_ENDPOINT = "https://api.indexnow.org/indexnow";
4
+ var MAX_URLS = 1e4;
5
+ function isPublicUrl(url) {
6
+ let parsed;
7
+ try {
8
+ parsed = new URL(url);
9
+ } catch {
10
+ return false;
11
+ }
12
+ const host = parsed.hostname;
13
+ return parsed.protocol === "https:" && host.includes(".") && !/^(\d+\.){3}\d+$/.test(host) && !/(^|\.)(localhost|local|test|example|invalid|internal)$/.test(host);
14
+ }
15
+ function createIndexNow(options) {
16
+ const pending = /* @__PURE__ */ new Set();
17
+ let timer;
18
+ let logger = console;
19
+ let sending = Promise.resolve();
20
+ async function send(urls) {
21
+ const byHost = /* @__PURE__ */ new Map();
22
+ for (const url of urls) {
23
+ const host = new URL(url).host;
24
+ byHost.set(host, [...byHost.get(host) ?? [], url]);
25
+ }
26
+ const post = options.fetch ?? fetch;
27
+ for (const [host, list] of byHost) {
28
+ for (let i = 0; i < list.length; i += MAX_URLS) {
29
+ try {
30
+ const response = await post(options.endpoint ?? DEFAULT_ENDPOINT, {
31
+ method: "POST",
32
+ headers: { "content-type": "application/json; charset=utf-8" },
33
+ body: JSON.stringify({
34
+ host,
35
+ key: options.key,
36
+ keyLocation: `https://${host}/${options.key}.txt`,
37
+ urlList: list.slice(i, i + MAX_URLS)
38
+ }),
39
+ signal: AbortSignal.timeout(1e4)
40
+ });
41
+ if (!response.ok) logger.warn(`IndexNow: ${host} answered ${response.status}`);
42
+ } catch (error2) {
43
+ logger.warn(`IndexNow: could not send ${host}: ${error2.message}`);
44
+ }
45
+ }
46
+ }
47
+ }
48
+ function flush() {
49
+ if (timer) clearTimeout(timer);
50
+ timer = void 0;
51
+ const urls = [...pending];
52
+ pending.clear();
53
+ if (urls.length > 0) sending = sending.then(() => send(urls));
54
+ return sending;
55
+ }
56
+ return {
57
+ /** Queues URLs of a page that changed for visitors; sends them after `delay`. */
58
+ submit(urls, log) {
59
+ if (log) logger = log;
60
+ for (const url of urls) if (isPublicUrl(url)) pending.add(url);
61
+ if (pending.size === 0 || timer) return;
62
+ timer = setTimeout(() => void flush(), options.delay ?? 5e3);
63
+ timer.unref?.();
64
+ },
65
+ flush
66
+ };
67
+ }
68
+ function indexNowKeyFile(cms, pathname) {
69
+ for (const endpoint of cms.config.endpoints) {
70
+ const key = endpoint.handler[INDEXNOW_SOURCE];
71
+ if (key && pathname === `/${key}.txt`) return key;
72
+ }
73
+ return void 0;
74
+ }
75
+ var INDEXNOW_SOURCE = /* @__PURE__ */ Symbol.for("easy-cms.plugin-seo.indexnow");
76
+
77
+ // src/llms.ts
78
+ import { renderMarkdown } from "@easy-cms/richtext";
79
+
1
80
  // src/shared.ts
2
81
  var DEFAULT_TITLE_LENGTH = { min: 50, max: 60 };
3
82
  var DEFAULT_DESCRIPTION_LENGTH = { min: 100, max: 150 };
@@ -5,169 +84,273 @@ var META_FIELD = "meta";
5
84
  var GENERATE_PATH = "/seo/generate";
6
85
  var GENERATE_KINDS = ["title", "description", "image", "url"];
7
86
 
8
- // src/sitemap.ts
9
- var SITEMAP_SOURCE = /* @__PURE__ */ Symbol.for("easy-cms.plugin-seo.sitemap");
10
- var SITEMAP_LIMIT = 5e4;
87
+ // src/source.ts
88
+ var SEO_SOURCE = /* @__PURE__ */ Symbol.for("easy-cms.plugin-seo.sitemap");
89
+ function findSource(cms, helper) {
90
+ for (const endpoint of cms.config.endpoints) {
91
+ const source = endpoint.handler[SEO_SOURCE];
92
+ if (source) return source;
93
+ }
94
+ throw new Error(`${helper}: add seoPlugin() to the plugins in your Easy CMS config`);
95
+ }
96
+ async function visitor(read) {
97
+ try {
98
+ return await read();
99
+ } catch (error2) {
100
+ const status = error2.status;
101
+ if (status === 401 || status === 403 || status === 404) return void 0;
102
+ throw error2;
103
+ }
104
+ }
105
+ function absolute(url, site) {
106
+ if (/^https?:\/\//.test(url) || !site || !/^https?:\/\//.test(site)) return url;
107
+ return `${site.replace(/\/+$/, "")}/${url.replace(/^\/+/, "")}`;
108
+ }
109
+ var siteOf = (cms, siteUrl) => siteUrl ?? (cms.config.admin.siteUrl || cms.config.serverURL);
11
110
  var BATCH = 500;
12
- async function sitemap(cms, options = {}) {
13
- const source = findSource(cms);
14
- const site = options.siteUrl ?? (cms.config.admin.siteUrl || cms.config.serverURL);
15
- const localization = cms.config.localization;
16
- const locales = localization ? [...localization.locales] : [null];
111
+ async function visiblePages(cms, source, options) {
17
112
  const generateURL = source.generateURL;
18
- if (!generateURL) return [];
19
- const pages = /* @__PURE__ */ new Map();
20
- const add = async (key, doc, locale, target) => {
113
+ if (!generateURL) return;
114
+ const { locale, site } = options;
115
+ const pageOf = async (key, doc, target) => {
21
116
  if (doc[META_FIELD]?.noindex === true) return;
22
117
  const id = doc.id ?? null;
23
- const url = await generateURL({
24
- doc,
25
- id,
26
- locale,
27
- cms,
28
- user: null,
29
- ...target
30
- });
118
+ const url = await generateURL({ doc, id, locale, cms, user: null, ...target });
31
119
  if (typeof url !== "string" || url.trim() === "") return;
32
- const page = pages.get(key) ?? { urls: /* @__PURE__ */ new Map() };
33
- page.urls.set(locale, absolute(url.trim(), site));
34
- if (typeof doc.updatedAt === "string") page.lastModified = doc.updatedAt;
35
- pages.set(key, page);
120
+ await options.each({ key, doc, url: absolute(url.trim(), site), ...target });
36
121
  };
37
122
  for (const collection of source.collections) {
38
- for (const locale of locales) {
39
- for (let page = 1; ; page++) {
40
- const result = await visitor(
41
- () => cms.find(collection, {
42
- overrideAccess: false,
43
- user: null,
44
- depth: 0,
45
- sort: "id",
46
- limit: BATCH,
47
- page,
48
- ...locale ? { locale } : {}
49
- })
50
- );
51
- if (!result) break;
52
- for (const doc of result.docs)
53
- await add(`c:${collection}:${doc.id}`, doc, locale, { collection });
54
- if (!result.hasNextPage) break;
55
- }
56
- }
57
- }
58
- for (const global of source.globals) {
59
- for (const locale of locales) {
60
- const doc = await visitor(
61
- () => cms.findGlobal(global, {
123
+ let seen = 0;
124
+ for (let page = 1; ; page++) {
125
+ const limit = options.limit === void 0 ? BATCH : Math.min(BATCH, options.limit - seen);
126
+ if (limit <= 0) break;
127
+ const result = await visitor(
128
+ () => cms.find(collection, {
62
129
  overrideAccess: false,
63
130
  user: null,
64
- depth: 0,
131
+ depth: options.depth ?? 0,
132
+ sort: options.sort ?? "id",
133
+ limit,
134
+ page: options.limit === void 0 ? page : 1,
65
135
  ...locale ? { locale } : {}
66
136
  })
67
137
  );
68
- if (doc && doc.status !== "draft") await add(`g:${global}`, doc, locale, { global });
138
+ if (!result) break;
139
+ for (const doc of result.docs) await pageOf(`c:${collection}:${doc.id}`, doc, { collection });
140
+ seen += result.docs.length;
141
+ if (!result.hasNextPage || options.limit !== void 0) break;
69
142
  }
70
143
  }
71
- const entries = [];
72
- for (const { urls, lastModified } of pages.values()) {
73
- const distinct = new Set(urls.values());
74
- const languages = {};
75
- if (localization && distinct.size > 1) {
76
- for (const [locale, url] of urls) languages[locale] = url;
77
- const fallback = urls.get(localization.defaultLocale);
78
- if (fallback) languages["x-default"] = fallback;
79
- }
80
- for (const url of distinct) {
81
- if (!/^https?:\/\//.test(url))
82
- throw new Error(
83
- `sitemap: "${url}" is not an absolute URL. Set admin.siteUrl in the config, or pass siteUrl.`
84
- );
85
- entries.push({
86
- url,
87
- ...lastModified ? { lastModified } : {},
88
- ...Object.keys(languages).length > 0 ? { alternates: { languages } } : {}
89
- });
90
- }
144
+ for (const global of source.globals) {
145
+ const doc = await visitor(
146
+ () => cms.findGlobal(global, {
147
+ overrideAccess: false,
148
+ user: null,
149
+ depth: options.depth ?? 0,
150
+ ...locale ? { locale } : {}
151
+ })
152
+ );
153
+ if (doc && doc.status !== "draft") await pageOf(`g:${global}`, doc, { global });
91
154
  }
92
- return entries;
93
155
  }
94
- async function sitemapXml(cms, options = {}) {
95
- const entries = await sitemap(cms, options);
96
- const page = Number(options.page ?? 0);
97
- if (entries.length > SITEMAP_LIMIT && !(page >= 1)) {
98
- const site = options.siteUrl ?? (cms.config.admin.siteUrl || cms.config.serverURL) ?? "";
99
- const base = options.base ?? `${site.replace(/\/+$/, "")}/sitemap.xml`;
100
- const pages = Math.ceil(entries.length / SITEMAP_LIMIT);
101
- const items = Array.from(
102
- { length: pages },
103
- (_, i) => ` <sitemap><loc>${xml(`${base}?page=${i + 1}`)}</loc></sitemap>`
104
- );
105
- return `<?xml version="1.0" encoding="UTF-8"?>
106
- <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
107
- ${items.join("\n")}
108
- </sitemapindex>
156
+ function labelText(label, locale, fallback) {
157
+ if (!label) return fallback;
158
+ if (typeof label === "string") return label;
159
+ return locale && label[locale] || label.en || Object.values(label)[0] || fallback;
160
+ }
161
+
162
+ // src/llms.ts
163
+ var DEFAULT_LIMIT = 100;
164
+ var DEFAULT_MAX_BYTES = 5e6;
165
+ async function llmsTxt(cms, options = {}) {
166
+ const source = findSource(cms, "llmsTxt");
167
+ const locale = localeOf(cms, source);
168
+ const site = siteOf(cms, options.siteUrl);
169
+ const sections = /* @__PURE__ */ new Map();
170
+ await visiblePages(cms, source, {
171
+ locale,
172
+ site,
173
+ sort: "-createdAt",
174
+ limit: source.llms.limit ?? DEFAULT_LIMIT,
175
+ each: (page) => {
176
+ const heading = page.collection ? labelText(collectionOf(cms, page.collection)?.labels?.plural, locale, page.collection) : "Pages";
177
+ const title = titleOf(cms, page);
178
+ const description = text(page.doc[META_FIELD]?.description);
179
+ const href = linkOf(source, page, locale, site);
180
+ const line = `- [${inline(title)}](${href})${description ? `: ${inline(description)}` : ""}`;
181
+ sections.set(heading, [...sections.get(heading) ?? [], line]);
182
+ }
183
+ });
184
+ const lines = [`# ${inline(await siteTitle(cms, source, locale))}`];
185
+ if (source.llms.description) lines.push("", `> ${inline(source.llms.description)}`);
186
+ for (const [heading, items] of sections) lines.push("", `## ${inline(heading)}`, "", ...items);
187
+ return `${lines.join("\n")}
109
188
  `;
110
- }
111
- const slice = entries.length > SITEMAP_LIMIT ? entries.slice((page - 1) * SITEMAP_LIMIT, page * SITEMAP_LIMIT) : entries;
112
- const localized = slice.some((e) => e.alternates);
113
- const urls = slice.map((entry) => {
114
- const lines = [` <loc>${xml(entry.url)}</loc>`];
115
- if (entry.lastModified) lines.push(` <lastmod>${xml(entry.lastModified)}</lastmod>`);
116
- for (const [lang, href] of Object.entries(entry.alternates?.languages ?? {}))
117
- lines.push(` <xhtml:link rel="alternate" hreflang="${xml(lang)}" href="${xml(href)}"/>`);
118
- return ` <url>
119
- ${lines.join("\n")}
120
- </url>`;
189
+ }
190
+ async function llmsFullTxt(cms, options = {}) {
191
+ const source = findSource(cms, "llmsFullTxt");
192
+ const locale = localeOf(cms, source);
193
+ const site = siteOf(cms, options.siteUrl);
194
+ const max = options.maxBytes ?? DEFAULT_MAX_BYTES;
195
+ const header = [`# ${inline(await siteTitle(cms, source, locale))}`];
196
+ if (source.llms.description) header.push("", `> ${inline(source.llms.description)}`);
197
+ const parts = [];
198
+ let bytes = byteLength(header.join("\n"));
199
+ let full = false;
200
+ await visiblePages(cms, source, {
201
+ locale,
202
+ site,
203
+ sort: "-createdAt",
204
+ depth: 1,
205
+ each: (page) => {
206
+ if (full) return;
207
+ const markdown = pageMarkdown(cms, source, page);
208
+ const size = byteLength(markdown) + 7;
209
+ if (bytes + size > max) {
210
+ full = true;
211
+ return;
212
+ }
213
+ bytes += size;
214
+ parts.push(markdown);
215
+ }
121
216
  });
122
- const namespaces = `xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"${localized ? ' xmlns:xhtml="http://www.w3.org/1999/xhtml"' : ""}`;
123
- return `<?xml version="1.0" encoding="UTF-8"?>
124
- <urlset ${namespaces}>
125
- ${urls.join("\n")}
126
- </urlset>
217
+ const body = [header.join("\n"), ...parts].join("\n\n---\n\n");
218
+ return `${body}${full ? "\n\n---\n\n(More pages are listed in /llms.txt and /sitemap.xml.)" : ""}
127
219
  `;
128
220
  }
129
- function findSource(cms) {
130
- for (const endpoint of cms.config.endpoints) {
131
- const source = endpoint.handler[SITEMAP_SOURCE];
132
- if (source) return source;
221
+ function docMarkdown(cms, options) {
222
+ const source = findSource(cms, "docMarkdown");
223
+ const page = {
224
+ key: "",
225
+ doc: options.doc,
226
+ url: options.url ?? "",
227
+ ...options.collection ? { collection: options.collection } : {},
228
+ ...options.global ? { global: options.global } : {}
229
+ };
230
+ return `${pageMarkdown(cms, source, page)}
231
+ `;
232
+ }
233
+ function pageMarkdown(cms, source, page) {
234
+ const slug = page.collection ?? page.global ?? "";
235
+ const custom = source.markdown[slug];
236
+ if (custom) return custom(page.doc).trim();
237
+ const { doc } = page;
238
+ const meta = doc[META_FIELD] ?? {};
239
+ const title = titleOf(cms, page);
240
+ const lines = [`# ${inline(title)}`];
241
+ const description = text(meta.description) ?? text(doc.excerpt);
242
+ if (description) lines.push("", `> ${inline(description)}`);
243
+ const facts = [
244
+ page.url ? `URL: ${page.url}` : "",
245
+ dateOf(doc.publishedAt ?? doc.createdAt) ? `Published: ${dateOf(doc.publishedAt ?? doc.createdAt)}` : "",
246
+ dateOf(doc.updatedAt) ? `Updated: ${dateOf(doc.updatedAt)}` : ""
247
+ ].filter(Boolean);
248
+ if (facts.length > 0) lines.push("", facts.join(" \n"));
249
+ const titleField = page.collection ? collectionOf(cms, page.collection)?.useAsTitle : void 0;
250
+ const fields = page.collection ? collectionOf(cms, page.collection)?.fields ?? [] : cms.config.globals?.find((g) => g.slug === page.global)?.fields ?? [];
251
+ const skip = /* @__PURE__ */ new Set([META_FIELD, "excerpt", titleField ?? "title"]);
252
+ const body = fieldsMarkdown(
253
+ fields.filter((f) => !skip.has(f.name)),
254
+ doc,
255
+ false
256
+ );
257
+ if (body) lines.push("", body);
258
+ return lines.join("\n");
259
+ }
260
+ function fieldsMarkdown(fields, data, nested) {
261
+ const out = [];
262
+ for (const field of fields) {
263
+ const value = data[field.name];
264
+ if (value === null || value === void 0) continue;
265
+ switch (field.type) {
266
+ case "richText":
267
+ out.push(renderMarkdown(value));
268
+ break;
269
+ case "textarea":
270
+ out.push(plain(String(value)));
271
+ break;
272
+ case "text":
273
+ if (nested) out.push(plain(String(value)));
274
+ break;
275
+ case "group":
276
+ out.push(fieldsMarkdown(field.fields ?? [], value, nested));
277
+ break;
278
+ case "array":
279
+ for (const row of Array.isArray(value) ? value : [])
280
+ out.push(fieldsMarkdown(field.fields ?? [], row, true));
281
+ break;
282
+ case "blocks":
283
+ for (const row of Array.isArray(value) ? value : []) {
284
+ const block = field.blocks?.find((b) => b.slug === row.blockType);
285
+ if (block) out.push(fieldsMarkdown(block.fields, row, true));
286
+ }
287
+ break;
288
+ }
133
289
  }
134
- throw new Error("sitemap: add seoPlugin() to the plugins in your Easy CMS config");
290
+ return out.filter((part) => part.trim() !== "").join("\n\n");
135
291
  }
136
- async function visitor(read) {
137
- try {
138
- return await read();
139
- } catch (error2) {
140
- const status = error2.status;
141
- if (status === 401 || status === 403 || status === 404) return void 0;
142
- throw error2;
292
+ function plain(value) {
293
+ return renderMarkdown({
294
+ type: "doc",
295
+ content: value.split(/\n{2,}/).filter((p) => p.trim() !== "").map((p) => ({ type: "paragraph", content: [{ type: "text", text: p.trim() }] }))
296
+ });
297
+ }
298
+ function inline(value) {
299
+ return plain(value.replace(/\s+/g, " ")) || value;
300
+ }
301
+ var text = (value) => typeof value === "string" && value.trim() !== "" ? value.trim() : void 0;
302
+ var dateOf = (value) => typeof value === "string" && /^\d{4}-\d{2}-\d{2}/.test(value) ? value.slice(0, 10) : void 0;
303
+ var byteLength = (value) => new TextEncoder().encode(value).length;
304
+ var collectionOf = (cms, slug) => cms.config.collections?.find((c) => c.slug === slug);
305
+ function localeOf(cms, source) {
306
+ const localization = cms.config.localization;
307
+ if (!localization) return null;
308
+ return source.llms.locale ?? localization.defaultLocale;
309
+ }
310
+ async function siteTitle(cms, source, locale) {
311
+ if (source.llms.title) return source.llms.title;
312
+ for (const global of source.globals) {
313
+ const doc = await visitor(
314
+ () => cms.findGlobal(global, { overrideAccess: false, user: null, ...locale ? { locale } : {} })
315
+ );
316
+ const name = text(doc?.siteName) ?? text(doc?.name) ?? text(doc?.title);
317
+ if (name) return name;
143
318
  }
319
+ return "Site";
144
320
  }
145
- function absolute(url, site) {
146
- if (/^https?:\/\//.test(url) || !site || !/^https?:\/\//.test(site)) return url;
147
- return `${site.replace(/\/+$/, "")}/${url.replace(/^\/+/, "")}`;
321
+ function titleOf(cms, page) {
322
+ const { doc } = page;
323
+ const field = page.collection ? collectionOf(cms, page.collection)?.useAsTitle : void 0;
324
+ return text(doc[META_FIELD]?.title) ?? text(field ? doc[field] : void 0) ?? text(doc.title) ?? text(doc.siteName) ?? text(doc.name) ?? page.url;
325
+ }
326
+ function linkOf(source, page, locale, site) {
327
+ const markdown = source.llms.markdownURL?.({
328
+ doc: page.doc,
329
+ locale,
330
+ ...page.collection ? { collection: page.collection } : {},
331
+ ...page.global ? { global: page.global } : {}
332
+ });
333
+ const href = text(markdown) ? absolute(text(markdown), site) : page.url;
334
+ return href.replace(/[()\s]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);
148
335
  }
149
- var xml = (value) => value.replace(
150
- /[&<>"']/g,
151
- (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&apos;" })[c]
152
- );
153
336
 
154
337
  // src/meta.ts
155
- var text = (value) => typeof value === "string" && value.trim() !== "" ? value.trim() : void 0;
338
+ var text2 = (value) => typeof value === "string" && value.trim() !== "" ? value.trim() : void 0;
156
339
  var ogLocale = (locale) => locale.replace("-", "_");
157
340
  var defined = (record) => Object.fromEntries(Object.entries(record).filter(([, v]) => v !== void 0));
158
341
  function seoMeta(doc, options = {}) {
159
342
  const config = options.config;
160
343
  const meta = doc[META_FIELD] ?? {};
161
344
  const site = options.siteUrl ?? (config?.admin?.siteUrl || config?.serverURL);
162
- const title = text(meta.title) ?? text((options.title ?? ((d) => d.title))(doc));
163
- const description = text(meta.description) ?? text((options.description ?? ((d) => d.excerpt ?? d.description))(doc));
345
+ const title = text2(meta.title) ?? text2((options.title ?? ((d) => d.title))(doc));
346
+ const description = text2(meta.description) ?? text2((options.description ?? ((d) => d.excerpt ?? d.description))(doc));
164
347
  const noindex = meta.noindex === true;
165
348
  const article = options.type === "article";
166
349
  const locale = options.locale;
167
350
  const locales = options.locales ?? config?.localization?.locales ?? [];
168
351
  const urlFor = (l) => {
169
352
  const url = typeof options.url === "function" ? options.url(doc, l) : options.url;
170
- return text(url) ? absolute(text(url), site) : void 0;
353
+ return text2(url) ? absolute(text2(url), site) : void 0;
171
354
  };
172
355
  const canonical = urlFor(locale);
173
356
  let alternates;
@@ -182,15 +365,15 @@ function seoMeta(doc, options = {}) {
182
365
  if (Object.keys(alternates).length === 0) alternates = void 0;
183
366
  }
184
367
  const media = meta.image;
185
- const image = typeof media === "object" && media !== null && text(media.url) ? {
186
- url: absolute(text(media.url), site),
187
- ...text(media.alt) ? { alt: text(media.alt) } : {},
368
+ const image = typeof media === "object" && media !== null && text2(media.url) ? {
369
+ url: absolute(text2(media.url), site),
370
+ ...text2(media.alt) ? { alt: text2(media.alt) } : {},
188
371
  ...typeof media.width === "number" ? { width: media.width } : {},
189
372
  ...typeof media.height === "number" ? { height: media.height } : {}
190
373
  } : void 0;
191
- const publishedTime = article ? text((options.publishedTime ?? ((d) => d.publishedAt ?? d.createdAt))(doc)) : void 0;
192
- const modifiedTime = article ? text(doc.updatedAt) : void 0;
193
- const author = article ? text(typeof options.author === "function" ? options.author(doc) : options.author) : void 0;
374
+ const publishedTime = article ? text2((options.publishedTime ?? ((d) => d.publishedAt ?? d.createdAt))(doc)) : void 0;
375
+ const modifiedTime = article ? text2(doc.updatedAt) : void 0;
376
+ const author = article ? text2(typeof options.author === "function" ? options.author(doc) : options.author) : void 0;
194
377
  const otherLocales = locale ? locales.filter((l) => l !== locale).map(ogLocale) : [];
195
378
  const card = image ? "summary_large_image" : "summary";
196
379
  const type = article ? "article" : "website";
@@ -310,18 +493,49 @@ function jsonLdScript(value) {
310
493
  }
311
494
 
312
495
  // src/robots.ts
496
+ var AI_CRAWLERS = {
497
+ training: [
498
+ "GPTBot",
499
+ "ClaudeBot",
500
+ "Google-Extended",
501
+ "Applebot-Extended",
502
+ "CCBot",
503
+ "Meta-ExternalAgent",
504
+ "Bytespider",
505
+ "cohere-training-data-crawler"
506
+ ],
507
+ search: ["OAI-SearchBot", "Claude-SearchBot", "PerplexityBot"],
508
+ user: ["ChatGPT-User", "Claude-User", "Perplexity-User"]
509
+ };
313
510
  function robotsTxt(options = {}) {
314
511
  const config = options.config;
315
512
  const trim = (path) => `/${path.replace(/^\/+|\/+$/g, "")}`;
316
513
  const admin = trim(config?.admin?.path ?? "/admin");
317
514
  const api = trim(config?.routes?.api ?? "/api/cms");
318
515
  const site = options.siteUrl ?? (config?.admin?.siteUrl || config?.serverURL);
319
- const lines = ["User-agent: *"];
320
- if (options.disallowAll) {
321
- lines.push("Disallow: /");
322
- } else {
323
- lines.push(`Allow: ${api}/media/file/`, `Disallow: ${admin}/`, `Disallow: ${api}/`);
324
- for (const path of options.disallow ?? []) lines.push(`Disallow: ${path}`);
516
+ const base = options.disallowAll ? ["Disallow: /"] : [
517
+ `Allow: ${api}/media/file/`,
518
+ `Disallow: ${admin}/`,
519
+ `Disallow: ${api}/`,
520
+ ...(options.disallow ?? []).map((path) => `Disallow: ${path}`)
521
+ ];
522
+ const lines = ["User-agent: *", ...base];
523
+ const group = (agents, rules) => {
524
+ lines.push("", ...agents.map((agent) => `User-agent: ${agent}`), ...rules);
525
+ };
526
+ if (!options.disallowAll) {
527
+ const blocked = ["training", "search", "user"].flatMap(
528
+ (kind) => options.ai?.[kind] === false ? AI_CRAWLERS[kind] : []
529
+ );
530
+ if (blocked.length > 0) group(blocked, ["Disallow: /"]);
531
+ for (const rule of options.rules ?? []) {
532
+ const agents = typeof rule.userAgent === "string" ? [rule.userAgent] : rule.userAgent;
533
+ group(agents, [
534
+ ...(rule.allow ?? []).map((path) => `Allow: ${path}`),
535
+ ...(rule.disallow ?? []).map((path) => `Disallow: ${path}`),
536
+ ...base
537
+ ]);
538
+ }
325
539
  }
326
540
  const sitemap2 = options.sitemap === false ? void 0 : options.sitemap ?? (site ? `${site.replace(/\/+$/, "")}/sitemap.xml` : void 0);
327
541
  if (sitemap2) lines.push("", `Sitemap: ${sitemap2}`);
@@ -329,6 +543,89 @@ function robotsTxt(options = {}) {
329
543
  `;
330
544
  }
331
545
 
546
+ // src/sitemap.ts
547
+ var SITEMAP_LIMIT = 5e4;
548
+ async function sitemap(cms, options = {}) {
549
+ const source = findSource(cms, "sitemap");
550
+ const site = siteOf(cms, options.siteUrl);
551
+ const localization = cms.config.localization;
552
+ const locales = localization ? [...localization.locales] : [null];
553
+ const pages = /* @__PURE__ */ new Map();
554
+ for (const locale of locales) {
555
+ await visiblePages(cms, source, {
556
+ locale,
557
+ site,
558
+ each: ({ key, doc, url }) => {
559
+ const page = pages.get(key) ?? { urls: /* @__PURE__ */ new Map() };
560
+ page.urls.set(locale, url);
561
+ if (typeof doc.updatedAt === "string") page.lastModified = doc.updatedAt;
562
+ pages.set(key, page);
563
+ }
564
+ });
565
+ }
566
+ const entries = [];
567
+ for (const { urls, lastModified } of pages.values()) {
568
+ const distinct = new Set(urls.values());
569
+ const languages = {};
570
+ if (localization && distinct.size > 1) {
571
+ for (const [locale, url] of urls) languages[locale] = url;
572
+ const fallback = urls.get(localization.defaultLocale);
573
+ if (fallback) languages["x-default"] = fallback;
574
+ }
575
+ for (const url of distinct) {
576
+ if (!/^https?:\/\//.test(url))
577
+ throw new Error(
578
+ `sitemap: "${url}" is not an absolute URL. Set admin.siteUrl in the config, or pass siteUrl.`
579
+ );
580
+ entries.push({
581
+ url,
582
+ ...lastModified ? { lastModified } : {},
583
+ ...Object.keys(languages).length > 0 ? { alternates: { languages } } : {}
584
+ });
585
+ }
586
+ }
587
+ return entries;
588
+ }
589
+ async function sitemapXml(cms, options = {}) {
590
+ const entries = await sitemap(cms, options);
591
+ const page = Number(options.page ?? 0);
592
+ if (entries.length > SITEMAP_LIMIT && !(page >= 1)) {
593
+ const site = siteOf(cms, options.siteUrl) ?? "";
594
+ const base = options.base ?? `${site.replace(/\/+$/, "")}/sitemap.xml`;
595
+ const pages = Math.ceil(entries.length / SITEMAP_LIMIT);
596
+ const items = Array.from(
597
+ { length: pages },
598
+ (_, i) => ` <sitemap><loc>${xml(`${base}?page=${i + 1}`)}</loc></sitemap>`
599
+ );
600
+ return `<?xml version="1.0" encoding="UTF-8"?>
601
+ <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
602
+ ${items.join("\n")}
603
+ </sitemapindex>
604
+ `;
605
+ }
606
+ const slice = entries.length > SITEMAP_LIMIT ? entries.slice((page - 1) * SITEMAP_LIMIT, page * SITEMAP_LIMIT) : entries;
607
+ const localized = slice.some((e) => e.alternates);
608
+ const urls = slice.map((entry) => {
609
+ const lines = [` <loc>${xml(entry.url)}</loc>`];
610
+ if (entry.lastModified) lines.push(` <lastmod>${xml(entry.lastModified)}</lastmod>`);
611
+ for (const [lang, href] of Object.entries(entry.alternates?.languages ?? {}))
612
+ lines.push(` <xhtml:link rel="alternate" hreflang="${xml(lang)}" href="${xml(href)}"/>`);
613
+ return ` <url>
614
+ ${lines.join("\n")}
615
+ </url>`;
616
+ });
617
+ const namespaces = `xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"${localized ? ' xmlns:xhtml="http://www.w3.org/1999/xhtml"' : ""}`;
618
+ return `<?xml version="1.0" encoding="UTF-8"?>
619
+ <urlset ${namespaces}>
620
+ ${urls.join("\n")}
621
+ </urlset>
622
+ `;
623
+ }
624
+ var xml = (value) => value.replace(
625
+ /[&<>"']/g,
626
+ (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&apos;" })[c]
627
+ );
628
+
332
629
  // src/plugin.ts
333
630
  var ADMIN_MODULE = "@easy-cms/plugin-seo/admin";
334
631
  var TEXT_KINDS = ["title", "description"];
@@ -430,18 +727,73 @@ function seoPlugin(options = {}) {
430
727
  }
431
728
  return { ...data, [META_FIELD]: meta };
432
729
  } : void 0;
730
+ if (options.indexNow && !INDEXNOW_KEY.test(options.indexNow.key ?? ""))
731
+ throw new Error(
732
+ "seoPlugin: indexNow.key must be 8\u2013128 letters, digits or dashes, e.g. a UUID"
733
+ );
734
+ const indexNow = options.indexNow ? createIndexNow(options.indexNow) : void 0;
735
+ const urlsOf = async (doc, target, cms) => {
736
+ if (!generators.url) return [];
737
+ const site = config.admin?.siteUrl || config.serverURL;
738
+ const locales = config.localization?.locales ?? [null];
739
+ const urls = /* @__PURE__ */ new Set();
740
+ for (const locale of locales) {
741
+ const id = doc.id ?? null;
742
+ const url = await generators.url({ doc, id, locale, cms, user: null, ...target });
743
+ if (typeof url === "string" && url.trim()) urls.add(absolute(url.trim(), site));
744
+ }
745
+ return [...urls];
746
+ };
747
+ const live = (doc) => !!doc && (doc.status === void 0 || doc.status === "published");
748
+ const wasLive = /* @__PURE__ */ new Map();
749
+ const liveNow = async (cms, slug, id) => live(
750
+ await cms.findById(slug, id, { depth: 0 }) ?? void 0
751
+ );
752
+ const indexNowBefore = async ({
753
+ data,
754
+ operation,
755
+ originalDoc,
756
+ cms,
757
+ slug
758
+ }) => {
759
+ if (operation === "update" && originalDoc?.id !== void 0 && !globals.includes(slug))
760
+ wasLive.set(`${slug}:${originalDoc.id}`, await liveNow(cms, slug, originalDoc.id));
761
+ return data;
762
+ };
763
+ const indexNowChange = async ({ doc, previousDoc, cms, slug }) => {
764
+ if (!indexNow) return;
765
+ if (globals.includes(slug)) {
766
+ if (live(doc) || live(previousDoc))
767
+ indexNow.submit(await urlsOf(doc, { global: slug }, cms), cms.logger);
768
+ return;
769
+ }
770
+ const key = `${slug}:${doc.id}`;
771
+ const before = wasLive.get(key);
772
+ wasLive.delete(key);
773
+ if (!live(doc)) {
774
+ if (!before || await liveNow(cms, slug, doc.id)) return;
775
+ }
776
+ indexNow.submit(await urlsOf(doc, { collection: slug }, cms), cms.logger);
777
+ };
778
+ const indexNowDelete = async ({ doc, cms, slug }) => {
779
+ if (indexNow && live(doc))
780
+ indexNow.submit(await urlsOf(doc, { collection: slug }, cms), cms.logger);
781
+ };
433
782
  const withSeo = (container, titleField) => {
434
783
  if (container.fields.some((f) => f.name === META_FIELD))
435
784
  throw new Error(`seoPlugin: "${container.slug}" already has a field named "${META_FIELD}"`);
785
+ const hooks = { ...container.hooks };
786
+ if (hook) hooks.beforeChange = [...hooks.beforeChange ?? [], hook];
787
+ if (indexNow) {
788
+ hooks.beforeChange = [...hooks.beforeChange ?? [], indexNowBefore];
789
+ hooks.afterChange = [...hooks.afterChange ?? [], indexNowChange];
790
+ if (!globals.includes(container.slug))
791
+ hooks.afterDelete = [...hooks.afterDelete ?? [], indexNowDelete];
792
+ }
436
793
  return {
437
794
  ...container,
438
795
  fields: [...container.fields, group(titleField)],
439
- ...hook ? {
440
- hooks: {
441
- ...container.hooks,
442
- beforeChange: [...container.hooks?.beforeChange ?? [], hook]
443
- }
444
- } : {}
796
+ ...hook || indexNow ? { hooks } : {}
445
797
  };
446
798
  };
447
799
  const endpoint = {
@@ -476,7 +828,9 @@ function seoPlugin(options = {}) {
476
828
  const source = {
477
829
  collections,
478
830
  globals,
479
- generateURL: generators.url
831
+ generateURL: generators.url,
832
+ llms: options.llms || {},
833
+ markdown: options.markdown ?? {}
480
834
  };
481
835
  const xmlResponse = (body) => new Response(body, {
482
836
  headers: {
@@ -496,8 +850,8 @@ function seoPlugin(options = {}) {
496
850
  })
497
851
  );
498
852
  },
499
- // Lets `sitemap(cms)` find these options from the resolved config.
500
- { [SITEMAP_SOURCE]: source }
853
+ // Lets `sitemap(cms)`, `llmsTxt(cms)`… find these options from the resolved config.
854
+ { [SEO_SOURCE]: source }
501
855
  );
502
856
  const siteEndpoints = [
503
857
  {
@@ -523,6 +877,45 @@ function seoPlugin(options = {}) {
523
877
  })
524
878
  });
525
879
  }
880
+ const textResponse = (body, type = "text/plain") => new Response(body, {
881
+ headers: {
882
+ "content-type": `${type}; charset=utf-8`,
883
+ "cache-control": "public, max-age=600"
884
+ }
885
+ });
886
+ if (options.llms !== false) {
887
+ const site = (origin) => config.admin?.siteUrl || config.serverURL || origin;
888
+ siteEndpoints.push(
889
+ {
890
+ path: "/llms.txt",
891
+ method: "get",
892
+ root: true,
893
+ handler: async ({ url, cms }) => textResponse(
894
+ await llmsTxt(cms, { siteUrl: site(url.origin) }),
895
+ "text/markdown"
896
+ )
897
+ },
898
+ {
899
+ path: "/llms-full.txt",
900
+ method: "get",
901
+ root: true,
902
+ handler: async ({ url, cms }) => textResponse(
903
+ await llmsFullTxt(cms, { siteUrl: site(url.origin) }),
904
+ "text/markdown"
905
+ )
906
+ }
907
+ );
908
+ }
909
+ if (options.indexNow) {
910
+ const key = options.indexNow.key;
911
+ siteEndpoints.push({
912
+ path: `/${key}.txt`,
913
+ method: "get",
914
+ root: true,
915
+ // `indexNowKeyFile(cms, path)` finds the key here.
916
+ handler: Object.assign(() => textResponse(key), { [INDEXNOW_SOURCE]: key })
917
+ });
918
+ }
526
919
  return {
527
920
  ...config,
528
921
  collections: (config.collections ?? []).map(
@@ -542,10 +935,15 @@ function error(status, message) {
542
935
  return Response.json({ errors: [{ message }] }, { status });
543
936
  }
544
937
  export {
938
+ AI_CRAWLERS,
545
939
  DEFAULT_DESCRIPTION_LENGTH,
546
940
  DEFAULT_TITLE_LENGTH,
547
941
  SITEMAP_LIMIT,
942
+ docMarkdown,
943
+ indexNowKeyFile,
548
944
  jsonLdScript,
945
+ llmsFullTxt,
946
+ llmsTxt,
549
947
  robotsTxt,
550
948
  seoMeta,
551
949
  seoPlugin,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easy-cms/plugin-seo",
3
- "version": "0.17.0",
3
+ "version": "0.18.0",
4
4
  "description": "SEO for Easy CMS: meta fields, length meters, a search result preview, generators and page metadata",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -16,13 +16,16 @@
16
16
  "files": [
17
17
  "dist"
18
18
  ],
19
+ "dependencies": {
20
+ "@easy-cms/richtext": "^0.18.0"
21
+ },
19
22
  "peerDependencies": {
20
- "@easy-cms/core": "^0.17.0"
23
+ "@easy-cms/core": "^0.18.0"
21
24
  },
22
25
  "devDependencies": {
23
26
  "happy-dom": "^20.14.5",
24
- "@easy-cms/core": "^0.17.0",
25
- "@easy-cms/db-sqlite": "^0.17.0"
27
+ "@easy-cms/core": "^0.18.0",
28
+ "@easy-cms/db-sqlite": "^0.18.0"
26
29
  },
27
30
  "engines": {
28
31
  "node": ">=22.12"