@useavalon/seo 0.1.0 → 0.2.2
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/mod.js +1 -1
- package/dist/src/meta-tags.js +1 -1
- package/dist/src/plugin.js +1 -1
- package/package.json +1 -1
package/dist/mod.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export{
|
|
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";
|
package/dist/src/meta-tags.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export function injectMetaTags(e,t,n){let i=r(e),a=[],o=n.siteUrl.replace(/\/+$/,``),c=t.startsWith(`http`)?t:`${o}${t}`;if(n.canonical&&!i.canonical&&a.push(`<link rel="canonical" href="${s(c)}" />`),n.openGraph){let e=i.ogTitle||i.title,t=i.ogDescription||i.description||n.defaultDescription,r=i.ogImage||n.defaultOgImage?.url;if(!i.ogTitle&&e&&a.push(`<meta property="og:title" content="${s(e)}" />`),!i.ogDescription&&t&&a.push(`<meta property="og:description" content="${s(t)}" />`),i.ogUrl||a.push(`<meta property="og:url" content="${s(c)}" />`),!i.ogType){let e=i.articlePublishedTime?`article`:`website`;a.push(`<meta property="og:type" content="${e}" />`)}if(!i.ogLocale&&n.locale&&a.push(`<meta property="og:locale" content="${s(n.locale)}" />`),!i.ogSiteName&&n.siteName&&a.push(`<meta property="og:site_name" content="${s(n.siteName)}" />`),!i.ogImage&&r){let e=r.startsWith(`http`)?r:`${o}${r}`;a.push(`<meta property="og:image" content="${s(e)}" />`)}if(i.ogImage||r){if(!i.ogImageWidth){let e=n.defaultOgImage?.width||1200;a.push(`<meta property="og:image:width" content="${e}" />`)}if(!i.ogImageHeight){let e=n.defaultOgImage?.height||630;a.push(`<meta property="og:image:height" content="${e}" />`)}}}if(n.twitterCards){let e=i.twitterTitle||i.ogTitle||i.title,t=i.twitterDescription||i.ogDescription||i.description||n.defaultDescription,r=i.twitterImage||i.ogImage||n.defaultOgImage?.url;if(!i.twitterCard){let e=r?`summary_large_image`:`summary`;a.push(`<meta name="twitter:card" content="${e}" />`)}if(!i.twitterSite&&n.twitterSite&&a.push(`<meta name="twitter:site" content="${s(n.twitterSite)}" />`),!i.twitterCreator&&n.twitterCreator&&a.push(`<meta name="twitter:creator" content="${s(n.twitterCreator)}" />`),!i.twitterTitle&&e&&a.push(`<meta name="twitter:title" content="${s(e)}" />`),!i.twitterDescription&&t&&a.push(`<meta name="twitter:description" content="${s(t)}" />`),!i.twitterImage&&r){let e=r.startsWith(`http`)?r:`${o}${r}`;a.push(`<meta name="twitter:image" content="${s(e)}" />`)}}if(n.fontPreconnect?.length)for(let t of n.fontPreconnect)e.includes(t)||(a.push(`<link rel="preconnect" href="${s(t)}" />`),(t.includes(`fonts.gstatic`)||t.includes(`cdn.fonts`))&&a.push(`<link rel="preconnect" href="${s(t)}" crossorigin />`));if(a.length===0)return e;let l=e.toLowerCase().indexOf(`</head>`);return l===-1?e:e.slice(0,l)+a.join(`
|
|
2
2
|
`)+`
|
|
3
|
-
`+e.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 a=extractMetaProperty(e,`og:title`),o=extractMetaProperty(e,`og:description`),s=extractMetaProperty(e,`og:image`),c=extractMetaProperty(e,`og:image:width`),l=extractMetaProperty(e,`og:image:height`);return(a||o||s)&&(t.openGraph={},a&&(t.openGraph.title=a),o&&(t.openGraph.description=o),s&&(t.openGraph.image=s),c&&(t.openGraph.imageWidth=parseInt(c,10)),l&&(t.openGraph.imageHeight=parseInt(l,10))),t}export function extractArticleMetaFromHtml(e){let t=extractMetaProperty(e,`article:published_time`),n=extractMetaProperty(e,`article:modified_time`),r=extractMetaProperty(e,`article:author`),a=extractMetaProperty(e,`article:section`),o=/<meta\s[^>]*property\s*=\s*["']article:tag["'][^>]*content\s*=\s*["']([^"']*)["'][^>]*\/?>/gi,s=[]
|
|
3
|
+
`+e.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 a=extractMetaProperty(e,`og:title`),o=extractMetaProperty(e,`og:description`),s=extractMetaProperty(e,`og:image`),c=extractMetaProperty(e,`og:image:width`),l=extractMetaProperty(e,`og:image:height`);return(a||o||s)&&(t.openGraph={},a&&(t.openGraph.title=a),o&&(t.openGraph.description=o),s&&(t.openGraph.image=s),c&&(t.openGraph.imageWidth=parseInt(c,10)),l&&(t.openGraph.imageHeight=parseInt(l,10))),t}export function extractArticleMetaFromHtml(e){let t=extractMetaProperty(e,`article:published_time`),n=extractMetaProperty(e,`article:modified_time`),r=extractMetaProperty(e,`article:author`),a=extractMetaProperty(e,`article:section`),o=/<meta\s[^>]*property\s*=\s*["']article:tag["'][^>]*content\s*=\s*["']([^"']*)["'][^>]*\/?>/gi,s=[];for(let t=o.exec(e);t!==null;t=o.exec(e))s.push(t[1]);return{...t&&{datePublished:t},...n&&{dateModified:n},...r&&{author:r},...a&&{section:a},...s.length>0&&{tags:s}}}function r(e){let t={},n=e.match(/<title[^>]*>([\s\S]*?)<\/title>/i);n&&(t.title=n[1].trim()),t.description=a(e,`description`),t.robots=a(e,`robots`);let r=e.match(/<link\s[^>]*rel\s*=\s*["']canonical["'][^>]*href\s*=\s*["']([^"']*)["'][^>]*\/?>/i);return r&&(t.canonical=r[1]),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=a(e,`twitter:card`),t.twitterSite=a(e,`twitter:site`),t.twitterCreator=a(e,`twitter:creator`),t.twitterTitle=a(e,`twitter:title`),t.twitterDescription=a(e,`twitter:description`),t.twitterImage=a(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=o(t),r=String.raw`<meta\s[^>]*property\s*=\s*["']`+n+String.raw`["'][^>]*content\s*=\s*["']([^"']*)["'][^>]*/?>`,i=String.raw`<meta\s[^>]*content\s*=\s*["']([^"']*)["'][^>]*property\s*=\s*["']`+n+String.raw`["'][^>]*/?>`,a=e.match(new RegExp(r,`i`));if(a)return a[1];let s=e.match(new RegExp(i,`i`));return s?s[1]:void 0}function a(e,t){let n=o(t),r=String.raw`<meta\s[^>]*name\s*=\s*["']`+n+String.raw`["'][^>]*content\s*=\s*["']([^"']*)["'][^>]*/?>`,i=String.raw`<meta\s[^>]*content\s*=\s*["']([^"']*)["'][^>]*name\s*=\s*["']`+n+String.raw`["'][^>]*/?>`,a=e.match(new RegExp(r,`i`));if(a)return a[1];let s=e.match(new RegExp(i,`i`));return s?s[1]:void 0}function o(e){return e.replaceAll(/[.*+?^${}()|[\]\\]/g,e=>`\\`+e)}function s(e){return e.replaceAll(`&`,`&`).replaceAll(`"`,`"`).replaceAll(`<`,`<`).replaceAll(`>`,`>`)}
|
package/dist/src/plugin.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{validateSeoConfig as e}from"./config.js";import{
|
|
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();u(t,i,n),r()})}}),r}function u(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,u)=>{if(!s){let t=e.getHeader(`content-type`);typeof t==`string`&&t.includes(`text/html`)&&c()}if(!s)return r(a,l,u);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,u);let f=Buffer.concat(o).toString(`utf-8`),p=typeof l==`function`?l:u,m=d(f,t,n);return m!==f&&i(`Content-Length`,String(Buffer.byteLength(m))),r(m,`utf-8`,p)})}function d(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}
|
package/package.json
CHANGED