aeo.js 0.0.3 → 0.0.4

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