@stacksjs/stx 0.2.101 → 0.2.103

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.
@@ -13,15 +13,30 @@ export function injectSeo(html, site, page = {}, pagePath = "/") {
13
13
  tags.push(`<meta name="twitter:site" content="@${escapeAttr(seo.twitter.replace(/^@/, ""))}">`);
14
14
  tags.push(`<meta name="twitter:creator" content="@${escapeAttr(seo.twitter.replace(/^@/, ""))}">`);
15
15
  }
16
- const block = `<!-- SEO -->
17
- ${tags.join(`
18
- `)}
19
- <!-- /SEO -->`, stxAutoBlockRe = /<!--\s*stx SEO Tags\s*-->[\s\S]*?<meta\s+name="twitter:description"[^>]*>\s*/i;
16
+ const stxAutoBlockRe = /<!--\s*stx SEO Tags\s*-->[\s\S]*?<meta\s+name="twitter:description"[^>]*>\s*/i;
20
17
  if (stxAutoBlockRe.test(html))
21
18
  html = html.replace(stxAutoBlockRe, "");
22
19
  html = html.replace(/<!--\s*SEO\s*-->[\s\S]*?<!--\s*\/SEO\s*-->\s*/g, "");
23
- html = html.replace(/<title>[\s\S]*?<\/title>\s*/i, "");
24
- html = html.replace(/<meta\s+name="title"\s+content="stx Project"[^>]*>\s*/g, "").replace(/<meta\s+name="description"\s+content="A website built with stx[^"]*"[^>]*>\s*/g, "").replace(/<meta\s+property="og:title"\s+content="stx Project"[^>]*>\s*/g, "").replace(/<meta\s+property="og:description"\s+content="A website built with stx[^"]*"[^>]*>\s*/g, "").replace(/<meta\s+property="og:type"\s+content="website"[^>]*>\s*/g, "").replace(/<meta\s+name="twitter:card"\s+content="summary_large_image"[^>]*>\s*/g, "").replace(/<meta\s+name="twitter:title"\s+content="stx Project"[^>]*>\s*/g, "").replace(/<meta\s+name="twitter:description"\s+content="A website built with stx[^"]*"[^>]*>\s*/g, "");
20
+ html = html.replace(/<meta\s+name="title"\s+content="stx Project"[^>]*>\s*/g, "").replace(/<meta\s+name="description"\s+content="A website built with stx[^"]*"[^>]*>\s*/g, "").replace(/<meta\s+property="og:title"\s+content="stx Project"[^>]*>\s*/g, "").replace(/<meta\s+property="og:description"\s+content="A website built with stx[^"]*"[^>]*>\s*/g, "").replace(/<meta\s+name="twitter:title"\s+content="stx Project"[^>]*>\s*/g, "").replace(/<meta\s+name="twitter:description"\s+content="A website built with stx[^"]*"[^>]*>\s*/g, "");
21
+ const headEnd = html.search(/<\/head>/i), head = headEnd === -1 ? html : html.slice(0, headEnd), declared = (tag) => {
22
+ if (/^<title[\s>]/i.test(tag))
23
+ return /<title[\s>]/i.test(head);
24
+ if (/rel="canonical"/i.test(tag))
25
+ return /<link[^>]+rel=["']canonical["']/i.test(head);
26
+ const key = /(?:property|name)="([^"]+)"/i.exec(tag)?.[1];
27
+ if (!key)
28
+ return !1;
29
+ return new RegExp(`<meta[^>]+(?:property|name)=["']${key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}["']`, "i").test(head);
30
+ }, missing = tags.filter((tag) => !declared(tag));
31
+ if (missing.length === 0)
32
+ return html;
33
+ const block = `<!-- SEO -->
34
+ ${missing.join(`
35
+ `)}
36
+ <!-- /SEO -->`;
37
+ if (headEnd !== -1)
38
+ return `${html.slice(0, headEnd)}${block}
39
+ ${html.slice(headEnd)}`;
25
40
  if (/<head[^>]*>/i.test(html))
26
41
  return html.replace(/<head([^>]*)>/i, `<head$1>
27
42
  ${block}`);