@useavalon/seo 0.6.0 → 0.7.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
@@ -59,7 +59,7 @@ export default defineConfig(async () => {
59
59
 
60
60
  ## What It Does
61
61
 
62
- The plugin intercepts every HTML response and auto-injects missing SEO tags. It reads existing tags to avoid duplication — if your layout already sets `og:title`, the plugin won't add another one.
62
+ The plugin intercepts every HTML response and auto-injects missing SEO tags. In development it patches the Vite dev server; in production it registers a Nitro HTML post-process hook (and you can re-apply tags to prerendered files after `runPostBuild` with `applySeoToDist` from `@useavalon/seo/dist-transform`). `transformSeoHtml` is idempotent: HTML that already contains the `<!-- avalon-seo -->` marker is left unchanged, so post-build passes are safe to run more than once. It reads existing tags to avoid duplication — if your layout already sets `og:title`, the plugin won't add another one.
63
63
 
64
64
  ### Meta Tags
65
65
 
package/dist/mod.js CHANGED
@@ -1 +1 @@
1
- export{OrganizationConfigSchema,PersonConfigSchema,SeoConfigSchema,validateSeoConfig}from"./src/config.js";export{extractArticleMetaFromHtml,extractMetadataFromHtml,extractMetaProperty,injectMetaTags}from"./src/meta-tags.js";export{seo}from"./src/plugin.js";export{buildArticleJsonLd,buildBreadcrumbListJsonLd,buildWebPageJsonLd,buildWebSiteJsonLd,injectJsonLd}from"./src/structured-data.js";
1
+ export{OrganizationConfigSchema,PersonConfigSchema,SeoConfigSchema,validateSeoConfig}from"./src/config.js";export{extractArticleMetaFromHtml,extractMetadataFromHtml,extractMetaProperty,injectMetaTags}from"./src/meta-tags.js";export{seo}from"./src/plugin.js";export{resolveSiteUrl,withEnvSiteUrl}from"./src/site-config.js";export{buildArticleJsonLd,buildBreadcrumbListJsonLd,buildWebPageJsonLd,buildWebSiteJsonLd,injectJsonLd}from"./src/structured-data.js";export{transformSeoHtml}from"./src/transform.js";
@@ -1,2 +1,2 @@
1
- import{z as e}from"zod";export const PersonConfigSchema=e.object({name:e.string(),url:e.string().optional(),image:e.string().optional(),jobTitle:e.string().optional(),worksFor:e.object({name:e.string(),url:e.string().optional()}).optional(),description:e.string().optional(),sameAs:e.array(e.string()).optional()});export const OrganizationConfigSchema=e.object({name:e.string(),url:e.string().optional(),logo:e.object({url:e.string(),width:e.number().optional(),height:e.number().optional()}).optional()});export const SeoConfigSchema=e.object({siteUrl:e.string(),siteName:e.string(),defaultDescription:e.string().optional(),defaultOgImage:e.object({url:e.string(),width:e.number().optional().default(1200),height:e.number().optional().default(630)}).optional(),twitterSite:e.string().optional(),twitterCreator:e.string().optional(),locale:e.string().optional().default(`en_US`),author:PersonConfigSchema.optional(),publisher:OrganizationConfigSchema.optional(),searchPath:e.string().optional(),searchQueryParam:e.string().optional().default(`q`),canonical:e.boolean().optional().default(!0),openGraph:e.boolean().optional().default(!0),twitterCards:e.boolean().optional().default(!0),structuredData:e.boolean().optional().default(!0),breadcrumbs:e.boolean().optional().default(!0),speakable:e.boolean().optional().default(!0),speakableSelectors:e.array(e.string()).optional().default([`h1`,`[data-speakable]`]),fontPreconnect:e.array(e.string()).optional(),titleSuffix:e.union([e.string(),e.literal(!1)]).optional()});export function validateSeoConfig(e){let t=SeoConfigSchema.safeParse(e);if(t.success)return t.data;let n=t.error.issues.map(e=>` - ${e.path.join(`.`)}: ${e.message}`);throw Error(`Invalid @useavalon/seo config:\n${n.join(`
1
+ import{z as e}from"zod";export const PersonConfigSchema=e.object({name:e.string(),url:e.string().optional(),image:e.string().optional(),jobTitle:e.string().optional(),worksFor:e.object({name:e.string(),url:e.string().optional()}).optional(),description:e.string().optional(),sameAs:e.array(e.string()).optional()});export const OrganizationConfigSchema=e.object({name:e.string(),url:e.string().optional(),sameAs:e.array(e.string()).optional(),logo:e.object({url:e.string(),width:e.number().optional(),height:e.number().optional()}).optional()});export const FaqItemSchema=e.object({question:e.string(),answer:e.string()});export const SeoConfigSchema=e.object({siteUrl:e.string(),siteName:e.string(),defaultDescription:e.string().optional(),defaultOgImage:e.object({url:e.string(),width:e.number().optional().default(1200),height:e.number().optional().default(630)}).optional(),twitterSite:e.string().optional(),twitterCreator:e.string().optional(),locale:e.string().optional().default(`en_US`),hreflang:e.boolean().optional().default(!1),themeColor:e.string().optional(),appleTouchIcon:e.string().optional(),webFontStylesheet:e.string().optional(),contentModified:e.string().optional(),author:PersonConfigSchema.optional(),publisher:OrganizationConfigSchema.optional(),searchPath:e.string().optional(),searchQueryParam:e.string().optional().default(`q`),canonical:e.boolean().optional().default(!0),openGraph:e.boolean().optional().default(!0),twitterCards:e.boolean().optional().default(!0),structuredData:e.boolean().optional().default(!0),breadcrumbs:e.boolean().optional().default(!0),speakable:e.boolean().optional().default(!0),speakableSelectors:e.array(e.string()).optional().default([`h1`,`[data-speakable]`]),fontPreconnect:e.array(e.string()).optional(),dnsPrefetch:e.array(e.string()).optional(),robotsImagePreview:e.boolean().optional().default(!0),faq:e.array(FaqItemSchema).optional(),webAppManifest:e.string().optional(),feedUrl:e.string().optional(),titleSuffix:e.union([e.string(),e.literal(!1)]).optional()});export function validateSeoConfig(e){let t=SeoConfigSchema.safeParse(e);if(t.success)return t.data;let n=t.error.issues.map(e=>` - ${e.path.join(`.`)}: ${e.message}`);throw Error(`Invalid @useavalon/seo config:\n${n.join(`
2
2
  `)}`)}
@@ -0,0 +1 @@
1
+ export const SEO_ERROR_PAGE_PATH=`\0seo-error-page`;export const SEO_TRANSFORM_MARKER=`<!-- avalon-seo -->`;
@@ -0,0 +1 @@
1
+ import{transformSeoHtml as e}from"./transform.js";function t(e,t,n){if(t){if(Buffer.isBuffer(t))e.push(t);else if(t instanceof Uint8Array)e.push(Buffer.from(t));else if(typeof t==`string`){let r=typeof n==`string`?n:`utf-8`;e.push(Buffer.from(t,r))}}}export function patchResponseForSeo(n,r,i){let a=n.end.bind(n),o=n.write.bind(n),s=n.setHeader.bind(n),c=n.writeHead.bind(n),l=[],u=!1,d=()=>{u=!0};n.setHeader=((e,t)=>(e.toLowerCase()===`content-type`&&typeof t==`string`&&t.includes(`text/html`)&&d(),s(e,t))),n.writeHead=((e,...t)=>{for(let e of t)if(e&&typeof e==`object`&&!Array.isArray(e))for(let[t,n]of Object.entries(e))t.toLowerCase()===`content-type`&&typeof n==`string`&&n.includes(`text/html`)&&d();return c(e,...t)}),n.write=((e,n,r)=>u?(t(l,e,n),typeof n==`function`?n():typeof r==`function`&&r(),!0):o(e,n,r)),n.end=((o,c,f)=>{if(!u){let e=n.getHeader(`content-type`);typeof e==`string`&&e.includes(`text/html`)&&d()}if(!u||(t(l,o,c),l.length===0))return a(o,c,f);let p=Buffer.concat(l).toString(`utf-8`),m=typeof c==`function`?c:f,h=e(p,r,i);return h!==p&&s(`Content-Length`,String(Buffer.byteLength(h))),a(h,`utf-8`,m)})}
@@ -0,0 +1 @@
1
+ import{readdirSync as e,readFileSync as t,statSync as n,writeFileSync as r}from"node:fs";import{join as i,relative as a}from"node:path";import{SEO_ERROR_PAGE_PATH as o,SEO_TRANSFORM_MARKER as s}from"./constants.js";import{transformSeoHtml as c}from"./transform.js";const l=[`http://localhost:8012`,`http://localhost:3000`];export function stripSeoTransformMarker(e){return e.replaceAll(`${s}\n`,``).replaceAll(s,``)}export function rewriteDevSiteUrls(e,t){let n=t.replace(/\/+$/,``),r=e;for(let e of l)e!==n&&r.includes(e)&&(r=r.replaceAll(e,n));return r}function u(t,r=[]){if(!n(t,{throwIfNoEntry:!1})?.isDirectory())return r;for(let a of e(t)){let e=i(t,a);n(e).isDirectory()?u(e,r):a.endsWith(`.html`)&&r.push(e)}return r}export function htmlFileToPathname(e,t){let n=a(e,t).replaceAll(`\\`,`/`);if(n===`404.html`||n.endsWith(`/404.html`))return o;if(n===`index.html`)return`/`;if(n.endsWith(`/index.html`)){let e=n.slice(0,-11);return e?`/${e}/`:`/`}return`/${n.replace(/\.html$/i,``)}/`}function d(e){return e.replaceAll(/<script\s+type=["']application\/ld\+json["'][^>]*>[\s\S]*?<\/script>/gi,``)}export function needsSeoDistRefresh(e,t){if(!e.includes(s)||t.hreflang&&!e.includes(`hreflang=`)||t.themeColor&&!e.includes(`name="theme-color"`)||t.appleTouchIcon&&!e.includes(`apple-touch-icon`)||t.webFontStylesheet&&!e.includes(t.webFontStylesheet)||t.contentModified&&!e.includes(`"dateModified"`)||t.publisher?.sameAs?.length&&!e.includes(`"sameAs"`))return!0;let n=t.siteUrl.replace(/\/+$/,``);for(let t of l)if(t!==n&&e.includes(t))return!0;return!1}export function applySeoToDist(e,n){let i=0;for(let a of u(e)){let o=htmlFileToPathname(e,a),s=t(a,`utf-8`),l;if(needsSeoDistRefresh(s,n)){let e=d(stripSeoTransformMarker(s));l=rewriteDevSiteUrls(c(e,o,n),n.siteUrl)}else l=rewriteDevSiteUrls(s,n.siteUrl);l!==s&&(r(a,l,`utf-8`),i++)}return i}
@@ -1,3 +1,3 @@
1
- export function injectMetaTags(t,n,i){let a=e(t),o=[],s=i.siteUrl.replace(/\/+$/,``),c=n.startsWith(`http`)?n:`${s}${n}`;if(i.canonical&&!a.canonical&&o.push(`<link rel="canonical" href="${r(c)}" />`),i.openGraph){let e=a.ogTitle||a.title,t=a.ogDescription||a.description||i.defaultDescription,n=a.ogImage||i.defaultOgImage?.url;if(!a.ogTitle&&e&&o.push(`<meta property="og:title" content="${r(e)}" />`),!a.ogDescription&&t&&o.push(`<meta property="og:description" content="${r(t)}" />`),a.ogUrl||o.push(`<meta property="og:url" content="${r(c)}" />`),!a.ogType){let e=a.articlePublishedTime?`article`:`website`;o.push(`<meta property="og:type" content="${e}" />`)}if(!a.ogLocale&&i.locale&&o.push(`<meta property="og:locale" content="${r(i.locale)}" />`),!a.ogSiteName&&i.siteName&&o.push(`<meta property="og:site_name" content="${r(i.siteName)}" />`),!a.ogImage&&n){let e=n.startsWith(`http`)?n:`${s}${n}`;o.push(`<meta property="og:image" content="${r(e)}" />`)}if(a.ogImage||n){if(!a.ogImageWidth){let e=i.defaultOgImage?.width||1200;o.push(`<meta property="og:image:width" content="${e}" />`)}if(!a.ogImageHeight){let e=i.defaultOgImage?.height||630;o.push(`<meta property="og:image:height" content="${e}" />`)}}}if(i.twitterCards){let e=a.twitterTitle||a.ogTitle||a.title,t=a.twitterDescription||a.ogDescription||a.description||i.defaultDescription,n=a.twitterImage||a.ogImage||i.defaultOgImage?.url;if(!a.twitterCard){let e=n?`summary_large_image`:`summary`;o.push(`<meta name="twitter:card" content="${e}" />`)}if(!a.twitterSite&&i.twitterSite&&o.push(`<meta name="twitter:site" content="${r(i.twitterSite)}" />`),!a.twitterCreator&&i.twitterCreator&&o.push(`<meta name="twitter:creator" content="${r(i.twitterCreator)}" />`),!a.twitterTitle&&e&&o.push(`<meta name="twitter:title" content="${r(e)}" />`),!a.twitterDescription&&t&&o.push(`<meta name="twitter:description" content="${r(t)}" />`),!a.twitterImage&&n){let e=n.startsWith(`http`)?n:`${s}${n}`;o.push(`<meta name="twitter:image" content="${r(e)}" />`)}}if(i.fontPreconnect?.length)for(let e of i.fontPreconnect)t.includes(e)||(o.push(`<link rel="preconnect" href="${r(e)}" />`),(e.includes(`fonts.gstatic`)||e.includes(`cdn.fonts`))&&o.push(`<link rel="preconnect" href="${r(e)}" crossorigin />`));if(o.length===0)return t;let l=t.toLowerCase().indexOf(`</head>`);return l===-1?t:t.slice(0,l)+o.join(`
1
+ import{SEO_ERROR_PAGE_PATH as e}from"./constants.js";export function injectMetaTags(n,r,i){let a=i.siteUrl.replace(/\/+$/,``),o=r===e?`/`:r,u=r.startsWith(`http`)?r:`${a}${o}`,d=r===e;i.canonical&&!d&&(n=s(n,u,a)),i.openGraph&&(n=c(n,`og:url`,u,a));let f=t(n),p=[];if(i.canonical&&!f.canonical&&!d&&p.push(`<link rel="canonical" href="${l(u)}" />`),i.openGraph){let e=f.ogTitle||f.title,t=f.ogDescription||f.description||i.defaultDescription,n=f.ogImage||i.defaultOgImage?.url;if(!f.ogTitle&&e&&p.push(`<meta property="og:title" content="${l(e)}" />`),!f.ogDescription&&t&&p.push(`<meta property="og:description" content="${l(t)}" />`),f.ogUrl||p.push(`<meta property="og:url" content="${l(u)}" />`),!f.ogType){let e=f.articlePublishedTime?`article`:`website`;p.push(`<meta property="og:type" content="${e}" />`)}if(!f.ogLocale&&i.locale&&p.push(`<meta property="og:locale" content="${l(i.locale)}" />`),!f.ogSiteName&&i.siteName&&p.push(`<meta property="og:site_name" content="${l(i.siteName)}" />`),!f.ogImage&&n){let e=n.startsWith(`http`)?n:`${a}${n}`;p.push(`<meta property="og:image" content="${l(e)}" />`)}if(f.ogImage||n){if(!f.ogImageWidth){let e=i.defaultOgImage?.width||1200;p.push(`<meta property="og:image:width" content="${e}" />`)}if(!f.ogImageHeight){let e=i.defaultOgImage?.height||630;p.push(`<meta property="og:image:height" content="${e}" />`)}}}if(i.twitterCards){let e=f.twitterTitle||f.ogTitle||f.title,t=f.twitterDescription||f.ogDescription||f.description||i.defaultDescription,n=f.twitterImage||f.ogImage||i.defaultOgImage?.url;if(!f.twitterCard){let e=n?`summary_large_image`:`summary`;p.push(`<meta name="twitter:card" content="${e}" />`)}if(!f.twitterSite&&i.twitterSite&&p.push(`<meta name="twitter:site" content="${l(i.twitterSite)}" />`),!f.twitterCreator&&i.twitterCreator&&p.push(`<meta name="twitter:creator" content="${l(i.twitterCreator)}" />`),!f.twitterTitle&&e&&p.push(`<meta name="twitter:title" content="${l(e)}" />`),!f.twitterDescription&&t&&p.push(`<meta name="twitter:description" content="${l(t)}" />`),!f.twitterImage&&n){let e=n.startsWith(`http`)?n:`${a}${n}`;p.push(`<meta name="twitter:image" content="${l(e)}" />`)}}if(i.fontPreconnect?.length)for(let e of i.fontPreconnect)n.includes(e)||(p.push(`<link rel="preconnect" href="${l(e)}" />`),(e.includes(`fonts.gstatic`)||e.includes(`cdn.fonts`))&&p.push(`<link rel="preconnect" href="${l(e)}" crossorigin />`));if(i.dnsPrefetch?.length)for(let e of i.dnsPrefetch)n.includes(e)||p.push(`<link rel="dns-prefetch" href="${l(e)}" />`);if(i.webAppManifest&&!n.includes(`rel="manifest"`)&&p.push(`<link rel="manifest" href="${l(i.webAppManifest)}" />`),i.feedUrl&&!n.includes(`application/rss+xml`)&&!n.includes(`application/atom+xml`)&&p.push(`<link rel="alternate" type="application/rss+xml" title="${l(i.siteName)}" href="${l(i.feedUrl)}" />`),i.themeColor&&!n.includes(`name="theme-color"`)&&p.push(`<meta name="theme-color" content="${l(i.themeColor)}" />`),i.appleTouchIcon&&!n.includes(`apple-touch-icon`)&&p.push(`<link rel="apple-touch-icon" href="${l(i.appleTouchIcon)}" />`),i.webFontStylesheet&&!n.includes(i.webFontStylesheet)&&p.push(`<link rel="stylesheet" href="${l(i.webFontStylesheet)}" />`),i.robotsImagePreview&&!f.robots&&!n.includes(`max-image-preview`)){let t=r===e?`noindex`:`max-image-preview:large, max-snippet:-1, max-video-preview:-1`;p.push(`<meta name="robots" content="${t}" />`)}if(i.hreflang&&!d&&!n.includes(`hreflang=`)){let e=(i.locale??`en_US`).split(/[-_]/)[0];p.push(`<link rel="alternate" hreflang="${l(e)}" href="${l(u)}" />`),p.push(`<link rel="alternate" hreflang="x-default" href="${l(u)}" />`)}if(p.length===0)return n;let m=n.toLowerCase().indexOf(`</head>`);return m===-1?n:n.slice(0,m)+p.join(`
2
2
  `)+`
3
- `+t.slice(l)}export function extractMetadataFromHtml(e){let t={},n=e.match(/<title[^>]*>([\s\S]*?)<\/title>/i);n&&(t.title=n[1].trim());let r=e.match(/<meta\s[^>]*name\s*=\s*["']description["'][^>]*content\s*=\s*["']([^"']*)["'][^>]*\/?>/i);if(r)t.description=r[1];else{let n=e.match(/<meta\s[^>]*content\s*=\s*["']([^"']*)["'][^>]*name\s*=\s*["']description["'][^>]*\/?>/i);n&&(t.description=n[1])}let i=extractMetaProperty(e,`og:title`),a=extractMetaProperty(e,`og:description`),o=extractMetaProperty(e,`og:image`),s=extractMetaProperty(e,`og:image:width`),c=extractMetaProperty(e,`og:image:height`);return(i||a||o)&&(t.openGraph={},i&&(t.openGraph.title=i),a&&(t.openGraph.description=a),o&&(t.openGraph.image=o),s&&(t.openGraph.imageWidth=parseInt(s,10)),c&&(t.openGraph.imageHeight=parseInt(c,10))),t}export function extractArticleMetaFromHtml(e){let t=extractMetaProperty(e,`article:published_time`),n=extractMetaProperty(e,`article:modified_time`),r=extractMetaProperty(e,`article:author`),i=extractMetaProperty(e,`article:section`),a=/<meta\s[^>]*property\s*=\s*["']article:tag["'][^>]*content\s*=\s*["']([^"']*)["'][^>]*\/?>/gi,o=[];for(let t=a.exec(e);t!==null;t=a.exec(e))o.push(t[1]);return{...t&&{datePublished:t},...n&&{dateModified:n},...r&&{author:r},...i&&{section:i},...o.length>0&&{tags:o}}}function e(e){let n={},r=e.match(/<title[^>]*>([\s\S]*?)<\/title>/i);r&&(n.title=r[1].trim()),n.description=t(e,`description`),n.robots=t(e,`robots`);let i=e.match(/<link\s[^>]*rel\s*=\s*["']canonical["'][^>]*href\s*=\s*["']([^"']*)["'][^>]*\/?>/i);return i&&(n.canonical=i[1]),n.ogTitle=extractMetaProperty(e,`og:title`),n.ogDescription=extractMetaProperty(e,`og:description`),n.ogImage=extractMetaProperty(e,`og:image`),n.ogImageWidth=extractMetaProperty(e,`og:image:width`),n.ogImageHeight=extractMetaProperty(e,`og:image:height`),n.ogUrl=extractMetaProperty(e,`og:url`),n.ogType=extractMetaProperty(e,`og:type`),n.ogLocale=extractMetaProperty(e,`og:locale`),n.ogSiteName=extractMetaProperty(e,`og:site_name`),n.twitterCard=t(e,`twitter:card`),n.twitterSite=t(e,`twitter:site`),n.twitterCreator=t(e,`twitter:creator`),n.twitterTitle=t(e,`twitter:title`),n.twitterDescription=t(e,`twitter:description`),n.twitterImage=t(e,`twitter:image`),n.articlePublishedTime=extractMetaProperty(e,`article:published_time`),n.articleModifiedTime=extractMetaProperty(e,`article:modified_time`),n.articleAuthor=extractMetaProperty(e,`article:author`),n.articleSection=extractMetaProperty(e,`article:section`),n}export function extractMetaProperty(e,t){let r=n(t),i=String.raw`<meta\s[^>]*property\s*=\s*["']`+r+String.raw`["'][^>]*content\s*=\s*["']([^"']*)["'][^>]*/?>`,a=String.raw`<meta\s[^>]*content\s*=\s*["']([^"']*)["'][^>]*property\s*=\s*["']`+r+String.raw`["'][^>]*/?>`,o=e.match(new RegExp(i,`i`));if(o)return o[1];let s=e.match(new RegExp(a,`i`));return s?s[1]:void 0}function t(e,t){let r=n(t),i=String.raw`<meta\s[^>]*name\s*=\s*["']`+r+String.raw`["'][^>]*content\s*=\s*["']([^"']*)["'][^>]*/?>`,a=String.raw`<meta\s[^>]*content\s*=\s*["']([^"']*)["'][^>]*name\s*=\s*["']`+r+String.raw`["'][^>]*/?>`,o=e.match(new RegExp(i,`i`));if(o)return o[1];let s=e.match(new RegExp(a,`i`));return s?s[1]:void 0}function n(e){return e.replaceAll(/[.*+?^${}()|[\]\\]/g,e=>`\\`+e)}function r(e){return e.replaceAll(`&`,`&amp;`).replaceAll(`"`,`&quot;`).replaceAll(`<`,`&lt;`).replaceAll(`>`,`&gt;`)}
3
+ `+n.slice(m)}export function extractMetadataFromHtml(e){let t={},n=e.match(/<title[^>]*>([\s\S]*?)<\/title>/i);n&&(t.title=n[1].trim());let r=e.match(/<meta\s[^>]*name\s*=\s*["']description["'][^>]*content\s*=\s*["']([^"']*)["'][^>]*\/?>/i);if(r)t.description=r[1];else{let n=e.match(/<meta\s[^>]*content\s*=\s*["']([^"']*)["'][^>]*name\s*=\s*["']description["'][^>]*\/?>/i);n&&(t.description=n[1])}let i=extractMetaProperty(e,`og:title`),a=extractMetaProperty(e,`og:description`),o=extractMetaProperty(e,`og:image`),s=extractMetaProperty(e,`og:image:width`),c=extractMetaProperty(e,`og:image:height`);return(i||a||o)&&(t.openGraph={},i&&(t.openGraph.title=i),a&&(t.openGraph.description=a),o&&(t.openGraph.image=o),s&&(t.openGraph.imageWidth=parseInt(s,10)),c&&(t.openGraph.imageHeight=parseInt(c,10))),t}export function extractArticleMetaFromHtml(e){let t=extractMetaProperty(e,`article:published_time`),n=extractMetaProperty(e,`article:modified_time`),r=extractMetaProperty(e,`article:author`),i=extractMetaProperty(e,`article:section`),a=/<meta\s[^>]*property\s*=\s*["']article:tag["'][^>]*content\s*=\s*["']([^"']*)["'][^>]*\/?>/gi,o=[];for(let t=a.exec(e);t!==null;t=a.exec(e))o.push(t[1]);return{...t&&{datePublished:t},...n&&{dateModified:n},...r&&{author:r},...i&&{section:i},...o.length>0&&{tags:o}}}function t(e){let t={},r=e.match(/<title[^>]*>([\s\S]*?)<\/title>/i);return r&&(t.title=r[1].trim()),t.description=n(e,`description`),t.robots=n(e,`robots`),t.canonical=o(e),t.ogTitle=extractMetaProperty(e,`og:title`),t.ogDescription=extractMetaProperty(e,`og:description`),t.ogImage=extractMetaProperty(e,`og:image`),t.ogImageWidth=extractMetaProperty(e,`og:image:width`),t.ogImageHeight=extractMetaProperty(e,`og:image:height`),t.ogUrl=extractMetaProperty(e,`og:url`),t.ogType=extractMetaProperty(e,`og:type`),t.ogLocale=extractMetaProperty(e,`og:locale`),t.ogSiteName=extractMetaProperty(e,`og:site_name`),t.twitterCard=n(e,`twitter:card`),t.twitterSite=n(e,`twitter:site`),t.twitterCreator=n(e,`twitter:creator`),t.twitterTitle=n(e,`twitter:title`),t.twitterDescription=n(e,`twitter:description`),t.twitterImage=n(e,`twitter:image`),t.articlePublishedTime=extractMetaProperty(e,`article:published_time`),t.articleModifiedTime=extractMetaProperty(e,`article:modified_time`),t.articleAuthor=extractMetaProperty(e,`article:author`),t.articleSection=extractMetaProperty(e,`article:section`),t}export function extractMetaProperty(e,t){let n=r(t),a=new RegExp(String.raw`<meta\s[^>]*`+i(`property`,n)+String.raw`[^>]*`+i(`content`)+String.raw`([^"']*)["'][^>]*/?>`,`i`),o=new RegExp(String.raw`<meta\s[^>]*`+i(`content`)+String.raw`([^"']*)["'][^>]*`+i(`property`,n)+String.raw`[^>]*/?>`,`i`),s=e.match(a);if(s)return s[1];let c=e.match(o);return c?c[1]:void 0}function n(e,t){let n=r(t),i=String.raw`<meta\s[^>]*name\s*=\s*["']`+n+String.raw`["'][^>]*content\s*=\s*["']([^"']*)["'][^>]*/?>`,a=String.raw`<meta\s[^>]*content\s*=\s*["']([^"']*)["'][^>]*name\s*=\s*["']`+n+String.raw`["'][^>]*/?>`,o=e.match(new RegExp(i,`i`));if(o)return o[1];let s=e.match(new RegExp(a,`i`));return s?s[1]:void 0}function r(e){return e.replaceAll(/[.*+?^${}()|[\]\\]/g,e=>`\\`+e)}function i(e,t){let n=String.raw`(?<![\w-])${r(e)}\s*=\s*["']`;return t===void 0?n:n+r(t)+String.raw`["']`}function a(){return[new RegExp(String.raw`<link\s[^>]*`+i(`rel`,`canonical`)+String.raw`[^>]*`+i(`href`)+String.raw`([^"']*)["'][^>]*\/?>`,`gi`),new RegExp(String.raw`<link\s[^>]*`+i(`href`)+String.raw`([^"']*)["'][^>]*`+i(`rel`,`canonical`)+String.raw`[^>]*\/?>`,`gi`)]}function o(e){for(let t of a()){let n=e.match(new RegExp(t.source,`i`));if(n)return n[1]}}function s(e,t,n){let r=e;for(let e of a())r=r.replaceAll(e,(e,r)=>r.startsWith(n)?e:`<link rel="canonical" href="${l(t)}" />`);return r}function c(e,t,n,a){let o=r(t),s=new RegExp(String.raw`<meta\s[^>]*`+i(`property`,o)+String.raw`[^>]*`+i(`content`)+String.raw`([^"']*)["'][^>]*/?>`,`gi`),c=new RegExp(String.raw`<meta\s[^>]*`+i(`content`)+String.raw`([^"']*)["'][^>]*`+i(`property`,o)+String.raw`[^>]*/?>`,`gi`),u=e.replaceAll(s,(e,r)=>r.startsWith(a)?e:`<meta property="${t}" content="${l(n)}" />`);return u=u.replaceAll(c,(e,r)=>r.startsWith(a)?e:`<meta property="${t}" content="${l(n)}" />`),u}function l(e){return e.replaceAll(`&`,`&amp;`).replaceAll(`"`,`&quot;`).replaceAll(`<`,`&lt;`).replaceAll(`>`,`&gt;`)}
@@ -0,0 +1,2 @@
1
+ export const SEO_HTML_POST_PROCESS_VIRTUAL_ID=`virtual:avalon/html-post-process-register`;export function generateSeoNitroRegisterModule(e){return[`import { registerHtmlPostProcess } from '@useavalon/avalon/nitro/html-post-process';`,`import { transformSeoHtml } from '@useavalon/seo/transform';`,`const seoConfig = ${JSON.stringify(e)};`,`registerHtmlPostProcess((html, pathname) => transformSeoHtml(html, pathname, seoConfig));`,``].join(`
2
+ `)}
@@ -1 +1 @@
1
- import{validateSeoConfig as e}from"./config.js";import{extractArticleMetaFromHtml as t,extractMetadataFromHtml as n,injectMetaTags as r}from"./meta-tags.js";import{buildArticleJsonLd as i,buildBreadcrumbListJsonLd as a,buildWebPageJsonLd as o,buildWebSiteJsonLd as s,injectJsonLd as c}from"./structured-data.js";export function seo(t){let n=e(t),r=[];return r.push({name:`avalon-seo`,enforce:`pre`,configureServer(e){e.middlewares.use((e,t,r)=>{let i=e.url??`/`;if(i.startsWith(`/@`)||i.startsWith(`/__`)||i.startsWith(`/node_modules/`)||i.startsWith(`/src/`)||i.startsWith(`/packages/`)||i.includes(`.`)&&!i.endsWith(`/`))return r();l(t,i,n),r()})}}),r}function l(e,t,n){let r=e.end.bind(e),i=e.setHeader.bind(e),a=e.writeHead.bind(e),o=[],s=!1,c=()=>{s=!0};e.setHeader=((e,t)=>(e.toLowerCase()===`content-type`&&typeof t==`string`&&t.includes(`text/html`)&&c(),i(e,t))),e.writeHead=((e,...t)=>{for(let e of t)if(e&&typeof e==`object`&&!Array.isArray(e))for(let[t,n]of Object.entries(e))t.toLowerCase()===`content-type`&&typeof n==`string`&&n.includes(`text/html`)&&c();return a(e,...t)}),e.end=((a,l,d)=>{if(!s){let t=e.getHeader(`content-type`);typeof t==`string`&&t.includes(`text/html`)&&c()}if(!s)return r(a,l,d);if(a){if(Buffer.isBuffer(a))o.push(a);else if(typeof a==`string`){let e=typeof l==`string`?l:`utf-8`;o.push(Buffer.from(a,e))}}if(o.length===0)return r(a,l,d);let f=Buffer.concat(o).toString(`utf-8`),p=typeof l==`function`?l:d,m=u(f,t,n);return m!==f&&i(`Content-Length`,String(Buffer.byteLength(m))),r(m,`utf-8`,p)})}function u(e,l,u){let d=n(e);if(!d.title&&!d.description)return e;let f=u.siteUrl.replace(/\/+$/,``),p=l.startsWith(`http`)?l:`${f}${l}`;if(u.titleSuffix!==!1&&d.title){let t=typeof u.titleSuffix==`string`?u.titleSuffix:` — ${u.siteName}`;if(!d.title.includes(u.siteName)){let n=d.title+t;e=e.replace(/<title[^>]*>[\s\S]*?<\/title>/i,`<title>${n}</title>`),d.title=n}}if(e=r(e,l,u),u.structuredData){if(e=c(e,o(d,p)),e=c(e,s(u)),u.breadcrumbs){let t=new URL(l,f||`http://localhost`).pathname,n=a(t,f);n&&(e=c(e,n))}let n=t(e);if(d.title&&(n.datePublished||d.description)){let t={title:d.title,description:d.description,image:d.openGraph?.image,imageWidth:d.openGraph?.imageWidth,imageHeight:d.openGraph?.imageHeight,datePublished:n.datePublished,dateModified:n.dateModified,author:n.author,section:n.section,tags:n.tags};e=c(e,i(t,p,u))}}return e}
1
+ import{validateSeoConfig as e}from"./config.js";import{patchResponseForSeo as t}from"./dev-middleware.js";import{generateSeoNitroRegisterModule as n,SEO_HTML_POST_PROCESS_VIRTUAL_ID as r}from"./nitro-register.js";async function i(e){let{setHtmlPostProcessRegisterModuleSource:t}=await import(`@useavalon/avalon/nitro/html-post-process-register`);t(e)}export function seo(a){let o=e(a),s=[];s.push({name:`avalon-seo`,enforce:`pre`,configureServer(e){e.middlewares.use((e,n,r)=>{let i=e.url??`/`;if(i.startsWith(`/@`)||i.startsWith(`/__`)||i.startsWith(`/node_modules/`)||i.startsWith(`/src/`)||i.startsWith(`/packages/`)||i.includes(`.`)&&!i.endsWith(`/`))return r();t(n,i,o),r()})}});let c=()=>n(o),l=`\0${r}`;return s.push({name:`avalon-seo:nitro`,enforce:`post`,resolveId(e){return e===r?l:null},load(e){return e===l?c():null},async configResolved(){await i(c)},async buildStart(){await i(c)}}),s}
@@ -0,0 +1 @@
1
+ import{validateSeoConfig as e}from"./config.js";export function resolveSiteUrl(e=`http://localhost:8012`){let t=process.env.AVALON_SITE_URL??process.env.SITE_URL;return t&&t.length>0?t.replace(/\/+$/,``):e.replace(/\/+$/,``)}export function withEnvSiteUrl(t){let n=resolveSiteUrl(typeof t.siteUrl==`string`?t.siteUrl:`http://localhost:8012`);return e({...t,siteUrl:n})}
@@ -1 +1 @@
1
- export function buildWebPageJsonLd(e,t){let n={"@context":`https://schema.org`,"@type":`WebPage`,url:t};return e.title&&(n.name=e.title),e.description&&(n.description=e.description),e.openGraph?.image&&(n.image=e.openGraph.image),n}export function buildWebSiteJsonLd(e){let t={"@context":`https://schema.org`,"@type":`WebSite`,url:e.siteUrl,name:e.siteName};if(e.searchPath){let n=e.siteUrl.replace(/\/+$/,``),r=e.searchQueryParam||`q`;t.potentialAction={"@type":`SearchAction`,target:`${n}${e.searchPath}?${r}={search_term_string}`,"query-input":`required name=search_term_string`}}return t}export function buildBreadcrumbListJsonLd(e,t){let n=e.split(`/`).filter(Boolean);if(n.length===0)return null;let r=t.replace(/\/+$/,``),i=[{"@type":`ListItem`,position:1,name:`Home`,item:r||`/`}];for(let e=0;e<n.length;e++){let t=n[e].replace(/[-_]/g,` `).replace(/\b\w/g,e=>e.toUpperCase()),a=e===n.length-1,o={"@type":`ListItem`,position:e+2,name:t};a||(o.item=`${r}/${n.slice(0,e+1).join(`/`)}`),i.push(o)}return{"@context":`https://schema.org`,"@type":`BreadcrumbList`,itemListElement:i}}export function buildArticleJsonLd(t,r,i){let a=i.siteUrl.replace(/\/+$/,``),o={"@context":`https://schema.org`,"@type":`Article`,headline:t.title,mainEntityOfPage:r};t.description&&(o.description=t.description);let s=t.image||i.defaultOgImage?.url;if(s){let e={"@type":`ImageObject`,url:s.startsWith(`http`)?s:`${a}${s}`},n=t.imageWidth||i.defaultOgImage?.width,r=t.imageHeight||i.defaultOgImage?.height;n&&(e.width=n),r&&(e.height=r),o.image=e}return t.datePublished&&(o.datePublished=t.datePublished),t.dateModified&&(o.dateModified=t.dateModified),o.author=e(t,i),o.publisher=n(i),t.wordCount&&(o.wordCount=t.wordCount),t.section&&(o.articleSection=t.section),t.tags?.length&&(o.keywords=t.tags.join(`, `)),i.speakable&&i.speakableSelectors?.length&&(o.speakable={"@type":`SpeakableSpecification`,cssSelector:i.speakableSelectors}),o}function e(e,n){if(n.author)return t(n.author);if(e.author){let t={"@type":`Person`,name:e.author};return e.authorUrl&&(t.url=e.authorUrl),t}return{"@type":`Person`,name:n.siteName}}function t(e){let t={"@type":`Person`,name:e.name};return e.url&&(t.url=e.url),e.image&&(t.image=e.image),e.jobTitle&&(t.jobTitle=e.jobTitle),e.description&&(t.description=e.description),e.worksFor&&(t.worksFor={"@type":`Organization`,name:e.worksFor.name,...e.worksFor.url&&{url:e.worksFor.url}}),e.sameAs?.length&&(t.sameAs=e.sameAs),t}function n(e){if(e.publisher){let t={"@type":`Organization`,name:e.publisher.name};return e.publisher.url&&(t.url=e.publisher.url),e.publisher.logo&&(t.logo={"@type":`ImageObject`,url:e.publisher.logo.url,...e.publisher.logo.width&&{width:e.publisher.logo.width},...e.publisher.logo.height&&{height:e.publisher.logo.height}}),t}let t=e.siteUrl.replace(/\/+$/,``);return{"@type":`Organization`,name:e.siteName,url:t}}export function injectJsonLd(e,t){let n=e.toLowerCase().indexOf(`</head>`);if(n===-1)return e;let r=`<script type="application/ld+json">${JSON.stringify(t)}<\/script>`;return e.slice(0,n)+r+e.slice(n)}
1
+ export function buildWebPageJsonLd(e,t,n){let r={"@context":`https://schema.org`,"@type":`WebPage`,url:t};return e.title&&(r.name=e.title),e.description&&(r.description=e.description),e.openGraph?.image&&(r.image=e.openGraph.image),n?.dateModified&&(r.dateModified=n.dateModified),r}export function buildWebSiteJsonLd(e){let t={"@context":`https://schema.org`,"@type":`WebSite`,url:e.siteUrl,name:e.siteName};if(e.searchPath){let n=e.siteUrl.replace(/\/+$/,``),r=e.searchQueryParam||`q`;t.potentialAction={"@type":`SearchAction`,target:`${n}${e.searchPath}?${r}={search_term_string}`,"query-input":`required name=search_term_string`}}return t}export function buildBreadcrumbListJsonLd(e,t){let n=e.split(`/`).filter(Boolean);if(n.length===0)return null;let r=t.replace(/\/+$/,``),i=[{"@type":`ListItem`,position:1,name:`Home`,item:r||`/`}];for(let e=0;e<n.length;e++){let t=n[e].replace(/[-_]/g,` `).replace(/\b\w/g,e=>e.toUpperCase()),a=e===n.length-1,o={"@type":`ListItem`,position:e+2,name:t};a||(o.item=`${r}/${n.slice(0,e+1).join(`/`)}`),i.push(o)}return{"@context":`https://schema.org`,"@type":`BreadcrumbList`,itemListElement:i}}export function buildArticleJsonLd(t,n,i){let a=i.siteUrl.replace(/\/+$/,``),o={"@context":`https://schema.org`,"@type":`Article`,headline:t.title,mainEntityOfPage:n};t.description&&(o.description=t.description);let s=t.image||i.defaultOgImage?.url;if(s){let e={"@type":`ImageObject`,url:s.startsWith(`http`)?s:`${a}${s}`},n=t.imageWidth||i.defaultOgImage?.width,r=t.imageHeight||i.defaultOgImage?.height;n&&(e.width=n),r&&(e.height=r),o.image=e}return t.datePublished&&(o.datePublished=t.datePublished),t.dateModified&&(o.dateModified=t.dateModified),o.author=e(t,i),o.publisher=r(i),t.wordCount&&(o.wordCount=t.wordCount),t.section&&(o.articleSection=t.section),t.tags?.length&&(o.keywords=t.tags.join(`, `)),i.speakable&&i.speakableSelectors?.length&&(o.speakable={"@type":`SpeakableSpecification`,cssSelector:i.speakableSelectors}),o}function e(e,n){if(n.author)return t(n.author);if(e.author){let t={"@type":`Person`,name:e.author};return e.authorUrl&&(t.url=e.authorUrl),t}return{"@type":`Person`,name:n.siteName}}function t(e){let t={"@type":`Person`,name:e.name};return e.url&&(t.url=e.url),e.image&&(t.image=e.image),e.jobTitle&&(t.jobTitle=e.jobTitle),e.description&&(t.description=e.description),e.worksFor&&(t.worksFor={"@type":`Organization`,name:e.worksFor.name,...e.worksFor.url&&{url:e.worksFor.url}}),e.sameAs?.length&&(t.sameAs=e.sameAs),t}function n(e,t){let n=t.replace(/\/+$/,``),r={"@type":`Organization`,name:e.name,url:e.url??n};return e.logo&&(r.logo={"@type":`ImageObject`,url:e.logo.url.startsWith(`http`)?e.logo.url:`${n}${e.logo.url}`,...e.logo.width&&{width:e.logo.width},...e.logo.height&&{height:e.logo.height}}),e.sameAs?.length&&(r.sameAs=e.sameAs),r}function r(e){if(e.publisher)return n(e.publisher,e.siteUrl);let t=e.siteUrl.replace(/\/+$/,``);return{"@type":`Organization`,name:e.siteName,url:t}}export function buildOrganizationJsonLd(e,t){return e?{"@context":`https://schema.org`,...n(e,t)}:null}export function buildFaqPageJsonLd(e){return{"@context":`https://schema.org`,"@type":`FAQPage`,mainEntity:e.map(e=>({"@type":`Question`,name:e.question,acceptedAnswer:{"@type":`Answer`,text:e.answer}}))}}export function injectJsonLd(e,t){let n=e.toLowerCase().indexOf(`</head>`);if(n===-1)return e;let r=`<script type="application/ld+json">${JSON.stringify(t)}<\/script>`;return e.slice(0,n)+r+e.slice(n)}
@@ -0,0 +1 @@
1
+ import{SEO_ERROR_PAGE_PATH as e,SEO_TRANSFORM_MARKER as t}from"./constants.js";import{extractArticleMetaFromHtml as n,extractMetadataFromHtml as r,injectMetaTags as i}from"./meta-tags.js";import{buildArticleJsonLd as a,buildBreadcrumbListJsonLd as o,buildFaqPageJsonLd as s,buildOrganizationJsonLd as c,buildWebPageJsonLd as l,buildWebSiteJsonLd as u,injectJsonLd as d}from"./structured-data.js";export{SEO_ERROR_PAGE_PATH,SEO_TRANSFORM_MARKER}from"./constants.js";function f(e){return!e||e===`/`?`/`:e.endsWith(`/`)?e:`${e}/`}function p(e){return e.replaceAll(`&`,`&amp;`).replaceAll(`<`,`&lt;`).replaceAll(`>`,`&gt;`)}function m(e){if(e.includes(t))return e;let n=e.toLowerCase().indexOf(`</head>`);return n===-1?`${e}\n${t}\n`:e.slice(0,n)+`${t}\n`+e.slice(n)}export function transformSeoHtml(h,g,_){if(h.includes(t))return h;let v=r(h);if(!v.title&&!v.description)return h;let y=_.siteUrl.replace(/\/+$/,``),b=g.startsWith(`http`)?new URL(g).pathname:g.startsWith(`/`)||g===e?g:`/${g}`,x=b===e,S=x?`/`:f(b),C=`${y}${S===`/`?`/`:S.replace(/\/$/,``)||`/`}`;if(_.titleSuffix!==!1&&v.title){let e=typeof _.titleSuffix==`string`?_.titleSuffix:` — ${_.siteName}`;if(!v.title.includes(_.siteName)){let t=v.title+e,n=p(t);h=h.replace(/<title[^>]*>[\s\S]*?<\/title>/i,()=>`<title>${n}</title>`),v.title=t}}if(h=i(h,x?e:S,_),_.structuredData){let e=n(h).dateModified??_.contentModified;if(h=d(h,l(v,C,{dateModified:e})),h=d(h,u(_)),_.publisher){let e=c(_.publisher,y);e&&(h=d(h,e))}if(_.breadcrumbs&&!x){let e=o(S,y);e&&(h=d(h,e))}_.faq?.length&&S===`/`&&(h=d(h,s(_.faq)));let t=n(h);if(v.title&&t.datePublished){let e={title:v.title,description:v.description,image:v.openGraph?.image,imageWidth:v.openGraph?.imageWidth,imageHeight:v.openGraph?.imageHeight,datePublished:t.datePublished,dateModified:t.dateModified,author:t.author,section:t.section,tags:t.tags};h=d(h,a(e,C,_))}}return m(h)}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@useavalon/seo",
3
- "version": "0.6.0",
3
+ "version": "0.7.1",
4
4
  "description": "SEO plugin for Avalon — auto-injects Open Graph, Twitter cards, canonical URLs, JSON-LD structured data, and robots meta tags",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -22,7 +22,14 @@
22
22
  "schema-org"
23
23
  ],
24
24
  "exports": {
25
- ".": "./dist/mod.js"
25
+ ".": "./dist/mod.js",
26
+ "./transform": "./dist/src/transform.js",
27
+ "./site-config": "./dist/src/site-config.js",
28
+ "./dist-transform": "./dist/src/dist-transform.js"
29
+ },
30
+ "peerDependencies": {
31
+ "@useavalon/avalon": "^0.7.0",
32
+ "vite": "^8.0.0"
26
33
  },
27
34
  "scripts": {
28
35
  "build": "bun run scripts/build.ts",
@@ -37,15 +44,13 @@
37
44
  "dependencies": {
38
45
  "zod": "^4.6.5"
39
46
  },
40
- "peerDependencies": {
41
- "vite": "^8.0.0"
42
- },
43
47
  "peerDependenciesMeta": {
44
48
  "vite": {
45
49
  "optional": true
46
50
  }
47
51
  },
48
52
  "devDependencies": {
53
+ "@useavalon/avalon": "workspace:*",
49
54
  "oxc-minify": "^0.153.0",
50
55
  "oxc-transform": "^0.153.0",
51
56
  "vitest": "^5.0.3"