@useavalon/seo 0.2.2 → 0.5.3
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/src/meta-tags.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export function injectMetaTags(
|
|
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(`
|
|
2
2
|
`)+`
|
|
3
|
-
`+
|
|
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(`&`,`&`).replaceAll(`"`,`"`).replaceAll(`<`,`<`).replaceAll(`>`,`>`)}
|
package/dist/src/plugin.js
CHANGED
|
@@ -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();
|
|
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 +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(
|
|
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)}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@useavalon/seo",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.3",
|
|
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",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"README.md"
|
|
36
36
|
],
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"zod": "^4.
|
|
38
|
+
"zod": "^4.6.5"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
41
|
"vite": "^8.0.0"
|
|
@@ -46,8 +46,8 @@
|
|
|
46
46
|
}
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"oxc-minify": "^0.
|
|
50
|
-
"oxc-transform": "^0.
|
|
51
|
-
"vitest": "^
|
|
49
|
+
"oxc-minify": "^0.152.0",
|
|
50
|
+
"oxc-transform": "^0.152.0",
|
|
51
|
+
"vitest": "^5.0.3"
|
|
52
52
|
}
|
|
53
53
|
}
|