@verbaly/compiler 0.34.0 → 0.35.0

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/index.js CHANGED
@@ -1274,13 +1274,15 @@ function renderHtml(html, options) {
1274
1274
  const missing = /* @__PURE__ */ new Set();
1275
1275
  const skip = protectedRanges(html);
1276
1276
  const inSkip = (index) => skip.some(([from, to]) => index >= from && index < to);
1277
+ const filled = [];
1278
+ const inFilled = (index) => filled.some(([from, to]) => index >= from && index < to);
1277
1279
  const selfUrl = options.alternates?.find((a) => a.hreflang === options.locale)?.href;
1278
1280
  const sourceUrl = options.alternates?.find((a) => a.hreflang === "x-default")?.href;
1279
1281
  const rewriteUrl = selfUrl !== void 0 && selfUrl !== sourceUrl;
1280
1282
  START_TAG.lastIndex = 0;
1281
1283
  let m;
1282
1284
  while ((m = START_TAG.exec(html)) !== null) {
1283
- if (inSkip(m.index)) continue;
1285
+ if (inSkip(m.index) || inFilled(m.index)) continue;
1284
1286
  const [full, rawName, attrChunk] = m;
1285
1287
  const tagName = rawName.toLowerCase();
1286
1288
  const openEnd = m.index + full.length;
@@ -1293,6 +1295,18 @@ function renderHtml(html, options) {
1293
1295
  const attrs = parseAttrs(attrChunk);
1294
1296
  if (rewriteUrl && tagName === "link" && attrs.get("rel")?.toLowerCase() === "canonical" && decodeEntities(attrs.get("href") ?? "") === sourceUrl) setAttribute(ms, html, chunkStart, openEnd, attrChunk, "href", selfUrl);
1295
1297
  if (rewriteUrl && tagName === "meta" && attrs.get("property") === "og:url" && decodeEntities(attrs.get("content") ?? "") === sourceUrl) setAttribute(ms, html, chunkStart, openEnd, attrChunk, "content", selfUrl);
1298
+ const mirror = options.mirror;
1299
+ if (mirror) {
1300
+ if (tagName === "a" || tagName === "area") {
1301
+ const inside = mirroredHref(attrs.get("href"), mirror);
1302
+ if (inside) setAttribute(ms, html, chunkStart, openEnd, attrChunk, "href", inside);
1303
+ } else if (tagName === "meta" && attrs.get("http-equiv")?.toLowerCase() === "refresh") {
1304
+ const content = attrs.get("content") ?? "";
1305
+ const url = /^([^;]*;\s*url\s*=\s*)(.*)$/i.exec(decodeEntities(content));
1306
+ const inside = url && mirroredHref(url[2], mirror);
1307
+ if (inside) setAttribute(ms, html, chunkStart, openEnd, attrChunk, "content", `${url[1]}${inside}`);
1308
+ }
1309
+ }
1296
1310
  const key = attrs.get(attr);
1297
1311
  const attrMapRaw = attrs.get(attrsAttr);
1298
1312
  if (key === void 0 && attrMapRaw === void 0) continue;
@@ -1311,6 +1325,7 @@ function renderHtml(html, options) {
1311
1325
  const content = attrs.has(richAttr) ? richToHtml(parseTags(text), richTags, links) : escapeHtml(text);
1312
1326
  if (html.slice(openEnd, close.contentEnd) !== content) if (openEnd === close.contentEnd) ms.appendLeft(openEnd, content);
1313
1327
  else ms.overwrite(openEnd, close.contentEnd, content);
1328
+ filled.push([openEnd, close.contentEnd]);
1314
1329
  }
1315
1330
  }
1316
1331
  }
@@ -1333,6 +1348,19 @@ function renderHtml(html, options) {
1333
1348
  missing: [...missing]
1334
1349
  };
1335
1350
  }
1351
+ function publicPath(rel) {
1352
+ const path = `/${rel.replace(/(^|\/)index\.html$/, "$1")}`;
1353
+ return path.length > 1 ? path.replace(/\/+$/, "") : path;
1354
+ }
1355
+ function mirroredHref(raw, mirror) {
1356
+ if (raw === void 0) return void 0;
1357
+ const href = decodeEntities(raw);
1358
+ if (!href.startsWith("/") || href.startsWith("//")) return void 0;
1359
+ const cut = href.search(/[?#]/);
1360
+ const path = cut < 0 ? href : href.slice(0, cut);
1361
+ if (!mirror.pages.has(publicPath(path.replace(/^\//, "")))) return void 0;
1362
+ return `${mirror.prefix}${href}`;
1363
+ }
1336
1364
  function injectAlternates(ms, html, alternates) {
1337
1365
  const links = alternates.map((a) => `<link rel="alternate" hreflang="${escapeAttr(a.hreflang)}" href="${escapeAttr(a.href)}">`).join("");
1338
1366
  const block = `${HREFLANG_OPEN}${links}${HREFLANG_CLOSE}`;
@@ -1369,6 +1397,7 @@ async function renderSite(cfg, options = {}) {
1369
1397
  absolute: true,
1370
1398
  ignore: locales.map((locale) => `${locale}/**`)
1371
1399
  });
1400
+ const pages = new Set(files.map((file) => publicPath(relative(site, file).replace(/\\/g, "/"))));
1372
1401
  const missing = {};
1373
1402
  const urls = [];
1374
1403
  for (const file of files) {
@@ -1387,7 +1416,11 @@ async function renderSite(cfg, options = {}) {
1387
1416
  attribute,
1388
1417
  richTags: options.richTags,
1389
1418
  richLinks: options.richLinks ?? cfg.render.links,
1390
- alternates
1419
+ alternates,
1420
+ mirror: locale === cfg.sourceLocale ? void 0 : {
1421
+ prefix: `/${locale}`,
1422
+ pages
1423
+ }
1391
1424
  });
1392
1425
  for (const key of result.missing) {
1393
1426
  const list = missing[locale] ??= [];