aeo.js 0.0.3 → 0.0.5

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/astro.mjs CHANGED
@@ -1135,7 +1135,7 @@ function escapeAttr(str) {
1135
1135
  }
1136
1136
 
1137
1137
  // src/plugins/astro.ts
1138
- function scanBuiltPages(dir, baseUrl) {
1138
+ function scanBuiltPages(dir, _baseUrl) {
1139
1139
  const pages = [];
1140
1140
  function walk(currentDir) {
1141
1141
  try {
@@ -1164,7 +1164,8 @@ function scanBuiltPages(dir, baseUrl) {
1164
1164
  pathname,
1165
1165
  title,
1166
1166
  description,
1167
- content: textContent
1167
+ content: textContent,
1168
+ filePath: fullPath
1168
1169
  });
1169
1170
  } catch {
1170
1171
  }
@@ -1208,6 +1209,55 @@ function scanDevPages(pagesDir) {
1208
1209
  }
1209
1210
  return pages;
1210
1211
  }
1212
+ function escapeAttr2(str) {
1213
+ return str.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
1214
+ }
1215
+ function injectHeadTags(pages, config) {
1216
+ let injectedCount = 0;
1217
+ for (const page of pages) {
1218
+ let html;
1219
+ try {
1220
+ html = readFileSync(page.filePath, "utf-8");
1221
+ } catch {
1222
+ continue;
1223
+ }
1224
+ const tags = [];
1225
+ if (!/name=["']description["']/i.test(html)) {
1226
+ const desc = page.description || config.description;
1227
+ if (desc) {
1228
+ tags.push(`<meta name="description" content="${escapeAttr2(desc)}" />`);
1229
+ }
1230
+ }
1231
+ if (!/rel=["']canonical["']/i.test(html)) {
1232
+ const pageUrl = page.pathname === "/" ? config.url : `${config.url.replace(/\/$/, "")}${page.pathname}`;
1233
+ tags.push(`<link rel="canonical" href="${escapeAttr2(pageUrl)}" />`);
1234
+ }
1235
+ if (config.og.enabled && !/property=["']og:/i.test(html)) {
1236
+ const ogHtml = generateOGTagsHtml(page, config);
1237
+ if (ogHtml) tags.push(ogHtml);
1238
+ }
1239
+ if (config.schema.enabled && !/application\/ld\+json/i.test(html)) {
1240
+ const siteSchemas = generateSiteSchemas(config);
1241
+ const pageSchemas = generatePageSchemas(page, config);
1242
+ const jsonLdHtml = generateJsonLdScript([...siteSchemas, ...pageSchemas]);
1243
+ if (jsonLdHtml) tags.push(jsonLdHtml);
1244
+ }
1245
+ if (!/rel=["']alternate["'][^>]*llms\.txt/i.test(html)) {
1246
+ tags.push(`<link rel="alternate" type="text/plain" href="/llms.txt" title="LLM Summary" />`);
1247
+ tags.push(`<link rel="alternate" type="text/plain" href="/llms-full.txt" title="Full Content for LLMs" />`);
1248
+ tags.push(`<link rel="alternate" type="application/json" href="/docs.json" title="Documentation Manifest" />`);
1249
+ tags.push(`<link rel="alternate" type="application/json" href="/ai-index.json" title="AI-Optimized Index" />`);
1250
+ }
1251
+ if (tags.length === 0) continue;
1252
+ const injection = "\n " + tags.join("\n ") + "\n ";
1253
+ const newHtml = html.replace("</head>", injection + "</head>");
1254
+ if (newHtml !== html) {
1255
+ writeFileSync(page.filePath, newHtml, "utf-8");
1256
+ injectedCount++;
1257
+ }
1258
+ }
1259
+ return injectedCount;
1260
+ }
1211
1261
  function aeoAstroIntegration(options = {}) {
1212
1262
  let resolvedConfig = resolveConfig(options);
1213
1263
  let astroConfig;
@@ -1288,6 +1338,14 @@ if (!document.querySelector('meta[name="astro-view-transitions-enabled"]')) {
1288
1338
  } catch (error) {
1289
1339
  buildLogger.error(`Failed to generate AEO files: ${error}`);
1290
1340
  }
1341
+ try {
1342
+ const injected = injectHeadTags(discoveredPages, resolvedConfig);
1343
+ if (injected > 0) {
1344
+ buildLogger.info(`Injected head tags into ${injected} pages`);
1345
+ }
1346
+ } catch (error) {
1347
+ buildLogger.error(`Failed to inject head tags: ${error}`);
1348
+ }
1291
1349
  },
1292
1350
  "astro:server:setup": async ({ server, logger }) => {
1293
1351
  const devLogger = logger.fork("aeo.js");