@easy-cms/plugin-seo 1.33.0 → 1.35.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 CHANGED
@@ -1,4 +1,4 @@
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=>({"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;"})[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=`
1
+ var h="/seo/generate";var E=["value","path","doc","collection","global","id","locale","uiLocale","readOnly","options","api"],y={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.',writeAi:"Write title and description with AI",writingAi:"Writing\u2026",spentAi:"Today's AI requests are used up",replaceAi:"Replace the title and description you have with AI's?",writtenAi:"Written by AI: check that they fit the page.",undo:"Undo"},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',writeAi:"\u0E40\u0E02\u0E35\u0E22\u0E19 title \u0E41\u0E25\u0E30 description \u0E14\u0E49\u0E27\u0E22 AI",writingAi:"\u0E01\u0E33\u0E25\u0E31\u0E07\u0E40\u0E02\u0E35\u0E22\u0E19\u2026",spentAi:"\u0E43\u0E0A\u0E49\u0E04\u0E33\u0E02\u0E2D AI \u0E02\u0E2D\u0E07\u0E27\u0E31\u0E19\u0E19\u0E35\u0E49\u0E04\u0E23\u0E1A\u0E41\u0E25\u0E49\u0E27",replaceAi:"\u0E43\u0E2B\u0E49 AI \u0E40\u0E02\u0E35\u0E22\u0E19 title \u0E41\u0E25\u0E30 description \u0E41\u0E17\u0E19\u0E17\u0E35\u0E48\u0E21\u0E35\u0E2D\u0E22\u0E39\u0E48\u0E44\u0E2B\u0E21?",writtenAi:"\u0E40\u0E02\u0E35\u0E22\u0E19\u0E42\u0E14\u0E22 AI: \u0E15\u0E23\u0E27\u0E08\u0E14\u0E39\u0E27\u0E48\u0E32\u0E15\u0E23\u0E07\u0E01\u0E31\u0E1A\u0E2B\u0E19\u0E49\u0E32",undo:"\u0E40\u0E25\u0E34\u0E01\u0E17\u0E33"}},o=i=>i.replace(/[&<>"']/g,t=>({"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;"})[t]);function $(i){if(typeof Intl.Segmenter!="function")return[...i].length;let t=0;for(let e of new Intl.Segmenter(void 0,{granularity:"grapheme"}).segment(i))t++;return t}function v(i,t){let e=[...i];return e.length>t?`${e.slice(0,t-1).join("").trimEnd()}\u2026`:i}var T=`
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"?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`
16
+ `,w=new WeakMap;function k(i,t=!1){let e=w.get(i);if(e&&!t)return e;let n=i("GET","/admin/ai/status").then(s=>{let a=s;return{ready:a.connected&&a.features.includes("seo"),spent:a.used>=a.limit}}).catch(()=>({ready:!1,spent:!1}));return w.set(i,n),n}var p=class i 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",e=>{let n=e.target.closest("[data-action]")?.getAttribute("data-action");n&&this.onAction(n)})}static{for(let t of E)Object.defineProperty(i.prototype,t,{get(){return this.ctx[t]},set(e){this.ctx[t]=e,this.update()}})}connectedCallback(){this.update()}t(t,e={}){return(this.ctx.uiLocale==="th"?y.th:y.en)[t].replace(/\{(\w+)\}/g,(s,a)=>String(e[a]??""))}update(){this.queued||(this.queued=!0,queueMicrotask(()=>{this.queued=!1;let t=this.shadowRoot;if(!t)return;let e=`<style>${T}${this.css()}</style>${this.render()}`;if(e===this.html)return;let n=t.activeElement?.getAttribute("data-action");this.html=e,t.innerHTML=e,n&&t.querySelector(`[data-action="${n}"]`)?.focus()}))}async generate(t){let{api:e,collection:n,global:s,id:a,doc:r,locale:c}=this.ctx;return e?(await e("POST",h,{kind:t,...n?{collection:n}:{},...s?{global:s}:{},id:a,doc:r,locale:c})).value??null:null}async generateInto(t,e){if(!(!e||this.busy)){this.busy=!0,this.notice=null,this.update();try{let n=await this.generate(t);n===null||n===""?this.notice={text:this.t("noValue"),error:!1}:this.dispatchEvent(new CustomEvent("set-field",{detail:{path:e,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:t,error:e}=this.notice;return e?`<p class="error" role="alert">${o(t)}</p>`:`<p class="hint" role="status">${o(t)}</p>`}onAction(t){}css(){return""}},g=class extends p{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); }
@@ -22,20 +22,20 @@ var f="/seo/generate";var v=["value","path","doc","collection","global","id","lo
22
22
  .long .fill { background: var(--danger); }
23
23
  .long .state { color: var(--danger); }
24
24
  .good .state { color: var(--ok); }
25
- `}render(){let{kind:e,min:t,max:n,generate:o}=this.ctx.options,c=typeof this.ctx.value=="string"?this.ctx.value:"",s=y(c.trim()),a=s===0?"empty":s<t?"short":s>n?"long":"good",l=Math.min(100,Math.round(s/n*100)),u=o&&!this.ctx.readOnly?`<button type="button" data-action="generate" ${this.busy?"disabled":""}>${i(this.t(this.busy?"generating":"generate"))}</button>`:"";return`
26
- <div class="row ${a}" data-kind="${i(e)}" data-state="${a}">
25
+ `}render(){let{kind:t,min:e,max:n,generate:s}=this.ctx.options,a=typeof this.ctx.value=="string"?this.ctx.value:"",r=$(a.trim()),c=r===0?"empty":r<e?"short":r>n?"long":"good",l=Math.min(100,Math.round(r/n*100)),u=s&&!this.ctx.readOnly?`<button type="button" data-action="generate" ${this.busy?"disabled":""}>${o(this.t(this.busy?"generating":"generate"))}</button>`:"";return`
26
+ <div class="row ${c}" data-kind="${o(t)}" data-state="${c}">
27
27
  <div class="bar" aria-hidden="true"><div class="fill" style="width:${l}%"></div></div>
28
- <span class="hint">${i(this.t("characters",{n:s,max:n}))}</span>
29
- <span class="hint state">${i(this.t(a,{min:t,max:n}))}</span>
28
+ <span class="hint">${o(this.t("characters",{n:r,max:n}))}</span>
29
+ <span class="hint state">${o(this.t(c,{min:e,max:n}))}</span>
30
30
  ${u}
31
31
  </div>
32
32
  ${this.renderNotice()}
33
- `}onAction(e){if(e==="generate"){let{kind:t}=this.ctx.options;return this.generateInto(t,this.ctx.path)}}},g=class extends d{render(){let{generate:e}=this.ctx.options;return!e||this.ctx.readOnly?"":`
33
+ `}onAction(t){if(t==="generate"){let{kind:e}=this.ctx.options;return this.generateInto(e,this.ctx.path)}}},f=class extends p{render(){let{generate:t}=this.ctx.options;return!t||this.ctx.readOnly?"":`
34
34
  <div class="row">
35
- <button type="button" data-action="generate" ${this.busy?"disabled":""}>${i(this.t(this.busy?"generating":"generateImage"))}</button>
35
+ <button type="button" data-action="generate" ${this.busy?"disabled":""}>${o(this.t(this.busy?"generating":"generateImage"))}</button>
36
36
  </div>
37
37
  ${this.renderNotice()}
38
- `}onAction(e){if(e==="generate")return this.generateInto("image",this.ctx.path)}},h=class extends d{url="";urlFor="";timer;css(){return`
38
+ `}onAction(t){if(t==="generate")return this.generateInto("image",this.ctx.path)}},m=class extends p{url="";urlFor="";timer;ai={ready:!1,spent:!1};aiAsked=!1;beforeAi=null;css(){return`
39
39
  .card { padding: 0.9rem 1rem; border: 1px solid var(--border); border-radius: var(--radius-sm, 8px); background: var(--surface); }
40
40
  .label { margin: 0 0 0.5rem; font-size: 0.75rem; font-weight: 600; letter-spacing: 0.02em; text-transform: uppercase; color: var(--text-muted); }
41
41
  .url { margin: 0; font-size: 0.8rem; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@@ -43,12 +43,23 @@ var f="/seo/generate";var v=["value","path","doc","collection","global","id","lo
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
45
  .noindex { margin: 0.6rem 0 0; font-size: 0.8rem; color: var(--warning, var(--text-muted)); }
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`
47
- <section class="card" aria-label="${i(this.t("preview"))}">
48
- <p class="label">${i(this.t("preview"))}</p>
49
- ${l?`<p class="url">${i(l)}</p>`:""}
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>`:""}
46
+ .ai { margin-top: 0.6rem; }
47
+ .ai button { color: var(--accent-ink); font-weight: 600; }
48
+ .written { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem; color: var(--accent-ink); }
49
+ `}render(){let{titleField:t,url:e,siteURL:n}=this.ctx.options,s=A(this.ctx.doc,this.ctx.path)??{},a=t?this.ctx.doc[t]:void 0,r=[s.title,a].find(d=>typeof d=="string"&&d.trim()),c=typeof s.description=="string"?s.description.trim():"";e&&this.refreshUrl(),!this.aiAsked&&this.ctx.api&&(this.aiAsked=!0,k(this.ctx.api).then(d=>{this.ai=d,this.update()}));let l=P(e?this.url:n),u=this.ai.ready&&!this.ctx.readOnly?`<div class="row ai">
50
+ <button type="button" data-action="ai" ${this.busy||this.ai.spent?"disabled":""}>
51
+ <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 3l1.9 5.1L19 10l-5.1 1.9L12 17l-1.9-5.1L5 10l5.1-1.9z"/></svg>
52
+ ${o(this.t(this.busy?"writingAi":this.ai.spent?"spentAi":"writeAi"))}
53
+ </button>
54
+ </div>
55
+ ${this.beforeAi?`<p class="hint written" role="status">${o(this.t("writtenAi"))} <button type="button" data-action="undo-ai">${o(this.t("undo"))}</button></p>`:""}
56
+ ${this.renderNotice()}`:"";return`
57
+ <section class="card" aria-label="${o(this.t("preview"))}">
58
+ <p class="label">${o(this.t("preview"))}</p>
59
+ ${l?`<p class="url">${o(l)}</p>`:""}
60
+ <p class="title ${r?"":"placeholder"}">${o(v(r??this.t("noTitle"),60))}</p>
61
+ <p class="description ${c?"":"placeholder"}">${o(c?v(c,160):this.t("noDescription"))}</p>
62
+ ${s.noindex===!0?`<p class="noindex">${o(this.t("hidden"))}</p>`:""}
53
63
  </section>
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);
64
+ ${u}
65
+ `}async onAction(t){let e=this.ctx.path;if(!e||!this.ctx.api)return;let n=(r,c)=>this.dispatchEvent(new CustomEvent("set-field",{detail:{path:`${e}.${r}`,value:c}}));if(t==="undo-ai"&&this.beforeAi){n("title",this.beforeAi.title),n("description",this.beforeAi.description),this.beforeAi=null,this.update();return}if(t!=="ai"||this.busy)return;let s=A(this.ctx.doc,e)??{},a=r=>typeof r=="string"&&r.trim()!=="";if(!((a(s.title)||a(s.description))&&!window.confirm(this.t("replaceAi")))){this.busy=!0,this.notice=null,this.update();try{let{collection:r,global:c,id:l,doc:u,locale:d}=this.ctx,x=await this.ctx.api("POST",h,{kind:"ai",...r?{collection:r}:{},...c?{global:c}:{},id:l,doc:u,locale:d});this.beforeAi??={title:s.title??null,description:s.description??null},n("title",x.value.title),n("description",x.value.description)}catch(r){this.notice={text:this.t("failed",{message:r.message}),error:!0}}finally{this.busy=!1,this.ai=await k(this.ctx.api,!0),this.update()}}}refreshUrl(){let t=JSON.stringify([this.ctx.doc,this.ctx.locale]);t!==this.urlFor&&(this.urlFor=t,clearTimeout(this.timer),this.timer=setTimeout(async()=>{try{let e=await this.generate("url"),n=typeof e=="string"?e:"";n!==this.url&&(this.url=n,this.update())}catch{}},400))}};function A(i,t){if(t)return t.split(".").reduce((e,n)=>typeof e=="object"&&e!==null?e[n]:void 0,i)}function P(i){if(!i)return"";try{let{host:t,pathname:e}=new URL(i,"https://site.invalid"),n=e.split("/").filter(Boolean).map(decodeURIComponent);return[t==="site.invalid"?"":t,...n].filter(Boolean).join(" \u203A ")}catch{return i}}var b=(i,t)=>{customElements.get(i)||customElements.define(i,t)};b("ecms-seo-meter",g);b("ecms-seo-image",f);b("ecms-seo-preview",m);
package/dist/index.d.ts CHANGED
@@ -409,6 +409,17 @@ interface SeoPluginOptions {
409
409
  readonly generateURL?: Generate;
410
410
  /** Fill empty meta fields with the generators when a document is saved. Default false. */
411
411
  readonly autoGenerate?: boolean;
412
+ /**
413
+ * With `@easy-cms/plugin-ai` (#358): the text "Write with AI" sends to write the meta title and
414
+ * description. Default: the title, then the text of the top-level text, textarea and rich text
415
+ * fields, cut at about 6,000 characters.
416
+ */
417
+ readonly aiSource?: (args: {
418
+ doc: Record<string, unknown>;
419
+ collection?: string;
420
+ global?: string;
421
+ locale: string | null;
422
+ }) => string | Promise<string>;
412
423
  /** Changes the fields of the group, e.g. `(defaults) => [...defaults, noindexField]`. */
413
424
  readonly fields?: (defaults: Field[]) => Field[];
414
425
  /** Length the admin marks as good for titles. Default 50–60 characters. */
package/dist/index.js CHANGED
@@ -524,7 +524,7 @@ var INFO = {
524
524
  name: "@easy-cms/plugin-seo",
525
525
  // The plugin API it is written for (`PLUGIN_API_VERSION` of @easy-cms/core).
526
526
  apiVersion: 1,
527
- ...true ? { version: "1.33.0" } : {}
527
+ ...true ? { version: "1.35.0" } : {}
528
528
  };
529
529
 
530
530
  // src/robots.ts
@@ -850,12 +850,69 @@ function seoPlugin(options = {}) {
850
850
  ...options.position !== "sidebar" && options.tab !== false ? { admin: { ...container.admin, layout: withSeoTab(container.admin?.layout) } } : {}
851
851
  };
852
852
  };
