leiho 1.0.0-beta.12 → 1.0.0-rc.1

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/README.md CHANGED
@@ -61,6 +61,17 @@ The standalone file starts itself from its `data-wp-origin` attribute.
61
61
 
62
62
  ## Annotate
63
63
 
64
+ In a JavaScript framework, let this package write the attributes: `field()`, `block()` and `entry()` return an object to spread on the element (JSX, Astro, Svelte; `v-bind` in Vue).
65
+
66
+ ```jsx
67
+ import { field, block } from 'leiho';
68
+
69
+ <h1 {...field(page, 'hero_title')}>{page.fields.hero_title}</h1>
70
+ <section {...block(page, 'sections', i, 'Hero')}>…</section>
71
+ ```
72
+
73
+ `field(page, 'intro', { html: true })` adds `data-leiho-apply="html"`; `{ refresh: true }` adds `data-leiho-refresh="save"`. They are pure functions: use them in server components and at build time. In plain HTML, write the attributes below by hand.
74
+
64
75
  ```html
65
76
  <main data-wp-post="12">
66
77
  <h1 data-wp-field="_title">About us</h1>
@@ -96,6 +107,28 @@ Content-Security-Policy: frame-ancestors 'self' https://your-wordpress.example
96
107
 
97
108
  and must not send `X-Frame-Options: DENY` or `SAMEORIGIN`.
98
109
 
110
+ ## Search engines and AI assistants
111
+
112
+ ```js
113
+ import { leihoGraph, jsonLd, seoMeta } from 'leiho/seo';
114
+ // One schema.org graph per page: site, organization, breadcrumbs, and the
115
+ // item's own nodes (item.seo.jsonld, computed by the plugin).
116
+ const graph = leihoGraph( { url, item, site: { url: 'https://example.com', name: 'Example' }, breadcrumbs } );
117
+ const tag = `<script type="application/ld+json">${ jsonLd( graph ) }</script>`;
118
+ ```
119
+
120
+ After a static build, every page as Markdown (`page.md`), `llms.txt`, `llms-full.txt` and `sitemap.xml` with `hreflang`:
121
+
122
+ ```js
123
+ import { aiReady } from 'leiho/ai-ready'; // Astro integration
124
+ import { buildAiFiles } from 'leiho/ai-ready'; // or call it yourself
125
+ await buildAiFiles( { dir: 'dist', site: 'https://example.com' } );
126
+ ```
127
+
128
+ ```bash
129
+ npx leiho ai-ready --site https://example.com --dir dist
130
+ ```
131
+
99
132
  ## Protocol
100
133
 
101
134
  Messages are `{ ns: 'leiho', type, … }`, sent with an explicit `targetOrigin` and checked against `event.origin`.
package/bin/leiho.js CHANGED
@@ -4,12 +4,14 @@
4
4
  *
5
5
  * npx leiho types [--url https://cms.example.com] [--out src/leiho.d.ts]
6
6
  * npx leiho pull [--url https://cms.example.com] [--out .leiho/content.json]
7
+ * npx leiho ai-ready --site https://example.com [--dir dist] [--locales en,fr]
7
8
  *
8
9
  * L'adresse du CMS et le jeton se lisent aussi dans LEIHO_URL et LEIHO_TOKEN.
9
10
  */
10
11
  import fs from 'node:fs/promises';
11
12
  import path from 'node:path';
12
13
  import { exportUrl, loadLeihoContent } from '../src/content.js';
14
+ import { buildAiFiles } from '../src/ai-ready.js';
13
15
 
14
16
  const [ command, ...rest ] = process.argv.slice( 2 );
15
17
  const args = {};
@@ -43,10 +45,22 @@ async function pull() {
43
45
  console.log( 'leiho: ' + data.items.length + ' items (' + source + ') in ' + out );
44
46
  }
45
47
 
46
- const commands = { types, pull };
48
+ // Après un build statique : pages en Markdown, llms.txt, llms-full.txt, sitemap.xml.
49
+ async function aiReady() {
50
+ await buildAiFiles( {
51
+ dir: args.dir || 'dist',
52
+ site: args.site || process.env.SITE_URL,
53
+ locales: args.locales ? args.locales.split( ',' ) : undefined,
54
+ exclude: args.exclude ? args.exclude.split( ',' ) : undefined,
55
+ sitemap: args.sitemap !== 'false',
56
+ logger: { info: ( message ) => console.log( 'leiho: ' + message ) },
57
+ } );
58
+ }
59
+
60
+ const commands = { types, pull, 'ai-ready': aiReady };
47
61
 
48
62
  if ( ! commands[ command ] ) {
49
- console.log( 'Usage: leiho <types|pull> [--url https://cms.example.com] [--out file] [--token …]' );
63
+ console.log( 'Usage: leiho <types|pull> [--url https://cms.example.com] [--out file] [--token …]\n leiho ai-ready --site https://example.com [--dir dist] [--locales en,fr] [--exclude /drafts] [--sitemap false]' );
50
64
  process.exit( command ? 1 : 0 );
51
65
  }
52
66
 
@@ -292,22 +292,68 @@
292
292
  * Application des valeurs
293
293
  * ------------------------------------------------------------------ */
294
294
 
295
+ // Liste blanche : seul ce qu'un texte riche contient passe. Une liste noire
296
+ // laisse toujours une porte (noscript, svg/math, schémas d'URL exotiques).
297
+ const ALLOWED_TAGS = new Set( [
298
+ 'A', 'ABBR', 'B', 'BLOCKQUOTE', 'BR', 'CAPTION', 'CITE', 'CODE', 'DD', 'DEL', 'DIV', 'DL', 'DT', 'EM', 'FIGCAPTION',
299
+ 'FIGURE', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'HR', 'I', 'IMG', 'INS', 'KBD', 'LI', 'MARK', 'OL', 'P', 'PRE', 'Q',
300
+ 'S', 'SMALL', 'SPAN', 'STRONG', 'SUB', 'SUP', 'TABLE', 'TBODY', 'TD', 'TFOOT', 'TH', 'THEAD', 'TR', 'U', 'UL',
301
+ ] );
302
+ // Hors liste blanche et retirées avec leur contenu (code, documents imbriqués).
303
+ const DROPPED_TAGS = new Set( [ ...BANNED_TAGS.map( ( t ) => t.toUpperCase() ), 'NOSCRIPT', 'NOEMBED', 'NOFRAMES', 'TEMPLATE', 'SVG', 'MATH', 'XMP', 'TEXTAREA', 'TITLE', 'SELECT', 'OBJECT' ] );
304
+ const ALLOWED_ATTRS = new Set( [ 'href', 'src', 'alt', 'title', 'class', 'width', 'height', 'target', 'rel', 'colspan', 'rowspan', 'lang', 'dir', 'start', 'reversed', 'loading', 'decoding', 'srcset', 'sizes', 'id', 'style' ] );
305
+
306
+ /** Adresse sûre : http(s), mailto, tel ou relative. */
307
+ function safeUrl( value ) {
308
+ const url = String( null == value ? '' : value ).replace( /[\u0000-\u0020\u007f-\u009f]/g, '' );
309
+ const scheme = url.match( /^([a-z][a-z0-9+.-]*):/i );
310
+ return ! scheme || /^(https?|mailto|tel)$/i.test( scheme[ 1 ] ) ? String( value ) : '';
311
+ }
312
+
295
313
  function sanitizeHtml( html ) {
296
- const doc = document.implementation.createHTMLDocument( '' );
297
- doc.body.innerHTML = String( null == html ? '' : html );
298
- doc.body.querySelectorAll( BANNED_TAGS.join( ',' ) ).forEach( ( el ) => el.remove() );
299
- doc.body.querySelectorAll( '*' ).forEach( ( el ) => {
300
- [ ...el.attributes ].forEach( ( attr ) => {
301
- const name = attr.name.toLowerCase();
302
- const value = attr.value.replace( /\s+/g, '' ).toLowerCase();
303
- if ( 0 === name.indexOf( 'on' ) ) {
304
- el.removeAttribute( attr.name );
305
- } else if ( ( 'href' === name || 'src' === name || 'xlink:href' === name ) && 0 === value.indexOf( 'javascript:' ) ) {
306
- el.removeAttribute( attr.name );
314
+ const source = new DOMParser().parseFromString( '<body>' + String( null == html ? '' : html ) + '</body>', 'text/html' ).body;
315
+ const clean = ( node, into ) => {
316
+ node.childNodes.forEach( ( child ) => {
317
+ if ( child.nodeType === Node.TEXT_NODE ) {
318
+ into.appendChild( document.createTextNode( child.nodeValue ) );
319
+ return;
320
+ }
321
+ if ( child.nodeType !== Node.ELEMENT_NODE ) {
322
+ return;
323
+ }
324
+ if ( ! ALLOWED_TAGS.has( child.tagName ) ) {
325
+ // Balise inconnue : son texte reste, sauf pour ce qui n'en a pas à montrer.
326
+ if ( ! DROPPED_TAGS.has( child.tagName.toUpperCase() ) ) {
327
+ clean( child, into );
328
+ }
329
+ return;
307
330
  }
331
+ const el = document.createElement( child.tagName );
332
+ [ ...child.attributes ].forEach( ( a ) => {
333
+ const name = a.name.toLowerCase();
334
+ if ( ! ALLOWED_ATTRS.has( name ) ) {
335
+ return;
336
+ }
337
+ if ( 'href' === name || 'src' === name ) {
338
+ const url = safeUrl( a.value );
339
+ if ( url ) {
340
+ el.setAttribute( name, url );
341
+ }
342
+ } else if ( 'srcset' === name ) {
343
+ if ( ! /(^|,)\s*[a-z][a-z0-9+.-]*:/i.test( a.value ) || a.value.split( ',' ).every( ( part ) => safeUrl( part.trim().split( /\s+/ )[ 0 ] ) ) ) {
344
+ el.setAttribute( name, a.value );
345
+ }
346
+ } else {
347
+ el.setAttribute( name, a.value );
348
+ }
349
+ } );
350
+ clean( child, el );
351
+ into.appendChild( el );
308
352
  } );
309
- } );
310
- return doc.body.innerHTML;
353
+ };
354
+ const out = document.createElement( 'div' );
355
+ clean( source, out );
356
+ return out.innerHTML;
311
357
  }
312
358
 
313
359
  /**
@@ -330,12 +376,13 @@
330
376
  if ( 'image' === fieldType ) {
331
377
  const img = 'IMG' === el.tagName ? el : el.querySelector( 'img' );
332
378
  if ( img ) {
333
- if ( img.getAttribute( 'src' ) !== ( value || '' ) ) {
334
- img.setAttribute( 'src', value || '' );
379
+ if ( img.getAttribute( 'src' ) !== safeUrl( value || '' ) ) {
380
+ img.setAttribute( 'src', safeUrl( value || '' ) );
335
381
  img.removeAttribute( 'srcset' );
336
382
  }
337
383
  } else {
338
- el.style.backgroundImage = value ? 'url("' + value + '")' : '';
384
+ const src = safeUrl( value || '' );
385
+ el.style.backgroundImage = src ? 'url("' + src.replace( /["\\\n]/g, '' ) + '")' : '';
339
386
  }
340
387
  } else if ( 'color_picker' === fieldType ) {
341
388
  el.style.setProperty( attr( el, 'style' ) || 'color', value || '' );
@@ -343,7 +390,7 @@
343
390
  const link = 'A' === el.tagName ? el : el.querySelector( 'a' );
344
391
  const data = value && 'object' === typeof value ? value : { url: value };
345
392
  if ( link ) {
346
- link.setAttribute( 'href', data.url || '' );
393
+ link.setAttribute( 'href', safeUrl( data.url || '' ) );
347
394
  if ( data.target ) {
348
395
  link.setAttribute( 'target', data.target );
349
396
  } else {
@@ -505,6 +552,9 @@
505
552
  } catch ( e ) {
506
553
  return;
507
554
  }
555
+ if ( ! /^https?:$/.test( url.protocol ) ) {
556
+ return;
557
+ }
508
558
  send( { type: 'navigate', href: url.href, path: url.pathname, external: url.origin !== window.location.origin } );
509
559
  return;
510
560
  }
@@ -544,7 +594,7 @@
544
594
  }
545
595
 
546
596
  window.addEventListener( 'message', ( event ) => {
547
- if ( event.origin !== wpOrigin || ! event.data || event.data.ns !== NS ) {
597
+ if ( event.origin !== wpOrigin || event.source !== window.parent || ! event.data || event.data.ns !== NS ) {
548
598
  return;
549
599
  }
550
600
  const data = event.data;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "leiho",
3
- "version": "1.0.0-beta.12",
3
+ "version": "1.0.0-rc.1",
4
4
  "description": "Pont postMessage de l'éditeur visuel Leiho : rend n'importe quel site (React, Next.js, Remix, Vite, Nuxt, SvelteKit, Astro, HTML) éditable depuis WordPress. Zéro dépendance, inactif hors de l'éditeur.",
5
5
  "keywords": [
6
6
  "wordpress",
@@ -13,7 +13,10 @@
13
13
  "remix",
14
14
  "vite",
15
15
  "nuxt",
16
- "sveltekit"
16
+ "sveltekit",
17
+ "llms.txt",
18
+ "json-ld",
19
+ "seo"
17
20
  ],
18
21
  "license": "GPL-2.0-or-later",
19
22
  "author": "Pixelers (https://pixelers.fr)",
@@ -31,6 +34,15 @@
31
34
  "./standalone": "./dist/leiho-bridge.js",
32
35
  "./content": {
33
36
  "import": "./src/content.js"
37
+ },
38
+ "./attrs": {
39
+ "import": "./src/attrs.js"
40
+ },
41
+ "./seo": {
42
+ "import": "./src/seo.js"
43
+ },
44
+ "./ai-ready": {
45
+ "import": "./src/ai-ready.js"
34
46
  }
35
47
  },
36
48
  "scripts": {