@easy-cms/plugin-seo 0.16.1 → 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.
- package/dist/admin.js +6 -4
- package/dist/index.d.ts +318 -22
- package/dist/index.js +740 -23
- package/package.json +7 -4
package/dist/admin.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var f="/seo/generate";var v=["value","path","doc","collection","global","id","locale","uiLocale","readOnly","options","api"],
|
|
1
|
+
var f="/seo/generate";var v=["value","path","doc","collection","global","id","locale","uiLocale","readOnly","options","api"],x={en:{characters:"{n} / {max} characters",empty:"Empty: search engines pick text from the page",short:"Short: aim for {min}\u2013{max}",good:"Good length",long:"Too long: may be cut off after {max}",generate:"Generate",generating:"Generating\u2026",generateImage:"Use image from content",noValue:"Nothing to generate from yet",failed:"Could not generate: {message}",preview:"Search result preview",noTitle:"Page title",noDescription:"Add a description to control the text under the title.",hidden:'Hidden from search engines: the page gets "noindex" and is left out of the sitemap.'},th:{characters:"{n} / {max} \u0E15\u0E31\u0E27\u0E2D\u0E31\u0E01\u0E29\u0E23",empty:"\u0E27\u0E48\u0E32\u0E07: \u0E40\u0E04\u0E23\u0E37\u0E48\u0E2D\u0E07\u0E21\u0E37\u0E2D\u0E04\u0E49\u0E19\u0E2B\u0E32\u0E08\u0E30\u0E40\u0E25\u0E37\u0E2D\u0E01\u0E02\u0E49\u0E2D\u0E04\u0E27\u0E32\u0E21\u0E08\u0E32\u0E01\u0E2B\u0E19\u0E49\u0E32\u0E40\u0E2D\u0E07",short:"\u0E2A\u0E31\u0E49\u0E19\u0E44\u0E1B: \u0E04\u0E27\u0E23\u0E22\u0E32\u0E27 {min}\u2013{max}",good:"\u0E04\u0E27\u0E32\u0E21\u0E22\u0E32\u0E27\u0E1E\u0E2D\u0E14\u0E35",long:"\u0E22\u0E32\u0E27\u0E44\u0E1B: \u0E2D\u0E32\u0E08\u0E16\u0E39\u0E01\u0E15\u0E31\u0E14\u0E2B\u0E25\u0E31\u0E07 {max}",generate:"\u0E2A\u0E23\u0E49\u0E32\u0E07\u0E43\u0E2B\u0E49",generating:"\u0E01\u0E33\u0E25\u0E31\u0E07\u0E2A\u0E23\u0E49\u0E32\u0E07\u2026",generateImage:"\u0E43\u0E0A\u0E49\u0E23\u0E39\u0E1B\u0E08\u0E32\u0E01\u0E40\u0E19\u0E37\u0E49\u0E2D\u0E2B\u0E32",noValue:"\u0E22\u0E31\u0E07\u0E44\u0E21\u0E48\u0E21\u0E35\u0E02\u0E49\u0E2D\u0E21\u0E39\u0E25\u0E43\u0E2B\u0E49\u0E2A\u0E23\u0E49\u0E32\u0E07",failed:"\u0E2A\u0E23\u0E49\u0E32\u0E07\u0E44\u0E21\u0E48\u0E2A\u0E33\u0E40\u0E23\u0E47\u0E08: {message}",preview:"\u0E15\u0E31\u0E27\u0E2D\u0E22\u0E48\u0E32\u0E07\u0E1C\u0E25\u0E01\u0E32\u0E23\u0E04\u0E49\u0E19\u0E2B\u0E32",noTitle:"\u0E0A\u0E37\u0E48\u0E2D\u0E2B\u0E19\u0E49\u0E32",noDescription:"\u0E40\u0E1E\u0E34\u0E48\u0E21\u0E04\u0E33\u0E2D\u0E18\u0E34\u0E1A\u0E32\u0E22\u0E40\u0E1E\u0E37\u0E48\u0E2D\u0E01\u0E33\u0E2B\u0E19\u0E14\u0E02\u0E49\u0E2D\u0E04\u0E27\u0E32\u0E21\u0E43\u0E15\u0E49\u0E0A\u0E37\u0E48\u0E2D\u0E2B\u0E19\u0E49\u0E32",hidden:'\u0E0B\u0E48\u0E2D\u0E19\u0E08\u0E32\u0E01\u0E40\u0E04\u0E23\u0E37\u0E48\u0E2D\u0E07\u0E21\u0E37\u0E2D\u0E04\u0E49\u0E19\u0E2B\u0E32\u0E2D\u0E22\u0E39\u0E48: \u0E2B\u0E19\u0E49\u0E32\u0E19\u0E35\u0E49\u0E21\u0E35 "noindex" \u0E41\u0E25\u0E30\u0E44\u0E21\u0E48\u0E2D\u0E22\u0E39\u0E48\u0E43\u0E19 sitemap'}},i=r=>r.replace(/[&<>"']/g,e=>({"&":"&","<":"<",">":">",'"':""","'":"'"})[e]);function y(r){if(typeof Intl.Segmenter!="function")return[...r].length;let e=0;for(let t of new Intl.Segmenter(void 0,{granularity:"grapheme"}).segment(r))e++;return e}function b(r,e){let t=[...r];return t.length>e?`${t.slice(0,e-1).join("").trimEnd()}\u2026`:r}var w=`
|
|
2
2
|
:host { display: block; font: inherit; color: var(--text); }
|
|
3
3
|
.row { display: flex; align-items: center; gap: 0.75rem; flex-wrap: wrap; }
|
|
4
4
|
.hint { margin: 0; font-size: 0.8rem; color: var(--text-muted); }
|
|
@@ -13,7 +13,7 @@ var f="/seo/generate";var v=["value","path","doc","collection","global","id","lo
|
|
|
13
13
|
button:hover:not(:disabled) { background: var(--surface-2); }
|
|
14
14
|
button:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
|
|
15
15
|
button:disabled { opacity: 0.6; cursor: default; }
|
|
16
|
-
`,d=class r extends HTMLElement{ctx={value:void 0,path:void 0,doc:{},collection:void 0,global:void 0,id:null,locale:null,uiLocale:"en",readOnly:!1,options:{},api:void 0};busy=!1;notice=null;queued=!1;html="";constructor(){super(),this.attachShadow({mode:"open"}).addEventListener("click",t=>{let n=t.target.closest("[data-action]")?.getAttribute("data-action");n&&this.onAction(n)})}static{for(let e of v)Object.defineProperty(r.prototype,e,{get(){return this.ctx[e]},set(t){this.ctx[e]=t,this.update()}})}connectedCallback(){this.update()}t(e,t={}){return(this.ctx.uiLocale==="th"?
|
|
16
|
+
`,d=class r extends HTMLElement{ctx={value:void 0,path:void 0,doc:{},collection:void 0,global:void 0,id:null,locale:null,uiLocale:"en",readOnly:!1,options:{},api:void 0};busy=!1;notice=null;queued=!1;html="";constructor(){super(),this.attachShadow({mode:"open"}).addEventListener("click",t=>{let n=t.target.closest("[data-action]")?.getAttribute("data-action");n&&this.onAction(n)})}static{for(let e of v)Object.defineProperty(r.prototype,e,{get(){return this.ctx[e]},set(t){this.ctx[e]=t,this.update()}})}connectedCallback(){this.update()}t(e,t={}){return(this.ctx.uiLocale==="th"?x.th:x.en)[e].replace(/\{(\w+)\}/g,(o,c)=>String(t[c]??""))}update(){this.queued||(this.queued=!0,queueMicrotask(()=>{this.queued=!1;let e=this.shadowRoot;if(!e)return;let t=`<style>${w}${this.css()}</style>${this.render()}`;if(t===this.html)return;let n=e.activeElement?.getAttribute("data-action");this.html=t,e.innerHTML=t,n&&e.querySelector(`[data-action="${n}"]`)?.focus()}))}async generate(e){let{api:t,collection:n,global:o,id:c,doc:s,locale:a}=this.ctx;return t?(await t("POST",f,{kind:e,...n?{collection:n}:{},...o?{global:o}:{},id:c,doc:s,locale:a})).value??null:null}async generateInto(e,t){if(!(!t||this.busy)){this.busy=!0,this.notice=null,this.update();try{let n=await this.generate(e);n===null||n===""?this.notice={text:this.t("noValue"),error:!1}:this.dispatchEvent(new CustomEvent("set-field",{detail:{path:t,value:n}}))}catch(n){this.notice={text:this.t("failed",{message:n.message}),error:!0}}finally{this.busy=!1,this.update()}}}renderNotice(){if(!this.notice)return"";let{text:e,error:t}=this.notice;return t?`<p class="error" role="alert">${i(e)}</p>`:`<p class="hint" role="status">${i(e)}</p>`}onAction(e){}css(){return""}},p=class extends d{css(){return`
|
|
17
17
|
.bar { flex: 1 1 8rem; height: 4px; border-radius: 2px; background: var(--surface-2); overflow: hidden; }
|
|
18
18
|
.fill { height: 100%; border-radius: 2px; }
|
|
19
19
|
.empty .fill { background: var(--border-strong); }
|
|
@@ -42,11 +42,13 @@ var f="/seo/generate";var v=["value","path","doc","collection","global","id","lo
|
|
|
42
42
|
.title { margin: 0.15rem 0; font-size: 1.15rem; line-height: 1.3; color: var(--info); }
|
|
43
43
|
.description { margin: 0; font-size: 0.875rem; line-height: 1.5; color: var(--text-muted); }
|
|
44
44
|
.placeholder { font-style: italic; opacity: 0.75; }
|
|
45
|
+
.noindex { margin: 0.6rem 0 0; font-size: 0.8rem; color: var(--warning, var(--text-muted)); }
|
|
45
46
|
`}render(){let{titleField:e,url:t,siteUrl:n}=this.ctx.options,o=k(this.ctx.doc,this.ctx.path)??{},c=e?this.ctx.doc[e]:void 0,s=[o.title,c].find(u=>typeof u=="string"&&u.trim()),a=typeof o.description=="string"?o.description.trim():"";t&&this.refreshUrl();let l=E(t?this.url:n);return`
|
|
46
47
|
<section class="card" aria-label="${i(this.t("preview"))}">
|
|
47
48
|
<p class="label">${i(this.t("preview"))}</p>
|
|
48
49
|
${l?`<p class="url">${i(l)}</p>`:""}
|
|
49
|
-
<p class="title ${s?"":"placeholder"}">${i(
|
|
50
|
-
<p class="description ${a?"":"placeholder"}">${i(a?
|
|
50
|
+
<p class="title ${s?"":"placeholder"}">${i(b(s??this.t("noTitle"),60))}</p>
|
|
51
|
+
<p class="description ${a?"":"placeholder"}">${i(a?b(a,160):this.t("noDescription"))}</p>
|
|
52
|
+
${o.noindex===!0?`<p class="noindex">${i(this.t("hidden"))}</p>`:""}
|
|
51
53
|
</section>
|
|
52
54
|
`}refreshUrl(){let e=JSON.stringify([this.ctx.doc,this.ctx.locale]);e!==this.urlFor&&(this.urlFor=e,clearTimeout(this.timer),this.timer=setTimeout(async()=>{try{let t=await this.generate("url"),n=typeof t=="string"?t:"";n!==this.url&&(this.url=n,this.update())}catch{}},400))}};function k(r,e){if(e)return e.split(".").reduce((t,n)=>typeof t=="object"&&t!==null?t[n]:void 0,r)}function E(r){if(!r)return"";try{let{host:e,pathname:t}=new URL(r,"https://site.invalid"),n=t.split("/").filter(Boolean).map(decodeURIComponent);return[e==="site.invalid"?"":e,...n].filter(Boolean).join(" \u203A ")}catch{return r}}var m=(r,e)=>{customElements.get(r)||customElements.define(r,e)};m("ecms-seo-meter",p);m("ecms-seo-image",g);m("ecms-seo-preview",h);
|
package/dist/index.d.ts
CHANGED
|
@@ -1,27 +1,166 @@
|
|
|
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;
|
|
27
|
+
|
|
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;
|
|
2
128
|
|
|
3
129
|
type Doc = Record<string, unknown>;
|
|
130
|
+
type Value = string | null | undefined;
|
|
4
131
|
interface SeoMetaOptions {
|
|
5
132
|
/**
|
|
6
133
|
* The site's public address, to make the canonical and image URLs absolute, e.g.
|
|
7
134
|
* `https://example.com`. Default: `config.admin.siteUrl`, then `config.serverURL`.
|
|
8
135
|
*/
|
|
9
136
|
readonly siteUrl?: string;
|
|
10
|
-
/** Your Easy CMS config, to read `admin.siteUrl`
|
|
11
|
-
readonly config?:
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
/** The page's
|
|
18
|
-
readonly
|
|
137
|
+
/** Your Easy CMS config, to read `admin.siteUrl`, `serverURL` and `localization` from. */
|
|
138
|
+
readonly config?: object;
|
|
139
|
+
/**
|
|
140
|
+
* The page's canonical address: absolute, or a path on the site. As a function it gets the
|
|
141
|
+
* locale too, which adds `hreflang` links to the page in every locale.
|
|
142
|
+
*/
|
|
143
|
+
readonly url?: string | ((doc: Doc, locale?: string) => Value);
|
|
144
|
+
/** The page's content locale, e.g. `en`. Shown as `og:locale`. */
|
|
145
|
+
readonly locale?: string;
|
|
146
|
+
/** All content locales, for `hreflang`. Default: `config.localization.locales`. */
|
|
147
|
+
readonly locales?: readonly string[];
|
|
148
|
+
/** The locale for `x-default`. Default: `config.localization.defaultLocale`, then the first. */
|
|
149
|
+
readonly defaultLocale?: string;
|
|
19
150
|
/** The title when `meta.title` is empty. Default: the document's `title`. */
|
|
20
|
-
readonly title?: (doc: Doc) =>
|
|
151
|
+
readonly title?: (doc: Doc) => Value;
|
|
21
152
|
/** The description when `meta.description` is empty. Default: `excerpt`, then `description`. */
|
|
22
|
-
readonly description?: (doc: Doc) =>
|
|
153
|
+
readonly description?: (doc: Doc) => Value;
|
|
23
154
|
/** Shown as `og:site_name`. */
|
|
24
155
|
readonly siteName?: string;
|
|
156
|
+
/** `article` for posts: adds the published and modified times, and BlogPosting JSON-LD. */
|
|
157
|
+
readonly type?: 'website' | 'article';
|
|
158
|
+
/** When the article was published. Default: `publishedAt`, then `createdAt`. */
|
|
159
|
+
readonly publishedTime?: (doc: Doc) => Value;
|
|
160
|
+
/** The article's author, e.g. `(post) => post.author?.name`. */
|
|
161
|
+
readonly author?: string | ((doc: Doc) => Value);
|
|
162
|
+
/** Schema.org type of an article's JSON-LD. Default `BlogPosting`. */
|
|
163
|
+
readonly articleType?: 'Article' | 'BlogPosting' | 'NewsArticle';
|
|
25
164
|
}
|
|
26
165
|
interface SeoImage {
|
|
27
166
|
readonly url: string;
|
|
@@ -33,34 +172,61 @@ interface SeoImage {
|
|
|
33
172
|
interface NuxtSeoMeta {
|
|
34
173
|
title?: string;
|
|
35
174
|
description?: string;
|
|
175
|
+
robots?: string;
|
|
36
176
|
ogTitle?: string;
|
|
37
177
|
ogDescription?: string;
|
|
38
178
|
ogUrl?: string;
|
|
39
|
-
ogType?: 'website';
|
|
179
|
+
ogType?: 'website' | 'article';
|
|
40
180
|
ogSiteName?: string;
|
|
181
|
+
ogLocale?: string;
|
|
182
|
+
ogLocaleAlternate?: string[];
|
|
41
183
|
ogImage?: string;
|
|
42
184
|
ogImageAlt?: string;
|
|
43
185
|
ogImageWidth?: number;
|
|
44
186
|
ogImageHeight?: number;
|
|
187
|
+
articlePublishedTime?: string;
|
|
188
|
+
articleModifiedTime?: string;
|
|
189
|
+
articleAuthor?: string[];
|
|
45
190
|
twitterCard?: 'summary' | 'summary_large_image';
|
|
46
191
|
twitterTitle?: string;
|
|
47
192
|
twitterDescription?: string;
|
|
48
193
|
twitterImage?: string;
|
|
49
194
|
}
|
|
195
|
+
/** Input for Nuxt's `useHead()`: the canonical and hreflang links, and the JSON-LD script. */
|
|
196
|
+
interface NuxtSeoHead {
|
|
197
|
+
link: {
|
|
198
|
+
rel: 'canonical' | 'alternate';
|
|
199
|
+
href: string;
|
|
200
|
+
hreflang?: string;
|
|
201
|
+
}[];
|
|
202
|
+
script: {
|
|
203
|
+
type: 'application/ld+json';
|
|
204
|
+
innerHTML: string;
|
|
205
|
+
}[];
|
|
206
|
+
}
|
|
50
207
|
/** The part of Next.js `Metadata` that `seoMeta` fills. */
|
|
51
208
|
interface NextSeoMetadata {
|
|
52
209
|
title?: string;
|
|
53
210
|
description?: string;
|
|
211
|
+
robots?: {
|
|
212
|
+
index: false;
|
|
213
|
+
};
|
|
54
214
|
alternates?: {
|
|
55
|
-
canonical
|
|
215
|
+
canonical?: string;
|
|
216
|
+
languages?: Record<string, string>;
|
|
56
217
|
};
|
|
57
218
|
openGraph: {
|
|
58
|
-
type: 'website';
|
|
219
|
+
type: 'website' | 'article';
|
|
59
220
|
title?: string;
|
|
60
221
|
description?: string;
|
|
61
222
|
url?: string;
|
|
62
223
|
siteName?: string;
|
|
224
|
+
locale?: string;
|
|
225
|
+
alternateLocale?: string[];
|
|
63
226
|
images?: SeoImage[];
|
|
227
|
+
publishedTime?: string;
|
|
228
|
+
modifiedTime?: string;
|
|
229
|
+
authors?: string[];
|
|
64
230
|
};
|
|
65
231
|
twitter: {
|
|
66
232
|
card: 'summary' | 'summary_large_image';
|
|
@@ -69,14 +235,23 @@ interface NextSeoMetadata {
|
|
|
69
235
|
images?: string[];
|
|
70
236
|
};
|
|
71
237
|
}
|
|
238
|
+
type JsonLd = Record<string, unknown>;
|
|
72
239
|
interface SeoMeta {
|
|
73
240
|
readonly title: string | undefined;
|
|
74
241
|
readonly description: string | undefined;
|
|
75
242
|
readonly canonical: string | undefined;
|
|
76
243
|
readonly image: SeoImage | undefined;
|
|
77
|
-
/**
|
|
244
|
+
/** The editor ticked "Hide from search engines". */
|
|
245
|
+
readonly noindex: boolean;
|
|
246
|
+
/** The page in each locale, and `x-default`; `undefined` without locales. */
|
|
247
|
+
readonly alternates: Readonly<Record<string, string>> | undefined;
|
|
248
|
+
/** Schema.org data for the page: BlogPosting (or `articleType`) for articles, else WebPage. */
|
|
249
|
+
readonly jsonLd: JsonLd;
|
|
250
|
+
/** For Nuxt's `useSeoMeta()`. */
|
|
78
251
|
readonly nuxt: NuxtSeoMeta;
|
|
79
|
-
/** For
|
|
252
|
+
/** For Nuxt's `useHead()`: canonical and hreflang links, and the JSON-LD script. */
|
|
253
|
+
readonly head: NuxtSeoHead;
|
|
254
|
+
/** For Next.js `generateMetadata()`. Render `jsonLd` with `jsonLdScript()` in the page. */
|
|
80
255
|
readonly next: NextSeoMetadata;
|
|
81
256
|
}
|
|
82
257
|
/**
|
|
@@ -84,12 +259,81 @@ interface SeoMeta {
|
|
|
84
259
|
* document. Fetch the document with `depth` 1 or more so the share image has its URL.
|
|
85
260
|
*
|
|
86
261
|
* ```ts
|
|
87
|
-
* const meta = seoMeta(post, { config, url:
|
|
88
|
-
* useSeoMeta(meta.nuxt)
|
|
89
|
-
* return meta.next
|
|
262
|
+
* const meta = seoMeta(post, { config, locale, url: (p, l) => `/${l}/posts/${p.slug}`, type: 'article' })
|
|
263
|
+
* useSeoMeta(meta.nuxt); useHead(meta.head) // Nuxt
|
|
264
|
+
* return meta.next // Next.js generateMetadata()
|
|
90
265
|
* ```
|
|
91
266
|
*/
|
|
92
267
|
declare function seoMeta(doc: Doc, options?: SeoMetaOptions): SeoMeta;
|
|
268
|
+
interface SiteJsonLdOptions {
|
|
269
|
+
/** The organization or site name. */
|
|
270
|
+
readonly name: string;
|
|
271
|
+
/** The site's address, e.g. `https://example.com`. */
|
|
272
|
+
readonly url: string;
|
|
273
|
+
/** Address of the logo image. */
|
|
274
|
+
readonly logo?: string;
|
|
275
|
+
/** Profiles elsewhere, e.g. Facebook or LinkedIn pages. */
|
|
276
|
+
readonly sameAs?: readonly string[];
|
|
277
|
+
/** The site's main language, e.g. `th`. */
|
|
278
|
+
readonly inLanguage?: string;
|
|
279
|
+
}
|
|
280
|
+
/** Organization and WebSite JSON-LD for the site's layout, once per page. */
|
|
281
|
+
declare function siteJsonLd(options: SiteJsonLdOptions): JsonLd;
|
|
282
|
+
/**
|
|
283
|
+
* JSON-LD as text for a `<script type="application/ld+json">`, safe to put in HTML: `<` and
|
|
284
|
+
* line separators are escaped, so content can't close the script tag.
|
|
285
|
+
*/
|
|
286
|
+
declare function jsonLdScript(value: JsonLd): string;
|
|
287
|
+
|
|
288
|
+
interface RobotsTxtOptions {
|
|
289
|
+
/** Your Easy CMS config, for the admin and API paths and `admin.siteUrl`. */
|
|
290
|
+
readonly config?: object;
|
|
291
|
+
/** The site's public address, for the `Sitemap:` line. Default `admin.siteUrl`, then `serverURL`. */
|
|
292
|
+
readonly siteUrl?: string;
|
|
293
|
+
/** The sitemap's address, or `false` for none. Default `<siteUrl>/sitemap.xml`. */
|
|
294
|
+
readonly sitemap?: string | false;
|
|
295
|
+
/**
|
|
296
|
+
* Ask search engines to stay out of the whole site, e.g. on a staging copy. Not turned on by
|
|
297
|
+
* `NODE_ENV`: staging servers usually run in production mode too, so set it yourself.
|
|
298
|
+
*/
|
|
299
|
+
readonly disallowAll?: boolean;
|
|
300
|
+
/** More paths to keep out of search, e.g. `['/search']`. */
|
|
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[];
|
|
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
|
+
};
|
|
332
|
+
/**
|
|
333
|
+
* A `robots.txt` that keeps search engines out of the admin and the API (except uploaded
|
|
334
|
+
* files, so share images still work) and points them to the sitemap.
|
|
335
|
+
*/
|
|
336
|
+
declare function robotsTxt(options?: RobotsTxtOptions): string;
|
|
93
337
|
|
|
94
338
|
type MaybePromise<T> = T | Promise<T>;
|
|
95
339
|
interface GenerateArgs {
|
|
@@ -139,7 +383,22 @@ interface SeoPluginOptions {
|
|
|
139
383
|
/** One value per content locale. Default: true when the config has `localization`. */
|
|
140
384
|
readonly localized?: boolean;
|
|
141
385
|
/** Label of the group. Default "SEO". */
|
|
142
|
-
readonly label?: Label;
|
|
386
|
+
readonly label?: Label$1;
|
|
387
|
+
/**
|
|
388
|
+
* `robots.txt` for the standalone server (`easy-cms serve`), or `false` for none. Nuxt and
|
|
389
|
+
* Next.js apps serve their own with `robotsTxt()`.
|
|
390
|
+
*/
|
|
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;
|
|
143
402
|
}
|
|
144
403
|
/**
|
|
145
404
|
* Adds a `meta` group (title, description, image) to the chosen collections and globals,
|
|
@@ -158,4 +417,41 @@ declare const DEFAULT_DESCRIPTION_LENGTH: {
|
|
|
158
417
|
readonly max: 150;
|
|
159
418
|
};
|
|
160
419
|
|
|
161
|
-
|
|
420
|
+
/** One page in the sitemap, in the shape of Next.js `MetadataRoute.Sitemap`. */
|
|
421
|
+
interface SitemapEntry {
|
|
422
|
+
readonly url: string;
|
|
423
|
+
/** ISO date of the last change (`updatedAt`). */
|
|
424
|
+
readonly lastModified?: string;
|
|
425
|
+
/** The page in each content locale, and `x-default`. */
|
|
426
|
+
readonly alternates?: {
|
|
427
|
+
readonly languages: Readonly<Record<string, string>>;
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
interface SitemapOptions {
|
|
431
|
+
/**
|
|
432
|
+
* The site's public address, to make relative URLs from `generateURL` absolute. Default:
|
|
433
|
+
* `admin.siteUrl`, then `serverURL`.
|
|
434
|
+
*/
|
|
435
|
+
readonly siteUrl?: string;
|
|
436
|
+
}
|
|
437
|
+
interface SitemapXmlOptions extends SitemapOptions {
|
|
438
|
+
/** Which part of a large sitemap (over 50,000 URLs) to render, from 1. */
|
|
439
|
+
readonly page?: number | string | null;
|
|
440
|
+
/** Address of this sitemap, for the index of a large one. Default `<siteUrl>/sitemap.xml`. */
|
|
441
|
+
readonly base?: string;
|
|
442
|
+
}
|
|
443
|
+
/** Google's limit of URLs in one sitemap file. */
|
|
444
|
+
declare const SITEMAP_LIMIT = 50000;
|
|
445
|
+
/**
|
|
446
|
+
* Every page with SEO fields that a visitor can see: documents read without a login (so only
|
|
447
|
+
* published ones, as the read access allows), with a URL from `generateURL` and not hidden with
|
|
448
|
+
* "noindex". Returns the shape Next.js `app/sitemap.ts` expects.
|
|
449
|
+
*/
|
|
450
|
+
declare function sitemap(cms: SeoCMS, options?: SitemapOptions): Promise<SitemapEntry[]>;
|
|
451
|
+
/**
|
|
452
|
+
* The sitemap as XML. Over 50,000 URLs, it is an index of pages `<base>?page=1`, `?page=2`…,
|
|
453
|
+
* and `page` renders one of them.
|
|
454
|
+
*/
|
|
455
|
+
declare function sitemapXml(cms: SeoCMS, options?: SitemapXmlOptions): Promise<string>;
|
|
456
|
+
|
|
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,45 +84,354 @@ var META_FIELD = "meta";
|
|
|
5
84
|
var GENERATE_PATH = "/seo/generate";
|
|
6
85
|
var GENERATE_KINDS = ["title", "description", "image", "url"];
|
|
7
86
|
|
|
8
|
-
// src/
|
|
9
|
-
var
|
|
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
|
+
}
|
|
10
105
|
function absolute(url, site) {
|
|
11
106
|
if (/^https?:\/\//.test(url) || !site || !/^https?:\/\//.test(site)) return url;
|
|
12
107
|
return `${site.replace(/\/+$/, "")}/${url.replace(/^\/+/, "")}`;
|
|
13
108
|
}
|
|
109
|
+
var siteOf = (cms, siteUrl) => siteUrl ?? (cms.config.admin.siteUrl || cms.config.serverURL);
|
|
110
|
+
var BATCH = 500;
|
|
111
|
+
async function visiblePages(cms, source, options) {
|
|
112
|
+
const generateURL = source.generateURL;
|
|
113
|
+
if (!generateURL) return;
|
|
114
|
+
const { locale, site } = options;
|
|
115
|
+
const pageOf = async (key, doc, target) => {
|
|
116
|
+
if (doc[META_FIELD]?.noindex === true) return;
|
|
117
|
+
const id = doc.id ?? null;
|
|
118
|
+
const url = await generateURL({ doc, id, locale, cms, user: null, ...target });
|
|
119
|
+
if (typeof url !== "string" || url.trim() === "") return;
|
|
120
|
+
await options.each({ key, doc, url: absolute(url.trim(), site), ...target });
|
|
121
|
+
};
|
|
122
|
+
for (const collection of source.collections) {
|
|
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, {
|
|
129
|
+
overrideAccess: false,
|
|
130
|
+
user: null,
|
|
131
|
+
depth: options.depth ?? 0,
|
|
132
|
+
sort: options.sort ?? "id",
|
|
133
|
+
limit,
|
|
134
|
+
page: options.limit === void 0 ? page : 1,
|
|
135
|
+
...locale ? { locale } : {}
|
|
136
|
+
})
|
|
137
|
+
);
|
|
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;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
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 });
|
|
154
|
+
}
|
|
155
|
+
}
|
|
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")}
|
|
188
|
+
`;
|
|
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
|
+
}
|
|
216
|
+
});
|
|
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.)" : ""}
|
|
219
|
+
`;
|
|
220
|
+
}
|
|
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
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return out.filter((part) => part.trim() !== "").join("\n\n");
|
|
291
|
+
}
|
|
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;
|
|
318
|
+
}
|
|
319
|
+
return "Site";
|
|
320
|
+
}
|
|
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()}`);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// src/meta.ts
|
|
338
|
+
var text2 = (value) => typeof value === "string" && value.trim() !== "" ? value.trim() : void 0;
|
|
339
|
+
var ogLocale = (locale) => locale.replace("-", "_");
|
|
340
|
+
var defined = (record) => Object.fromEntries(Object.entries(record).filter(([, v]) => v !== void 0));
|
|
14
341
|
function seoMeta(doc, options = {}) {
|
|
342
|
+
const config = options.config;
|
|
15
343
|
const meta = doc[META_FIELD] ?? {};
|
|
16
|
-
const site = options.siteUrl ??
|
|
17
|
-
const title =
|
|
18
|
-
const description =
|
|
19
|
-
const
|
|
20
|
-
const
|
|
344
|
+
const site = options.siteUrl ?? (config?.admin?.siteUrl || config?.serverURL);
|
|
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));
|
|
347
|
+
const noindex = meta.noindex === true;
|
|
348
|
+
const article = options.type === "article";
|
|
349
|
+
const locale = options.locale;
|
|
350
|
+
const locales = options.locales ?? config?.localization?.locales ?? [];
|
|
351
|
+
const urlFor = (l) => {
|
|
352
|
+
const url = typeof options.url === "function" ? options.url(doc, l) : options.url;
|
|
353
|
+
return text2(url) ? absolute(text2(url), site) : void 0;
|
|
354
|
+
};
|
|
355
|
+
const canonical = urlFor(locale);
|
|
356
|
+
let alternates;
|
|
357
|
+
if (typeof options.url === "function" && locales.length > 1) {
|
|
358
|
+
alternates = {};
|
|
359
|
+
for (const l of locales) {
|
|
360
|
+
const url = urlFor(l);
|
|
361
|
+
if (url) alternates[l] = url;
|
|
362
|
+
}
|
|
363
|
+
const fallback = options.defaultLocale ?? config?.localization?.defaultLocale ?? locales[0];
|
|
364
|
+
if (fallback && alternates[fallback]) alternates["x-default"] = alternates[fallback];
|
|
365
|
+
if (Object.keys(alternates).length === 0) alternates = void 0;
|
|
366
|
+
}
|
|
21
367
|
const media = meta.image;
|
|
22
|
-
const image = typeof media === "object" && media !== null &&
|
|
23
|
-
url: absolute(
|
|
24
|
-
...
|
|
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) } : {},
|
|
25
371
|
...typeof media.width === "number" ? { width: media.width } : {},
|
|
26
372
|
...typeof media.height === "number" ? { height: media.height } : {}
|
|
27
373
|
} : void 0;
|
|
28
|
-
const
|
|
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;
|
|
377
|
+
const otherLocales = locale ? locales.filter((l) => l !== locale).map(ogLocale) : [];
|
|
29
378
|
const card = image ? "summary_large_image" : "summary";
|
|
379
|
+
const type = article ? "article" : "website";
|
|
380
|
+
const jsonLd = article ? defined({
|
|
381
|
+
"@context": "https://schema.org",
|
|
382
|
+
"@type": options.articleType ?? "BlogPosting",
|
|
383
|
+
headline: title,
|
|
384
|
+
description,
|
|
385
|
+
image: image ? [image.url] : void 0,
|
|
386
|
+
datePublished: publishedTime,
|
|
387
|
+
dateModified: modifiedTime,
|
|
388
|
+
author: author ? { "@type": "Person", name: author } : void 0,
|
|
389
|
+
mainEntityOfPage: canonical,
|
|
390
|
+
url: canonical,
|
|
391
|
+
inLanguage: locale
|
|
392
|
+
}) : defined({
|
|
393
|
+
"@context": "https://schema.org",
|
|
394
|
+
"@type": "WebPage",
|
|
395
|
+
name: title,
|
|
396
|
+
description,
|
|
397
|
+
url: canonical,
|
|
398
|
+
primaryImageOfPage: image ? image.url : void 0,
|
|
399
|
+
inLanguage: locale
|
|
400
|
+
});
|
|
401
|
+
const link = [];
|
|
402
|
+
if (canonical) link.push({ rel: "canonical", href: canonical });
|
|
403
|
+
for (const [hreflang, href] of Object.entries(alternates ?? {}))
|
|
404
|
+
link.push({ rel: "alternate", hreflang, href });
|
|
30
405
|
return {
|
|
31
406
|
title,
|
|
32
407
|
description,
|
|
33
408
|
canonical,
|
|
34
409
|
image,
|
|
410
|
+
noindex,
|
|
411
|
+
alternates,
|
|
412
|
+
jsonLd,
|
|
413
|
+
head: {
|
|
414
|
+
link,
|
|
415
|
+
script: [{ type: "application/ld+json", innerHTML: jsonLdScript(jsonLd) }]
|
|
416
|
+
},
|
|
35
417
|
nuxt: defined({
|
|
36
418
|
title,
|
|
37
419
|
description,
|
|
420
|
+
robots: noindex ? "noindex" : void 0,
|
|
38
421
|
ogTitle: title,
|
|
39
422
|
ogDescription: description,
|
|
40
423
|
ogUrl: canonical,
|
|
41
|
-
ogType:
|
|
424
|
+
ogType: type,
|
|
42
425
|
ogSiteName: options.siteName,
|
|
426
|
+
ogLocale: locale ? ogLocale(locale) : void 0,
|
|
427
|
+
ogLocaleAlternate: otherLocales.length > 0 ? otherLocales : void 0,
|
|
43
428
|
ogImage: image?.url,
|
|
44
429
|
ogImageAlt: image?.alt,
|
|
45
430
|
ogImageWidth: image?.width,
|
|
46
431
|
ogImageHeight: image?.height,
|
|
432
|
+
articlePublishedTime: publishedTime,
|
|
433
|
+
articleModifiedTime: modifiedTime,
|
|
434
|
+
articleAuthor: author ? [author] : void 0,
|
|
47
435
|
twitterCard: card,
|
|
48
436
|
twitterTitle: title,
|
|
49
437
|
twitterDescription: description,
|
|
@@ -52,14 +440,20 @@ function seoMeta(doc, options = {}) {
|
|
|
52
440
|
next: defined({
|
|
53
441
|
title,
|
|
54
442
|
description,
|
|
55
|
-
|
|
443
|
+
robots: noindex ? { index: false } : void 0,
|
|
444
|
+
alternates: canonical || alternates ? defined({ canonical, languages: alternates ? { ...alternates } : void 0 }) : void 0,
|
|
56
445
|
openGraph: defined({
|
|
57
446
|
title,
|
|
58
447
|
description,
|
|
59
448
|
url: canonical,
|
|
60
449
|
siteName: options.siteName,
|
|
61
|
-
|
|
62
|
-
|
|
450
|
+
locale: locale ? ogLocale(locale) : void 0,
|
|
451
|
+
alternateLocale: otherLocales.length > 0 ? otherLocales : void 0,
|
|
452
|
+
type,
|
|
453
|
+
images: image ? [image] : void 0,
|
|
454
|
+
publishedTime,
|
|
455
|
+
modifiedTime,
|
|
456
|
+
authors: author ? [author] : void 0
|
|
63
457
|
}),
|
|
64
458
|
twitter: defined({
|
|
65
459
|
card,
|
|
@@ -70,6 +464,167 @@ function seoMeta(doc, options = {}) {
|
|
|
70
464
|
})
|
|
71
465
|
};
|
|
72
466
|
}
|
|
467
|
+
function siteJsonLd(options) {
|
|
468
|
+
const url = options.url.replace(/\/+$/, "");
|
|
469
|
+
return {
|
|
470
|
+
"@context": "https://schema.org",
|
|
471
|
+
"@graph": [
|
|
472
|
+
defined({
|
|
473
|
+
"@type": "Organization",
|
|
474
|
+
"@id": `${url}/#organization`,
|
|
475
|
+
name: options.name,
|
|
476
|
+
url: `${url}/`,
|
|
477
|
+
logo: options.logo,
|
|
478
|
+
sameAs: options.sameAs && options.sameAs.length > 0 ? [...options.sameAs] : void 0
|
|
479
|
+
}),
|
|
480
|
+
defined({
|
|
481
|
+
"@type": "WebSite",
|
|
482
|
+
"@id": `${url}/#website`,
|
|
483
|
+
name: options.name,
|
|
484
|
+
url: `${url}/`,
|
|
485
|
+
inLanguage: options.inLanguage,
|
|
486
|
+
publisher: { "@id": `${url}/#organization` }
|
|
487
|
+
})
|
|
488
|
+
]
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
function jsonLdScript(value) {
|
|
492
|
+
return JSON.stringify(value).replace(/</g, "\\u003c").replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029");
|
|
493
|
+
}
|
|
494
|
+
|
|
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
|
+
};
|
|
510
|
+
function robotsTxt(options = {}) {
|
|
511
|
+
const config = options.config;
|
|
512
|
+
const trim = (path) => `/${path.replace(/^\/+|\/+$/g, "")}`;
|
|
513
|
+
const admin = trim(config?.admin?.path ?? "/admin");
|
|
514
|
+
const api = trim(config?.routes?.api ?? "/api/cms");
|
|
515
|
+
const site = options.siteUrl ?? (config?.admin?.siteUrl || config?.serverURL);
|
|
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
|
+
}
|
|
539
|
+
}
|
|
540
|
+
const sitemap2 = options.sitemap === false ? void 0 : options.sitemap ?? (site ? `${site.replace(/\/+$/, "")}/sitemap.xml` : void 0);
|
|
541
|
+
if (sitemap2) lines.push("", `Sitemap: ${sitemap2}`);
|
|
542
|
+
return `${lines.join("\n")}
|
|
543
|
+
`;
|
|
544
|
+
}
|
|
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) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c]
|
|
627
|
+
);
|
|
73
628
|
|
|
74
629
|
// src/plugin.ts
|
|
75
630
|
var ADMIN_MODULE = "@easy-cms/plugin-seo/admin";
|
|
@@ -130,6 +685,11 @@ function seoPlugin(options = {}) {
|
|
|
130
685
|
]
|
|
131
686
|
}
|
|
132
687
|
} : {}
|
|
688
|
+
},
|
|
689
|
+
{
|
|
690
|
+
name: "noindex",
|
|
691
|
+
type: "boolean",
|
|
692
|
+
label: { en: "Hide from search engines", th: "\u0E0B\u0E48\u0E2D\u0E19\u0E08\u0E32\u0E01\u0E40\u0E04\u0E23\u0E37\u0E48\u0E2D\u0E07\u0E21\u0E37\u0E2D\u0E04\u0E49\u0E19\u0E2B\u0E32" }
|
|
133
693
|
}
|
|
134
694
|
];
|
|
135
695
|
return {
|
|
@@ -167,18 +727,73 @@ function seoPlugin(options = {}) {
|
|
|
167
727
|
}
|
|
168
728
|
return { ...data, [META_FIELD]: meta };
|
|
169
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
|
+
};
|
|
170
782
|
const withSeo = (container, titleField) => {
|
|
171
783
|
if (container.fields.some((f) => f.name === META_FIELD))
|
|
172
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
|
+
}
|
|
173
793
|
return {
|
|
174
794
|
...container,
|
|
175
795
|
fields: [...container.fields, group(titleField)],
|
|
176
|
-
...hook ? {
|
|
177
|
-
hooks: {
|
|
178
|
-
...container.hooks,
|
|
179
|
-
beforeChange: [...container.hooks?.beforeChange ?? [], hook]
|
|
180
|
-
}
|
|
181
|
-
} : {}
|
|
796
|
+
...hook || indexNow ? { hooks } : {}
|
|
182
797
|
};
|
|
183
798
|
};
|
|
184
799
|
const endpoint = {
|
|
@@ -210,6 +825,97 @@ function seoPlugin(options = {}) {
|
|
|
210
825
|
return { value: value ?? null };
|
|
211
826
|
}
|
|
212
827
|
};
|
|
828
|
+
const source = {
|
|
829
|
+
collections,
|
|
830
|
+
globals,
|
|
831
|
+
generateURL: generators.url,
|
|
832
|
+
llms: options.llms || {},
|
|
833
|
+
markdown: options.markdown ?? {}
|
|
834
|
+
};
|
|
835
|
+
const xmlResponse = (body) => new Response(body, {
|
|
836
|
+
headers: {
|
|
837
|
+
"content-type": "application/xml; charset=utf-8",
|
|
838
|
+
"cache-control": "public, max-age=600"
|
|
839
|
+
}
|
|
840
|
+
});
|
|
841
|
+
const api = config.routes?.api ?? "/api/cms";
|
|
842
|
+
const sitemapHandler = (base) => Object.assign(
|
|
843
|
+
async ({ url, cms }) => {
|
|
844
|
+
const site = config.admin?.siteUrl || config.serverURL || url.origin;
|
|
845
|
+
return xmlResponse(
|
|
846
|
+
await sitemapXml(cms, {
|
|
847
|
+
siteUrl: site,
|
|
848
|
+
page: url.searchParams.get("page"),
|
|
849
|
+
base: base(url.origin)
|
|
850
|
+
})
|
|
851
|
+
);
|
|
852
|
+
},
|
|
853
|
+
// Lets `sitemap(cms)`, `llmsTxt(cms)`… find these options from the resolved config.
|
|
854
|
+
{ [SEO_SOURCE]: source }
|
|
855
|
+
);
|
|
856
|
+
const siteEndpoints = [
|
|
857
|
+
{
|
|
858
|
+
path: "/seo/sitemap.xml",
|
|
859
|
+
method: "get",
|
|
860
|
+
handler: sitemapHandler((origin) => `${origin}${api}/seo/sitemap.xml`)
|
|
861
|
+
},
|
|
862
|
+
{
|
|
863
|
+
path: "/sitemap.xml",
|
|
864
|
+
method: "get",
|
|
865
|
+
root: true,
|
|
866
|
+
handler: sitemapHandler((origin) => `${origin}/sitemap.xml`)
|
|
867
|
+
}
|
|
868
|
+
];
|
|
869
|
+
if (options.robots !== false) {
|
|
870
|
+
const robots = options.robots ?? {};
|
|
871
|
+
siteEndpoints.push({
|
|
872
|
+
path: "/robots.txt",
|
|
873
|
+
method: "get",
|
|
874
|
+
root: true,
|
|
875
|
+
handler: ({ url }) => new Response(robotsTxt({ sitemap: `${url.origin}/sitemap.xml`, ...robots, config }), {
|
|
876
|
+
headers: { "content-type": "text/plain; charset=utf-8" }
|
|
877
|
+
})
|
|
878
|
+
});
|
|
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
|
+
}
|
|
213
919
|
return {
|
|
214
920
|
...config,
|
|
215
921
|
collections: (config.collections ?? []).map(
|
|
@@ -220,7 +926,7 @@ function seoPlugin(options = {}) {
|
|
|
220
926
|
...config.admin,
|
|
221
927
|
modules: [.../* @__PURE__ */ new Set([...config.admin?.modules ?? [], ADMIN_MODULE])]
|
|
222
928
|
},
|
|
223
|
-
endpoints: [...config.endpoints ?? [], endpoint]
|
|
929
|
+
endpoints: [...config.endpoints ?? [], endpoint, ...siteEndpoints]
|
|
224
930
|
};
|
|
225
931
|
};
|
|
226
932
|
}
|
|
@@ -229,8 +935,19 @@ function error(status, message) {
|
|
|
229
935
|
return Response.json({ errors: [{ message }] }, { status });
|
|
230
936
|
}
|
|
231
937
|
export {
|
|
938
|
+
AI_CRAWLERS,
|
|
232
939
|
DEFAULT_DESCRIPTION_LENGTH,
|
|
233
940
|
DEFAULT_TITLE_LENGTH,
|
|
941
|
+
SITEMAP_LIMIT,
|
|
942
|
+
docMarkdown,
|
|
943
|
+
indexNowKeyFile,
|
|
944
|
+
jsonLdScript,
|
|
945
|
+
llmsFullTxt,
|
|
946
|
+
llmsTxt,
|
|
947
|
+
robotsTxt,
|
|
234
948
|
seoMeta,
|
|
235
|
-
seoPlugin
|
|
949
|
+
seoPlugin,
|
|
950
|
+
siteJsonLd,
|
|
951
|
+
sitemap,
|
|
952
|
+
sitemapXml
|
|
236
953
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@easy-cms/plugin-seo",
|
|
3
|
-
"version": "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.
|
|
23
|
+
"@easy-cms/core": "^0.18.0"
|
|
21
24
|
},
|
|
22
25
|
"devDependencies": {
|
|
23
26
|
"happy-dom": "^20.14.5",
|
|
24
|
-
"@easy-cms/core": "^0.
|
|
25
|
-
"@easy-cms/db-sqlite": "^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"
|