853
+ const writeWithAi = async (body, user, cms) => {
854
+ const ai = cms.ai;
855
+ if (!ai) return error(404, "No AI plugin");
856
+ const collection = typeof body.collection === "string" ? body.collection : void 0;
857
+ const global = typeof body.global === "string" ? body.global : void 0;
858
+ const container = collection ? collections.includes(collection) && cms.config.collections.find((c) => c.slug === collection) : global && globals.includes(global) && cms.config.globals.find((g) => g.slug === global);
859
+ if (!container) return error(404, "No SEO fields on this collection or global");
860
+ const id = typeof body.id === "string" || typeof body.id === "number" ? body.id : null;
861
+ const allowed = collection ? id !== null ? (await cms.documentPermissions(collection, id, user)).update : await cms.roles.allows(user, { collection }, "create") !== false : await cms.roles.allows(user, { global }, "update") !== false;
862
+ if (!allowed) return error(403, "You may not change this document");
863
+ const doc = typeof body.doc === "object" && body.doc !== null ? body.doc : {};
864
+ const locale = typeof body.locale === "string" ? body.locale : null;
865
+ const titleField = "useAsTitle" in container ? container.useAsTitle ?? null : null;
866
+ const source2 = options.aiSource ? await options.aiSource({
867
+ doc,
868
+ locale,
869
+ ...collection ? { collection } : { global }
870
+ }) : contentText(container.fields, doc, titleField);
871
+ if (!source2.trim()) return error(400, "Nothing to write from yet: add some content first");
872
+ const title = options.titleLength ?? DEFAULT_TITLE_LENGTH;
873
+ const description = options.descriptionLength ?? DEFAULT_DESCRIPTION_LENGTH;
874
+ try {
875
+ const result = await ai.generate(
876
+ {
877
+ feature: "seo",
878
+ system: [
879
+ "You write the title and description search engines show for a web page.",
880
+ `The title: ${title.min} to ${title.max} characters, what the page is about, no site name.`,
881
+ `The description: ${description.min} to ${description.max} characters, one or two sentences`,
882
+ "that make a reader want to open the page. Plain text, no quotes, no emoji.",
883
+ `Write both in ${ai.languageOf(locale)}.`
884
+ ].join(" "),
885
+ prompt: `The page:
886
+
887
+ ${source2.slice(0, 6e3)}`,
888
+ json: {
889
+ type: "object",
890
+ properties: { title: { type: "string" }, description: { type: "string" } },
891
+ required: ["title", "description"]
892
+ },
893
+ maxTokens: 400
894
+ },
895
+ { user, ...collection && id !== null ? { about: { collection, id } } : {} }
896
+ );
897
+ const value = result.json ?? {};
898
+ return {
899
+ value: {
900
+ title: typeof value.title === "string" ? value.title.trim() : "",
901
+ description: typeof value.description === "string" ? value.description.trim() : ""
902
+ }
903
+ };
904
+ } catch (e) {
905
+ const status = e.status;
906
+ return error(typeof status === "number" ? status : 400, e.message);
907
+ }
908
+ };
853
909
  const endpoint = {
854
910
  path: GENERATE_PATH,
855
911
  method: "post",
856
912
  handler: async ({ json, user, cms }) => {
857
913
  if (!user) return error(401, "Log in to generate SEO values");
858
914
  const body = await json();
915
+ if (body.kind === "ai") return writeWithAi(body, user, cms);
859
916
  const kind = body.kind;
860
917
  if (!GENERATE_KINDS.includes(kind))
861
918
  return error(400, `kind must be one of ${GENERATE_KINDS.join(", ")}`);
@@ -985,12 +1042,51 @@ function seoPlugin(options = {}) {
985
1042
  ...config.admin,
986
1043
  modules: [.../* @__PURE__ */ new Set([...config.admin?.modules ?? [], ADMIN_MODULE])]
987
1044
  },
988
- endpoints: [...config.endpoints ?? [], endpoint, ...siteEndpoints]
1045
+ endpoints: [...config.endpoints ?? [], endpoint, ...siteEndpoints],
1046
+ // Used only with an AI plugin (`ai` in the config): "Write with AI" on the SEO fields.
1047
+ aiFeatures: [
1048
+ ...config.aiFeatures ?? [],
1049
+ {
1050
+ id: "seo",
1051
+ label: { en: "SEO title and description", th: "title \u0E41\u0E25\u0E30 description \u0E2A\u0E33\u0E2B\u0E23\u0E31\u0E1A SEO" },
1052
+ description: {
1053
+ en: "Writes the search result text from the content",
1054
+ th: "\u0E40\u0E02\u0E35\u0E22\u0E19\u0E02\u0E49\u0E2D\u0E04\u0E27\u0E32\u0E21\u0E43\u0E19\u0E1C\u0E25\u0E04\u0E49\u0E19\u0E2B\u0E32\u0E08\u0E32\u0E01\u0E40\u0E19\u0E37\u0E49\u0E2D\u0E2B\u0E32"
1055
+ }
1056
+ }
1057
+ ]
989
1058
  };
990
1059
  };
991
1060
  return Object.assign(plugin, { info: INFO });
992
1061
  }
993
1062
  var isEmpty = (value) => value === null || value === void 0 || value === "";
1063
+ function richText(value) {
1064
+ const out = [];
1065
+ const walk = (node) => {
1066
+ if (!node || typeof node !== "object") return;
1067
+ const n = node;
1068
+ if (typeof n.text === "string") out.push(n.text);
1069
+ if (Array.isArray(n.content)) {
1070
+ for (const child of n.content) walk(child);
1071
+ if (n.type !== "text" && n.type !== "doc") out.push("\n");
1072
+ }
1073
+ };
1074
+ walk(value);
1075
+ return out.join("").replace(/\n{3,}/g, "\n\n").trim();
1076
+ }
1077
+ function contentText(fields, doc, titleField) {
1078
+ const parts = [];
1079
+ const title = titleField ? doc[titleField] : void 0;
1080
+ if (typeof title === "string" && title.trim()) parts.push(title.trim());
1081
+ for (const field of fields) {
1082
+ if (!("name" in field) || field.name === titleField || field.name === "meta") continue;
1083
+ const value = doc[field.name];
1084
+ if ((field.type === "text" || field.type === "textarea") && typeof value === "string")
1085
+ parts.push(value.trim());
1086
+ else if (field.type === "richText") parts.push(richText(value));
1087
+ }
1088
+ return parts.filter(Boolean).join("\n\n");
1089
+ }
994
1090
  function error(status, message) {
995
1091
  return Response.json({ errors: [{ message }] }, { status });
996
1092
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easy-cms/plugin-seo",
3
- "version": "1.33.0",
3
+ "version": "1.35.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",
@@ -17,15 +17,15 @@
17
17
  "dist"
18
18
  ],
19
19
  "dependencies": {
20
- "@easy-cms/richtext": "^1.33.0"
20
+ "@easy-cms/richtext": "^1.35.0"
21
21
  },
22
22
  "peerDependencies": {
23
- "@easy-cms/core": "^1.33.0"
23
+ "@easy-cms/core": "^1.35.0"
24
24
  },
25
25
  "devDependencies": {
26
26
  "happy-dom": "^21.0.4",
27
- "@easy-cms/core": "^1.33.0",
28
- "@easy-cms/db-sqlite": "^1.33.0"
27
+ "@easy-cms/core": "^1.35.0",
28
+ "@easy-cms/db-sqlite": "^1.35.0"
29
29
  },
30
30
  "engines": {
31
31
  "node": ">=22.12"