@verbaly/compiler 0.51.0 → 0.53.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/README.md +12 -8
- package/dist/cli.js +40 -5
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +15 -2
- package/dist/index.js +830 -779
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -701,8 +701,8 @@ async function extractProject(cfg) {
|
|
|
701
701
|
for (const file of files) registry.update(file, analyzeFile(readFileSync(file, "utf8"), file));
|
|
702
702
|
return registry;
|
|
703
703
|
}
|
|
704
|
-
async function collectOrigins(cfg) {
|
|
705
|
-
const registry = await extractProject(cfg);
|
|
704
|
+
async function collectOrigins(cfg, reuse) {
|
|
705
|
+
const registry = reuse ?? await extractProject(cfg);
|
|
706
706
|
const origins = {};
|
|
707
707
|
for (const [key, files] of registry.origins()) origins[key] = files.map((file) => relative(cfg.root, file).replaceAll("\\", "/"));
|
|
708
708
|
return origins;
|
|
@@ -831,7 +831,7 @@ function validatePair(source, translated) {
|
|
|
831
831
|
const newParams = surplus(paramNames(translated), paramNames(source));
|
|
832
832
|
if (lostParams.length > 0) issues.push({
|
|
833
833
|
severity: "error",
|
|
834
|
-
message: `${list(lostParams.map((name) => `{${name}}`))} ${lostParams.length > 1 ? "are" : "is"} missing, so ${lostParams.length > 1 ? "their values" : "its value
|
|
834
|
+
message: `${list(lostParams.map((name) => `{${name}}`))} ${lostParams.length > 1 ? "are" : "is"} missing, so ${lostParams.length > 1 ? "their values never reach" : "its value never reaches"} the text`
|
|
835
835
|
});
|
|
836
836
|
if (newParams.length > 0) issues.push({
|
|
837
837
|
severity: "error",
|
|
@@ -1474,6 +1474,16 @@ function markDrafts(drafts, locale, keys) {
|
|
|
1474
1474
|
if (!keys.length) return;
|
|
1475
1475
|
drafts[locale] = [.../* @__PURE__ */ new Set([...drafts[locale] ?? [], ...keys])];
|
|
1476
1476
|
}
|
|
1477
|
+
function clearDrafts(drafts, locale, keys) {
|
|
1478
|
+
if (!drafts[locale]) return;
|
|
1479
|
+
if (!keys) {
|
|
1480
|
+
delete drafts[locale];
|
|
1481
|
+
return;
|
|
1482
|
+
}
|
|
1483
|
+
const drop = new Set(keys);
|
|
1484
|
+
drafts[locale] = drafts[locale].filter((key) => !drop.has(key));
|
|
1485
|
+
if (!drafts[locale].length) delete drafts[locale];
|
|
1486
|
+
}
|
|
1477
1487
|
function effectiveDrafts(drafts, catalogs) {
|
|
1478
1488
|
const out = {};
|
|
1479
1489
|
for (const [locale, keys] of Object.entries(drafts)) {
|
|
@@ -1534,6 +1544,164 @@ function formatStatusResult(result) {
|
|
|
1534
1544
|
return lines.join("\n");
|
|
1535
1545
|
}
|
|
1536
1546
|
//#endregion
|
|
1547
|
+
//#region src/doctor.ts
|
|
1548
|
+
const PREVIEW = 5;
|
|
1549
|
+
function readRoutingChoice(root, configFile) {
|
|
1550
|
+
try {
|
|
1551
|
+
return /\brouting["']?\s*[:=]/.test(readFileSync(join(root, configFile), "utf8"));
|
|
1552
|
+
} catch {
|
|
1553
|
+
return false;
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
const ROUTING_SAYS = {
|
|
1557
|
+
"prefix-except-source": "the source locale has no prefix, every other locale does",
|
|
1558
|
+
"prefix-all": "every locale has a prefix, the source included",
|
|
1559
|
+
"no-prefix": "the language is not in the url, one address serves every locale"
|
|
1560
|
+
};
|
|
1561
|
+
const ICON = {
|
|
1562
|
+
ok: "✓",
|
|
1563
|
+
warn: "⚠",
|
|
1564
|
+
error: "✗"
|
|
1565
|
+
};
|
|
1566
|
+
function formatDoctorEntry(entry) {
|
|
1567
|
+
const head = ` ${ICON[entry.level]} ${entry.check}: ${entry.message}`;
|
|
1568
|
+
return entry.fix ? `${head}
|
|
1569
|
+
fix: ${entry.fix}` : head;
|
|
1570
|
+
}
|
|
1571
|
+
async function doctor(cfg) {
|
|
1572
|
+
const entries = [];
|
|
1573
|
+
const ok = (name, message) => entries.push({
|
|
1574
|
+
level: "ok",
|
|
1575
|
+
check: name,
|
|
1576
|
+
message
|
|
1577
|
+
});
|
|
1578
|
+
const warn = (name, message, fix) => entries.push({
|
|
1579
|
+
level: "warn",
|
|
1580
|
+
check: name,
|
|
1581
|
+
message,
|
|
1582
|
+
fix
|
|
1583
|
+
});
|
|
1584
|
+
const error = (name, message, fix) => entries.push({
|
|
1585
|
+
level: "error",
|
|
1586
|
+
check: name,
|
|
1587
|
+
message,
|
|
1588
|
+
fix
|
|
1589
|
+
});
|
|
1590
|
+
const rel = (path) => relative(cfg.root, path).replaceAll("\\", "/");
|
|
1591
|
+
const configFile = findConfigFile(cfg.root);
|
|
1592
|
+
if (configFile) ok("config", `${configFile} found`);
|
|
1593
|
+
else warn("config", "no config file, running on defaults", "run `npx verbaly init`");
|
|
1594
|
+
if (cliReachable(cfg.root)) ok("cli", "the verbaly command is available in this project");
|
|
1595
|
+
else warn("cli", "the verbaly command is not linked in node_modules/.bin", CLI_INSTALL_FIX);
|
|
1596
|
+
const catalogs = {};
|
|
1597
|
+
let catalogsHealthy = true;
|
|
1598
|
+
if (!existsSync(cfg.dir)) {
|
|
1599
|
+
catalogsHealthy = false;
|
|
1600
|
+
error("catalogs", `catalogs directory ${rel(cfg.dir)}/ does not exist`, "run `npx verbaly init` to scaffold it");
|
|
1601
|
+
} else {
|
|
1602
|
+
for (const locale of cfg.locales) {
|
|
1603
|
+
const file = join(cfg.dir, `${locale}.json`);
|
|
1604
|
+
if (!existsSync(file)) {
|
|
1605
|
+
catalogsHealthy = false;
|
|
1606
|
+
error(`locale ${locale}`, `${rel(file)} is missing`, "run `npx verbaly extract` to create it");
|
|
1607
|
+
continue;
|
|
1608
|
+
}
|
|
1609
|
+
try {
|
|
1610
|
+
const parsed = JSON.parse(readFileSync(file, "utf8"));
|
|
1611
|
+
const bad = badLeaf(parsed);
|
|
1612
|
+
if (bad) {
|
|
1613
|
+
catalogsHealthy = false;
|
|
1614
|
+
error(`locale ${locale}`, `${rel(file)} has a non-string value at "${bad}"`, "catalog values are text (groups of text are fine); fix the value");
|
|
1615
|
+
} else catalogs[locale] = flatten(parsed);
|
|
1616
|
+
} catch {
|
|
1617
|
+
catalogsHealthy = false;
|
|
1618
|
+
error(`locale ${locale}`, `${rel(file)} is not valid JSON`, "repair the file (or delete it and run `npx verbaly extract`)");
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
if (catalogsHealthy) {
|
|
1622
|
+
const how = cfg.localesDeclared ? "" : ", no locales set, so the catalog files decide";
|
|
1623
|
+
ok("catalogs", `${counted(cfg.locales.length, "locale")} (${cfg.locales.join(", ")}) in ${rel(cfg.dir)}/${how}`);
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
const source = catalogs[cfg.sourceLocale];
|
|
1627
|
+
if (source && Object.keys(source).length === 0) warn("source", `source catalog ${cfg.sourceLocale}.json is empty`, "write your first t`…` message and run `npx verbaly extract`");
|
|
1628
|
+
const mirrors = cfg.render.site !== void 0 || Object.keys(cfg.render).length > 0;
|
|
1629
|
+
if (cfg.routing === "no-prefix" && mirrors) warn("routing", "routing is \"no-prefix\" but a render section is configured, and render writes one url tree per locale", "drop the render section, or set routing to \"prefix-except-source\" so the helpers agree with the urls");
|
|
1630
|
+
else {
|
|
1631
|
+
const how = (configFile ? readRoutingChoice(cfg.root, configFile) : false) ? "" : " (from your setup, no routing set)";
|
|
1632
|
+
ok("routing", `${cfg.routing}${how}: ${ROUTING_SAYS[cfg.routing]}`);
|
|
1633
|
+
}
|
|
1634
|
+
const deps = readDependencies(cfg.root);
|
|
1635
|
+
const host = detectHost(cfg.root);
|
|
1636
|
+
const installed = WIRING_PACKAGES.find((pkg) => deps[pkg]);
|
|
1637
|
+
if (!host) ok("plugin", "no framework or bundler detected, the CLI flow (extract/check) applies");
|
|
1638
|
+
else if (installed) ok("plugin", `${installed} installed for ${host.name}`);
|
|
1639
|
+
else warn("plugin", `${host.name} detected but ${host.pkg} is not installed`, `pnpm add -D ${host.pkg} and ${host.wire}`);
|
|
1640
|
+
const scanning = cfg.include.length > 0;
|
|
1641
|
+
if (!scanning) ok("sources", "source scanning is off (include: []), the catalogs are the source of truth");
|
|
1642
|
+
if (source && scanning && cfg.dts !== false) {
|
|
1643
|
+
const dtsPath = cfg.dts ?? join(cfg.root, "verbaly.d.ts");
|
|
1644
|
+
if (!existsSync(dtsPath)) warn("types", "verbaly.d.ts has not been generated", "run `npx verbaly extract`");
|
|
1645
|
+
else if (readFileSync(dtsPath, "utf8") !== generateDts(source, { inlineCatalog: cfg.render.inlineCatalog === true })) warn("types", "verbaly.d.ts is stale", "run `npx verbaly extract`");
|
|
1646
|
+
else ok("types", "verbaly.d.ts is up to date");
|
|
1647
|
+
}
|
|
1648
|
+
const registry = await extractProject(cfg);
|
|
1649
|
+
if (scanning) {
|
|
1650
|
+
const unreadable = registry.parseErrors();
|
|
1651
|
+
if (unreadable.length > 0) {
|
|
1652
|
+
const first = unreadable[0];
|
|
1653
|
+
warn("sources", `could not parse ${counted(unreadable.length, "file")}, so the messages inside are not extracted (${rel(first.file)}: ${first.message})`, "fix the syntax error, or exclude the file in your config if it is not source");
|
|
1654
|
+
}
|
|
1655
|
+
const stray = registry.strayImports();
|
|
1656
|
+
if (stray.length > 0) {
|
|
1657
|
+
const files = [...new Set(stray.map((entry) => rel(entry.file)))];
|
|
1658
|
+
error("imports", `t is imported from a verbaly package, which never exports it, in ${counted(files.length, "file")} (${preview(files)})`, "t comes from your instance (React: const t = useT()) or from virtual:verbaly");
|
|
1659
|
+
}
|
|
1660
|
+
const escaped = [...registry.messages().values()].map((msg) => ({
|
|
1661
|
+
file: rel(msg.file),
|
|
1662
|
+
slice: escapedSyntax(msg.message)
|
|
1663
|
+
})).filter((entry) => entry.slice !== void 0);
|
|
1664
|
+
if (escaped.length > 0) warn("messages", `a block ships as literal text in ${counted(escaped.length, "extracted message")} (${escaped[0].file}: ${escaped[0].slice})`, "a tagged template takes its values from ${…}: use t(key, params) for a plural or format block");
|
|
1665
|
+
}
|
|
1666
|
+
if (source && scanning) {
|
|
1667
|
+
const extracted = registry.messages();
|
|
1668
|
+
const used = registry.usedKeys();
|
|
1669
|
+
const orphans = Object.keys(source).filter((key) => !extracted.has(key) && !used.has(key));
|
|
1670
|
+
if (orphans.length > 0) warn("orphans", `${counted(orphans.length, "catalog key")} no longer referenced in code (${preview(orphans)})`, "run `npx verbaly extract --prune` to drop them");
|
|
1671
|
+
else ok("orphans", "no orphan keys");
|
|
1672
|
+
}
|
|
1673
|
+
const excluded = cfg.bundle.exclude ?? [];
|
|
1674
|
+
if (excluded.length > 0 && catalogsHealthy) {
|
|
1675
|
+
const issues = auditBundle(cfg, catalogs, registry);
|
|
1676
|
+
for (const issue of issues) warn("bundle", formatBundleIssue(issue), issue.fix);
|
|
1677
|
+
if (issues.length === 0) ok("bundle", `${counted(excluded.length, "group")} kept out of the client (${preview(excluded)})`);
|
|
1678
|
+
}
|
|
1679
|
+
if (catalogsHealthy) {
|
|
1680
|
+
const result = check(cfg, catalogs, registry);
|
|
1681
|
+
if (result.unknown.length > 0) error("keys", `${counted(result.unknown.length, "unknown key")} used in code (${preview(result.unknown.map((u) => u.key))})`, "fix the key or add it to the catalogs (`npx verbaly check` for details)");
|
|
1682
|
+
if (result.missing.length > 0) {
|
|
1683
|
+
const locales = [...new Set(result.missing.map((m) => m.locale))];
|
|
1684
|
+
warn("translations", `${counted(result.missing.length, "missing translation")} (${locales.join(", ")})`, "run `npx verbaly translate` or fill the catalogs (`npx verbaly check` for details)");
|
|
1685
|
+
}
|
|
1686
|
+
const broken = result.broken.filter((entry) => entry.severity === "error");
|
|
1687
|
+
if (broken.length > 0) {
|
|
1688
|
+
const locales = [...new Set(broken.map((b) => b.locale))];
|
|
1689
|
+
error("translations", `${counted(broken.length, "broken translation")} (${locales.join(", ")}): present but not rendering what the source renders`, "run `npx verbaly check` to read what each one lost");
|
|
1690
|
+
}
|
|
1691
|
+
const warnings = result.broken.filter((entry) => entry.severity === "warning");
|
|
1692
|
+
if (warnings.length > 0) warn("translations", `${counted(warnings.length, "structural warning")} (plural forms a language asks for)`, "run `npx verbaly check` to read them, they never fail the build");
|
|
1693
|
+
if (result.ok) ok("translations", "all translations complete");
|
|
1694
|
+
}
|
|
1695
|
+
return {
|
|
1696
|
+
ok: entries.every((entry) => entry.level !== "error"),
|
|
1697
|
+
entries
|
|
1698
|
+
};
|
|
1699
|
+
}
|
|
1700
|
+
function preview(keys) {
|
|
1701
|
+
const head = keys.slice(0, PREVIEW).join(", ");
|
|
1702
|
+
return keys.length > PREVIEW ? `${head}, …` : head;
|
|
1703
|
+
}
|
|
1704
|
+
//#endregion
|
|
1537
1705
|
//#region src/translate.ts
|
|
1538
1706
|
const RETRY_STATUS = /* @__PURE__ */ new Set([
|
|
1539
1707
|
408,
|
|
@@ -1695,568 +1863,169 @@ function glossaryFor(cfg, locale, messages) {
|
|
|
1695
1863
|
return Object.keys(out).length > 0 ? out : void 0;
|
|
1696
1864
|
}
|
|
1697
1865
|
//#endregion
|
|
1698
|
-
//#region src/
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
return false;
|
|
1705
|
-
}
|
|
1706
|
-
}
|
|
1707
|
-
const ROUTING_SAYS = {
|
|
1708
|
-
"prefix-except-source": "the source locale has no prefix, every other locale does",
|
|
1709
|
-
"prefix-all": "every locale has a prefix, the source included",
|
|
1710
|
-
"no-prefix": "the language is not in the url, one address serves every locale"
|
|
1711
|
-
};
|
|
1712
|
-
const ICON = {
|
|
1713
|
-
ok: "✓",
|
|
1714
|
-
warn: "⚠",
|
|
1715
|
-
error: "✗"
|
|
1716
|
-
};
|
|
1717
|
-
function formatDoctorEntry(entry) {
|
|
1718
|
-
const head = ` ${ICON[entry.level]} ${entry.check}: ${entry.message}`;
|
|
1719
|
-
return entry.fix ? `${head}
|
|
1720
|
-
fix: ${entry.fix}` : head;
|
|
1721
|
-
}
|
|
1722
|
-
async function doctor(cfg) {
|
|
1723
|
-
const entries = [];
|
|
1724
|
-
const ok = (name, message) => entries.push({
|
|
1725
|
-
level: "ok",
|
|
1726
|
-
check: name,
|
|
1727
|
-
message
|
|
1728
|
-
});
|
|
1729
|
-
const warn = (name, message, fix) => entries.push({
|
|
1730
|
-
level: "warn",
|
|
1731
|
-
check: name,
|
|
1732
|
-
message,
|
|
1733
|
-
fix
|
|
1734
|
-
});
|
|
1735
|
-
const error = (name, message, fix) => entries.push({
|
|
1736
|
-
level: "error",
|
|
1737
|
-
check: name,
|
|
1738
|
-
message,
|
|
1739
|
-
fix
|
|
1866
|
+
//#region src/render.ts
|
|
1867
|
+
function localeInstance(catalogs, locale, sourceLocale) {
|
|
1868
|
+
return createVerbaly({
|
|
1869
|
+
locale,
|
|
1870
|
+
fallback: sourceLocale,
|
|
1871
|
+
messages: catalogs
|
|
1740
1872
|
});
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
}
|
|
1785
|
-
const deps = readDependencies(cfg.root);
|
|
1786
|
-
const host = detectHost(cfg.root);
|
|
1787
|
-
const installed = WIRING_PACKAGES.find((pkg) => deps[pkg]);
|
|
1788
|
-
if (!host) ok("plugin", "no framework or bundler detected, the CLI flow (extract/check) applies");
|
|
1789
|
-
else if (installed) ok("plugin", `${installed} installed for ${host.name}`);
|
|
1790
|
-
else warn("plugin", `${host.name} detected but ${host.pkg} is not installed`, `pnpm add -D ${host.pkg} and ${host.wire}`);
|
|
1791
|
-
const scanning = cfg.include.length > 0;
|
|
1792
|
-
if (!scanning) ok("sources", "source scanning is off (include: []), the catalogs are the source of truth");
|
|
1793
|
-
if (source && scanning && cfg.dts !== false) {
|
|
1794
|
-
const dtsPath = cfg.dts ?? join(cfg.root, "verbaly.d.ts");
|
|
1795
|
-
if (!existsSync(dtsPath)) warn("types", "verbaly.d.ts has not been generated", "run `npx verbaly extract`");
|
|
1796
|
-
else if (readFileSync(dtsPath, "utf8") !== generateDts(source, { inlineCatalog: cfg.render.inlineCatalog === true })) warn("types", "verbaly.d.ts is stale", "run `npx verbaly extract`");
|
|
1797
|
-
else ok("types", "verbaly.d.ts is up to date");
|
|
1798
|
-
}
|
|
1799
|
-
const registry = await extractProject(cfg);
|
|
1800
|
-
if (scanning) {
|
|
1801
|
-
const unreadable = registry.parseErrors();
|
|
1802
|
-
if (unreadable.length > 0) {
|
|
1803
|
-
const first = unreadable[0];
|
|
1804
|
-
warn("sources", `could not parse ${counted(unreadable.length, "file")}, so the messages inside are not extracted (${rel(first.file)}: ${first.message})`, "fix the syntax error, or exclude the file in your config if it is not source");
|
|
1873
|
+
}
|
|
1874
|
+
const HREFLANG_OPEN = "<!--verbaly:hreflang-->";
|
|
1875
|
+
const HREFLANG_CLOSE = "<!--/verbaly:hreflang-->";
|
|
1876
|
+
const CATALOG_OPEN = "<!--verbaly:catalog-->";
|
|
1877
|
+
const CATALOG_CLOSE = "<!--/verbaly:catalog-->";
|
|
1878
|
+
const REDIRECT_OPEN = "<!--verbaly:redirect-->";
|
|
1879
|
+
const REDIRECT_CLOSE = "<!--/verbaly:redirect-->";
|
|
1880
|
+
const VOID = new Set(VOID_TAGS);
|
|
1881
|
+
const START_TAG = /<([a-zA-Z][a-zA-Z0-9-]*)((?:"[^"]*"|'[^']*'|[^"'>])*)>/g;
|
|
1882
|
+
const ATTR = /([^\s=/"'<>]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+)))?/g;
|
|
1883
|
+
function renderHtml(html, options) {
|
|
1884
|
+
const attr = options.attribute ?? "data-verbaly";
|
|
1885
|
+
const argsAttr = `${attr}-args`;
|
|
1886
|
+
const attrsAttr = `${attr}-attr`;
|
|
1887
|
+
const richAttr = `${attr}-rich`;
|
|
1888
|
+
const linksAttr = `${attr}-links`;
|
|
1889
|
+
const richTags = new Set(options.richTags ?? RICH_TAGS);
|
|
1890
|
+
const globalLinks = options.richLinks;
|
|
1891
|
+
const sourceLocale = options.sourceLocale ?? "en";
|
|
1892
|
+
const v = options.instance ?? localeInstance(options.catalogs, options.locale, sourceLocale);
|
|
1893
|
+
const t = v.t;
|
|
1894
|
+
const ms = new MagicString(html);
|
|
1895
|
+
const missing = /* @__PURE__ */ new Set();
|
|
1896
|
+
const used = /* @__PURE__ */ new Set();
|
|
1897
|
+
const skip = protectedRanges(html);
|
|
1898
|
+
const inSkip = (index) => skip.some(([from, to]) => index >= from && index < to);
|
|
1899
|
+
const filled = [];
|
|
1900
|
+
const inFilled = (index) => filled.some(([from, to]) => index >= from && index < to);
|
|
1901
|
+
const selfUrl = options.alternates?.find((a) => a.hreflang === options.locale)?.href;
|
|
1902
|
+
const sourceUrl = options.alternates?.find((a) => a.hreflang === "x-default")?.href;
|
|
1903
|
+
const rewriteUrl = selfUrl !== void 0 && selfUrl !== sourceUrl;
|
|
1904
|
+
START_TAG.lastIndex = 0;
|
|
1905
|
+
let m;
|
|
1906
|
+
while ((m = START_TAG.exec(html)) !== null) {
|
|
1907
|
+
if (inSkip(m.index) || inFilled(m.index)) continue;
|
|
1908
|
+
const [full, rawName, attrChunk] = m;
|
|
1909
|
+
const tagName = rawName.toLowerCase();
|
|
1910
|
+
const openEnd = m.index + full.length;
|
|
1911
|
+
const chunkStart = m.index + 1 + rawName.length;
|
|
1912
|
+
if (tagName === "html" && options.setLang !== false) {
|
|
1913
|
+
setAttribute(ms, html, chunkStart, openEnd, attrChunk, "lang", options.locale);
|
|
1914
|
+
setAttribute(ms, html, chunkStart, openEnd, attrChunk, "dir", localeDirection(options.locale));
|
|
1915
|
+
continue;
|
|
1805
1916
|
}
|
|
1806
|
-
const
|
|
1807
|
-
if (
|
|
1808
|
-
|
|
1809
|
-
|
|
1917
|
+
const attrs = parseAttrs(attrChunk);
|
|
1918
|
+
if (rewriteUrl && tagName === "link" && attrs.get("rel")?.toLowerCase() === "canonical" && decodeEntities(attrs.get("href") ?? "") === sourceUrl) setAttribute(ms, html, chunkStart, openEnd, attrChunk, "href", selfUrl);
|
|
1919
|
+
if (rewriteUrl && tagName === "meta" && attrs.get("property") === "og:url" && decodeEntities(attrs.get("content") ?? "") === sourceUrl) setAttribute(ms, html, chunkStart, openEnd, attrChunk, "content", selfUrl);
|
|
1920
|
+
const mirror = options.mirror;
|
|
1921
|
+
if (mirror) {
|
|
1922
|
+
if (tagName === "a" || tagName === "area") {
|
|
1923
|
+
const inside = mirroredHref(attrs.get("href"), mirror);
|
|
1924
|
+
if (inside) setAttribute(ms, html, chunkStart, openEnd, attrChunk, "href", inside);
|
|
1925
|
+
} else if (tagName === "form" || tagName === "button" || tagName === "input") {
|
|
1926
|
+
const name = tagName === "form" ? "action" : "formaction";
|
|
1927
|
+
const inside = mirroredHref(attrs.get(name), mirror);
|
|
1928
|
+
if (inside) setAttribute(ms, html, chunkStart, openEnd, attrChunk, name, inside);
|
|
1929
|
+
} else if (tagName === "meta" && attrs.get("http-equiv")?.toLowerCase() === "refresh") {
|
|
1930
|
+
const content = attrs.get("content") ?? "";
|
|
1931
|
+
const url = /^([^;]*;\s*url\s*=\s*)(.*)$/i.exec(decodeEntities(content));
|
|
1932
|
+
const inside = url && mirroredHref(url[2], mirror);
|
|
1933
|
+
if (inside) setAttribute(ms, html, chunkStart, openEnd, attrChunk, "content", `${url[1]}${inside}`);
|
|
1934
|
+
}
|
|
1810
1935
|
}
|
|
1811
|
-
const
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
if (
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1936
|
+
const key = attrs.get(attr);
|
|
1937
|
+
const attrMapRaw = attrs.get(attrsAttr);
|
|
1938
|
+
if (key === void 0 && attrMapRaw === void 0) continue;
|
|
1939
|
+
const args = parseArgs(attrs.get(argsAttr));
|
|
1940
|
+
if (key) {
|
|
1941
|
+
if (!v.has(key)) missing.add(key);
|
|
1942
|
+
else if (!VOID.has(tagName) && !attrChunk.trimEnd().endsWith("/")) {
|
|
1943
|
+
used.add(key);
|
|
1944
|
+
const close = findClose(html, tagName, openEnd, inSkip);
|
|
1945
|
+
if (close) {
|
|
1946
|
+
const text = t(key, args);
|
|
1947
|
+
const own = parseArgs(attrs.get(linksAttr));
|
|
1948
|
+
const merged = own ? globalLinks ? {
|
|
1949
|
+
...globalLinks,
|
|
1950
|
+
...own
|
|
1951
|
+
} : own : globalLinks;
|
|
1952
|
+
const links = mirror ? mirroredLinks(merged, mirror) : merged;
|
|
1953
|
+
const nodes = attrs.has(richAttr) ? parseTags(text) : [];
|
|
1954
|
+
if (mirror && links) {
|
|
1955
|
+
const carried = usedLinks(nodes, links);
|
|
1956
|
+
if (carried && JSON.stringify(carried) !== JSON.stringify(own)) setAttribute(ms, html, chunkStart, openEnd, attrChunk, linksAttr, JSON.stringify(carried));
|
|
1957
|
+
}
|
|
1958
|
+
const content = attrs.has(richAttr) ? richToHtml(nodes, richTags, links) : escapeHtml(text);
|
|
1959
|
+
if (html.slice(openEnd, close.contentEnd) !== content) {
|
|
1960
|
+
if (openEnd === close.contentEnd) ms.appendLeft(openEnd, content);
|
|
1961
|
+
else ms.overwrite(openEnd, close.contentEnd, content);
|
|
1962
|
+
}
|
|
1963
|
+
filled.push([openEnd, close.contentEnd]);
|
|
1964
|
+
}
|
|
1965
|
+
}
|
|
1836
1966
|
}
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1967
|
+
if (attrMapRaw !== void 0) {
|
|
1968
|
+
const map = parseArgs(attrMapRaw);
|
|
1969
|
+
if (map) for (const [name, attrKey] of Object.entries(map)) {
|
|
1970
|
+
if (typeof attrKey !== "string") continue;
|
|
1971
|
+
if (!v.has(attrKey)) {
|
|
1972
|
+
missing.add(attrKey);
|
|
1973
|
+
continue;
|
|
1974
|
+
}
|
|
1975
|
+
used.add(attrKey);
|
|
1976
|
+
const value = safeAttribute(name, t(attrKey, args));
|
|
1977
|
+
if (value !== void 0) setAttribute(ms, html, chunkStart, openEnd, attrChunk, name, value);
|
|
1978
|
+
}
|
|
1841
1979
|
}
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1980
|
+
}
|
|
1981
|
+
if (options.alternates?.length) injectAlternates(ms, html, options.alternates);
|
|
1982
|
+
if (options.redirect) injectRedirect(ms, html, options.redirect);
|
|
1983
|
+
if (options.inlineCatalog && options.locale !== sourceLocale) {
|
|
1984
|
+
const slice = pageSlice(v, options.locale, used);
|
|
1985
|
+
if (Object.keys(slice).length > 0) injectCatalog(ms, html, slice);
|
|
1845
1986
|
}
|
|
1846
1987
|
return {
|
|
1847
|
-
|
|
1848
|
-
|
|
1988
|
+
html: ms.toString(),
|
|
1989
|
+
missing: [...missing],
|
|
1990
|
+
used: [...used]
|
|
1849
1991
|
};
|
|
1850
1992
|
}
|
|
1851
|
-
function
|
|
1852
|
-
const
|
|
1853
|
-
|
|
1993
|
+
function pageSlice(v, locale, used) {
|
|
1994
|
+
const slice = {};
|
|
1995
|
+
for (const key of used) {
|
|
1996
|
+
const found = v.inspect(key);
|
|
1997
|
+
slice[key] = found?.from === locale ? found.source : "";
|
|
1998
|
+
}
|
|
1999
|
+
return slice;
|
|
1854
2000
|
}
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
"kbd",
|
|
1867
|
-
"samp",
|
|
1868
|
-
"var"
|
|
1869
|
-
]);
|
|
1870
|
-
const T_NAMES = /* @__PURE__ */ new Set(["t"]);
|
|
1871
|
-
const LETTER = /\p{L}/u;
|
|
1872
|
-
const WHITESPACE = /\s/;
|
|
1873
|
-
async function wrapProject(cfg, options = {}) {
|
|
1874
|
-
const files = (await glob(cfg.include, {
|
|
1875
|
-
cwd: cfg.root,
|
|
1876
|
-
ignore: cfg.exclude,
|
|
1877
|
-
absolute: true
|
|
1878
|
-
})).filter((file) => /\.[jt]sx$/.test(file));
|
|
1879
|
-
const result = {
|
|
1880
|
-
files: files.length,
|
|
1881
|
-
changed: [],
|
|
1882
|
-
wrapped: [],
|
|
1883
|
-
skipped: [],
|
|
1884
|
-
blocked: []
|
|
1885
|
-
};
|
|
1886
|
-
for (const file of files) {
|
|
1887
|
-
const code = readFileSync(file, "utf8");
|
|
1888
|
-
const label = relative(cfg.root, file).replaceAll("\\", "/");
|
|
1889
|
-
const out = wrapCode(code, label);
|
|
1890
|
-
result.wrapped.push(...out.wrapped);
|
|
1891
|
-
result.skipped.push(...out.skipped);
|
|
1892
|
-
if (out.code === void 0) continue;
|
|
1893
|
-
result.changed.push(label);
|
|
1894
|
-
if (!out.hasT) {
|
|
1895
|
-
result.blocked.push({
|
|
1896
|
-
file: label,
|
|
1897
|
-
texts: out.wrapped.length,
|
|
1898
|
-
client: out.client
|
|
1899
|
-
});
|
|
1900
|
-
continue;
|
|
2001
|
+
function publicPath(rel) {
|
|
2002
|
+
const path = `/${rel.replace(/(^|\/)index\.html$/, "$1")}`;
|
|
2003
|
+
return path.length > 1 ? path.replace(/\/+$/, "") : path;
|
|
2004
|
+
}
|
|
2005
|
+
function usedLinks(nodes, links) {
|
|
2006
|
+
const out = {};
|
|
2007
|
+
const walk = (list) => {
|
|
2008
|
+
for (const node of list) {
|
|
2009
|
+
if (typeof node === "string") continue;
|
|
2010
|
+
if (links[node.name] !== void 0) out[node.name] = links[node.name];
|
|
2011
|
+
walk(node.children);
|
|
1901
2012
|
}
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
return
|
|
2013
|
+
};
|
|
2014
|
+
walk(nodes);
|
|
2015
|
+
return Object.keys(out).length > 0 ? out : void 0;
|
|
1905
2016
|
}
|
|
1906
|
-
function
|
|
1907
|
-
|
|
1908
|
-
const
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
}).program;
|
|
1916
|
-
} catch {
|
|
1917
|
-
return {
|
|
1918
|
-
wrapped,
|
|
1919
|
-
skipped,
|
|
1920
|
-
hasT: false,
|
|
1921
|
-
client: false
|
|
2017
|
+
function mirroredLinks(links, mirror) {
|
|
2018
|
+
if (!links) return links;
|
|
2019
|
+
const out = {};
|
|
2020
|
+
for (const [name, link] of Object.entries(links)) {
|
|
2021
|
+
const inside = mirroredHref(typeof link === "string" ? link : link.href, mirror);
|
|
2022
|
+
if (inside === void 0) out[name] = link;
|
|
2023
|
+
else out[name] = typeof link === "string" ? inside : {
|
|
2024
|
+
...link,
|
|
2025
|
+
href: inside
|
|
1922
2026
|
};
|
|
1923
2027
|
}
|
|
1924
|
-
|
|
1925
|
-
const client = hasDirective(ast, "use client");
|
|
1926
|
-
const s = new MagicString(code);
|
|
1927
|
-
const visited = /* @__PURE__ */ new Set();
|
|
1928
|
-
let changed = false;
|
|
1929
|
-
walk$1(ast, (node) => {
|
|
1930
|
-
if (node.type !== "JSXElement" && node.type !== "JSXFragment") return;
|
|
1931
|
-
if (visited.has(node)) return;
|
|
1932
|
-
processElement(node);
|
|
1933
|
-
});
|
|
1934
|
-
return changed ? {
|
|
1935
|
-
code: s.toString(),
|
|
1936
|
-
wrapped,
|
|
1937
|
-
skipped,
|
|
1938
|
-
hasT,
|
|
1939
|
-
client
|
|
1940
|
-
} : {
|
|
1941
|
-
wrapped,
|
|
1942
|
-
skipped,
|
|
1943
|
-
hasT,
|
|
1944
|
-
client
|
|
1945
|
-
};
|
|
1946
|
-
function processElement(node) {
|
|
1947
|
-
visited.add(node);
|
|
1948
|
-
if (node.type === "JSXElement") {
|
|
1949
|
-
const opening = node.openingElement;
|
|
1950
|
-
const name = opening.name;
|
|
1951
|
-
if (name.type === "JSXIdentifier" && name.name === "Trans") return markSubtree(node);
|
|
1952
|
-
if (hasVerbalyAttr(opening)) return markSubtree(node);
|
|
1953
|
-
if (name.type === "JSXIdentifier" && CODE_TAGS.has(name.name)) return markSubtree(node);
|
|
1954
|
-
wrapAttributes(opening);
|
|
1955
|
-
}
|
|
1956
|
-
const children = node.children ?? [];
|
|
1957
|
-
const hasElementChild = children.some((c) => c.type === "JSXElement" || c.type === "JSXFragment");
|
|
1958
|
-
const hasText = children.some((c) => c.type === "JSXText" && LETTER.test(c.value) || c.type === "JSXExpressionContainer" && c.expression.type === "StringLiteral" && LETTER.test(c.expression.value));
|
|
1959
|
-
if (hasElementChild && hasText) {
|
|
1960
|
-
skip(node, snippet(children), "mixed text and markup: wrap by hand or use <Trans>");
|
|
1961
|
-
return markSubtree(node);
|
|
1962
|
-
}
|
|
1963
|
-
if (hasText) {
|
|
1964
|
-
wrapSegment(node, children);
|
|
1965
|
-
return markSubtree(node);
|
|
1966
|
-
}
|
|
1967
|
-
for (const child of children) if (child.type === "JSXElement" || child.type === "JSXFragment") processElement(child);
|
|
1968
|
-
}
|
|
1969
|
-
function wrapSegment(parent, children) {
|
|
1970
|
-
const meaningful = children.filter((c) => c.type === "JSXText" && c.value.trim() !== "" || c.type === "JSXExpressionContainer" && c.expression.type !== "JSXEmptyExpression");
|
|
1971
|
-
if (meaningful.length === 0) return;
|
|
1972
|
-
const first = meaningful[0];
|
|
1973
|
-
const last = meaningful[meaningful.length - 1];
|
|
1974
|
-
if (first.type === "JSXExpressionContainer" && meaningful.length > 1) {
|
|
1975
|
-
skip(parent, snippet(children), "starts with an interpolation: wrap the sentence by hand");
|
|
1976
|
-
return;
|
|
1977
|
-
}
|
|
1978
|
-
let template = "";
|
|
1979
|
-
let report = "";
|
|
1980
|
-
for (let i = children.indexOf(first); i <= children.indexOf(last); i++) {
|
|
1981
|
-
const child = children[i];
|
|
1982
|
-
if (child.type === "JSXText") {
|
|
1983
|
-
let value = cleanJsxText(child.value);
|
|
1984
|
-
if (child === first) value = value.trimStart();
|
|
1985
|
-
if (child === last) value = value.trimEnd();
|
|
1986
|
-
template += escapeTemplate(value);
|
|
1987
|
-
report += value;
|
|
1988
|
-
} else if (child.type === "JSXExpressionContainer") {
|
|
1989
|
-
const expr = child.expression;
|
|
1990
|
-
if (expr.type === "JSXEmptyExpression") continue;
|
|
1991
|
-
if (usesT(expr)) {
|
|
1992
|
-
skip(parent, snippet(children), "already uses t: finish it by hand");
|
|
1993
|
-
return;
|
|
1994
|
-
}
|
|
1995
|
-
if (containsJsx(expr)) {
|
|
1996
|
-
skip(parent, snippet(children), "an expression renders markup: wrap by hand or use <Trans>");
|
|
1997
|
-
markSubtree(child);
|
|
1998
|
-
return;
|
|
1999
|
-
}
|
|
2000
|
-
if (expr.type === "StringLiteral") {
|
|
2001
|
-
template += escapeTemplate(expr.value);
|
|
2002
|
-
report += expr.value;
|
|
2003
|
-
} else {
|
|
2004
|
-
const source = code.slice(expr.start, expr.end);
|
|
2005
|
-
template += "${" + source + "}";
|
|
2006
|
-
report += "${" + source + "}";
|
|
2007
|
-
}
|
|
2008
|
-
} else return;
|
|
2009
|
-
}
|
|
2010
|
-
if (!LETTER.test(report)) return;
|
|
2011
|
-
if (isIdentifierLike(report)) return;
|
|
2012
|
-
const start = first.type === "JSXText" ? first.start + leadingWs(first.value) : first.start;
|
|
2013
|
-
const end = last.type === "JSXText" ? last.end - trailingWs(last.value) : last.end;
|
|
2014
|
-
s.overwrite(start, end, "{t`" + template + "`}");
|
|
2015
|
-
changed = true;
|
|
2016
|
-
wrapped.push({
|
|
2017
|
-
file,
|
|
2018
|
-
line: line(first),
|
|
2019
|
-
text: report,
|
|
2020
|
-
kind: "text"
|
|
2021
|
-
});
|
|
2022
|
-
}
|
|
2023
|
-
function wrapAttributes(opening) {
|
|
2024
|
-
for (const attr of opening.attributes) {
|
|
2025
|
-
if (attr.type !== "JSXAttribute") continue;
|
|
2026
|
-
const name = attr.name;
|
|
2027
|
-
if (name.type !== "JSXIdentifier" || !WRAP_ATTRS.has(name.name)) continue;
|
|
2028
|
-
const value = attr.value;
|
|
2029
|
-
if (value?.type !== "StringLiteral") continue;
|
|
2030
|
-
const raw = value.value;
|
|
2031
|
-
if (!LETTER.test(raw) || isIdentifierLike(raw)) continue;
|
|
2032
|
-
s.overwrite(value.start, value.end, "{t`" + escapeTemplate(raw) + "`}");
|
|
2033
|
-
changed = true;
|
|
2034
|
-
wrapped.push({
|
|
2035
|
-
file,
|
|
2036
|
-
line: line(value),
|
|
2037
|
-
text: raw,
|
|
2038
|
-
kind: "attribute",
|
|
2039
|
-
attribute: name.name
|
|
2040
|
-
});
|
|
2041
|
-
}
|
|
2042
|
-
}
|
|
2043
|
-
function skip(node, text, reason) {
|
|
2044
|
-
skipped.push({
|
|
2045
|
-
file,
|
|
2046
|
-
line: line(node),
|
|
2047
|
-
text,
|
|
2048
|
-
reason
|
|
2049
|
-
});
|
|
2050
|
-
}
|
|
2051
|
-
function snippet(children) {
|
|
2052
|
-
const start = children[0]?.start ?? 0;
|
|
2053
|
-
const end = children[children.length - 1]?.end ?? start;
|
|
2054
|
-
const text = code.slice(start, end).replace(/\s+/g, " ").trim();
|
|
2055
|
-
return text.length > 60 ? text.slice(0, 59) + "…" : text;
|
|
2056
|
-
}
|
|
2057
|
-
function markSubtree(node) {
|
|
2058
|
-
walk$1(node, (n) => {
|
|
2059
|
-
if (n.type === "JSXElement" || n.type === "JSXFragment") visited.add(n);
|
|
2060
|
-
});
|
|
2061
|
-
}
|
|
2062
|
-
}
|
|
2063
|
-
function isIdentifierLike(text) {
|
|
2064
|
-
const trimmed = text.trim();
|
|
2065
|
-
if (WHITESPACE.test(trimmed)) return false;
|
|
2066
|
-
const first = [...trimmed].find((char) => LETTER.test(char));
|
|
2067
|
-
return first !== void 0 && first !== first.toUpperCase();
|
|
2068
|
-
}
|
|
2069
|
-
function hasDirective(program, value) {
|
|
2070
|
-
return (program.directives ?? []).some((d) => d.value?.value === value);
|
|
2071
|
-
}
|
|
2072
|
-
function declaresT(program) {
|
|
2073
|
-
let found = false;
|
|
2074
|
-
walk$1(program, (node) => {
|
|
2075
|
-
if (found) return;
|
|
2076
|
-
if (node.type === "ImportDeclaration") {
|
|
2077
|
-
for (const spec of node.specifiers) if (isTName(spec.local?.name)) found = true;
|
|
2078
|
-
return;
|
|
2079
|
-
}
|
|
2080
|
-
if (node.type === "VariableDeclarator" && bindsT(node.id)) found = true;
|
|
2081
|
-
if (node.type === "FunctionDeclaration" && isTName(node.id?.name)) found = true;
|
|
2082
|
-
if (node.params?.some((param) => bindsT(param))) found = true;
|
|
2083
|
-
});
|
|
2084
|
-
return found;
|
|
2085
|
-
}
|
|
2086
|
-
function bindsT(pattern) {
|
|
2087
|
-
if (!pattern) return false;
|
|
2088
|
-
if (pattern.type === "Identifier") return isTName(pattern.name);
|
|
2089
|
-
if (pattern.type === "AssignmentPattern") return bindsT(pattern.left);
|
|
2090
|
-
if (pattern.type === "ObjectPattern") return pattern.properties.some((prop) => prop.type === "RestElement" ? bindsT(prop.argument) : bindsT(prop.value));
|
|
2091
|
-
if (pattern.type === "ArrayPattern") return pattern.elements.some((el) => bindsT(el ?? void 0));
|
|
2092
|
-
return false;
|
|
2093
|
-
}
|
|
2094
|
-
function isTName(name) {
|
|
2095
|
-
return typeof name === "string" && T_NAMES.has(name);
|
|
2096
|
-
}
|
|
2097
|
-
function hasVerbalyAttr(opening) {
|
|
2098
|
-
return opening.attributes.some((attr) => {
|
|
2099
|
-
if (attr.type !== "JSXAttribute") return false;
|
|
2100
|
-
const name = attr.name;
|
|
2101
|
-
return name.type === "JSXIdentifier" && name.name.startsWith("data-verbaly");
|
|
2102
|
-
});
|
|
2103
|
-
}
|
|
2104
|
-
function usesT(node) {
|
|
2105
|
-
let found = false;
|
|
2106
|
-
walk$1(node, (n) => {
|
|
2107
|
-
if (n.type === "TaggedTemplateExpression" && isTReference(n.tag, T_NAMES)) found = true;
|
|
2108
|
-
if (n.type === "CallExpression" && isTReference(n.callee, T_NAMES)) found = true;
|
|
2109
|
-
});
|
|
2110
|
-
return found;
|
|
2111
|
-
}
|
|
2112
|
-
function containsJsx(node) {
|
|
2113
|
-
let found = false;
|
|
2114
|
-
walk$1(node, (n) => {
|
|
2115
|
-
if (n.type === "JSXElement" || n.type === "JSXFragment") found = true;
|
|
2116
|
-
});
|
|
2117
|
-
return found;
|
|
2118
|
-
}
|
|
2119
|
-
function escapeTemplate(text) {
|
|
2120
|
-
return text.replace(/[\\`]/g, "\\$&").replace(/\$\{/g, "\\${");
|
|
2121
|
-
}
|
|
2122
|
-
function leadingWs(text) {
|
|
2123
|
-
return text.length - text.trimStart().length;
|
|
2124
|
-
}
|
|
2125
|
-
function trailingWs(text) {
|
|
2126
|
-
return text.length - text.trimEnd().length;
|
|
2127
|
-
}
|
|
2128
|
-
function line(node) {
|
|
2129
|
-
return node.loc?.start?.line ?? 1;
|
|
2130
|
-
}
|
|
2131
|
-
//#endregion
|
|
2132
|
-
//#region src/render.ts
|
|
2133
|
-
function localeInstance(catalogs, locale, sourceLocale) {
|
|
2134
|
-
return createVerbaly({
|
|
2135
|
-
locale,
|
|
2136
|
-
fallback: sourceLocale,
|
|
2137
|
-
messages: catalogs
|
|
2138
|
-
});
|
|
2139
|
-
}
|
|
2140
|
-
const HREFLANG_OPEN = "<!--verbaly:hreflang-->";
|
|
2141
|
-
const HREFLANG_CLOSE = "<!--/verbaly:hreflang-->";
|
|
2142
|
-
const CATALOG_OPEN = "<!--verbaly:catalog-->";
|
|
2143
|
-
const CATALOG_CLOSE = "<!--/verbaly:catalog-->";
|
|
2144
|
-
const REDIRECT_OPEN = "<!--verbaly:redirect-->";
|
|
2145
|
-
const REDIRECT_CLOSE = "<!--/verbaly:redirect-->";
|
|
2146
|
-
const VOID = new Set(VOID_TAGS);
|
|
2147
|
-
const START_TAG = /<([a-zA-Z][a-zA-Z0-9-]*)((?:"[^"]*"|'[^']*'|[^"'>])*)>/g;
|
|
2148
|
-
const ATTR = /([^\s=/"'<>]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+)))?/g;
|
|
2149
|
-
function renderHtml(html, options) {
|
|
2150
|
-
const attr = options.attribute ?? "data-verbaly";
|
|
2151
|
-
const argsAttr = `${attr}-args`;
|
|
2152
|
-
const attrsAttr = `${attr}-attr`;
|
|
2153
|
-
const richAttr = `${attr}-rich`;
|
|
2154
|
-
const linksAttr = `${attr}-links`;
|
|
2155
|
-
const richTags = new Set(options.richTags ?? RICH_TAGS);
|
|
2156
|
-
const globalLinks = options.richLinks;
|
|
2157
|
-
const sourceLocale = options.sourceLocale ?? "en";
|
|
2158
|
-
const v = options.instance ?? localeInstance(options.catalogs, options.locale, sourceLocale);
|
|
2159
|
-
const t = v.t;
|
|
2160
|
-
const ms = new MagicString(html);
|
|
2161
|
-
const missing = /* @__PURE__ */ new Set();
|
|
2162
|
-
const used = /* @__PURE__ */ new Set();
|
|
2163
|
-
const skip = protectedRanges(html);
|
|
2164
|
-
const inSkip = (index) => skip.some(([from, to]) => index >= from && index < to);
|
|
2165
|
-
const filled = [];
|
|
2166
|
-
const inFilled = (index) => filled.some(([from, to]) => index >= from && index < to);
|
|
2167
|
-
const selfUrl = options.alternates?.find((a) => a.hreflang === options.locale)?.href;
|
|
2168
|
-
const sourceUrl = options.alternates?.find((a) => a.hreflang === "x-default")?.href;
|
|
2169
|
-
const rewriteUrl = selfUrl !== void 0 && selfUrl !== sourceUrl;
|
|
2170
|
-
START_TAG.lastIndex = 0;
|
|
2171
|
-
let m;
|
|
2172
|
-
while ((m = START_TAG.exec(html)) !== null) {
|
|
2173
|
-
if (inSkip(m.index) || inFilled(m.index)) continue;
|
|
2174
|
-
const [full, rawName, attrChunk] = m;
|
|
2175
|
-
const tagName = rawName.toLowerCase();
|
|
2176
|
-
const openEnd = m.index + full.length;
|
|
2177
|
-
const chunkStart = m.index + 1 + rawName.length;
|
|
2178
|
-
if (tagName === "html" && options.setLang !== false) {
|
|
2179
|
-
setAttribute(ms, html, chunkStart, openEnd, attrChunk, "lang", options.locale);
|
|
2180
|
-
setAttribute(ms, html, chunkStart, openEnd, attrChunk, "dir", localeDirection(options.locale));
|
|
2181
|
-
continue;
|
|
2182
|
-
}
|
|
2183
|
-
const attrs = parseAttrs(attrChunk);
|
|
2184
|
-
if (rewriteUrl && tagName === "link" && attrs.get("rel")?.toLowerCase() === "canonical" && decodeEntities(attrs.get("href") ?? "") === sourceUrl) setAttribute(ms, html, chunkStart, openEnd, attrChunk, "href", selfUrl);
|
|
2185
|
-
if (rewriteUrl && tagName === "meta" && attrs.get("property") === "og:url" && decodeEntities(attrs.get("content") ?? "") === sourceUrl) setAttribute(ms, html, chunkStart, openEnd, attrChunk, "content", selfUrl);
|
|
2186
|
-
const mirror = options.mirror;
|
|
2187
|
-
if (mirror) {
|
|
2188
|
-
if (tagName === "a" || tagName === "area") {
|
|
2189
|
-
const inside = mirroredHref(attrs.get("href"), mirror);
|
|
2190
|
-
if (inside) setAttribute(ms, html, chunkStart, openEnd, attrChunk, "href", inside);
|
|
2191
|
-
} else if (tagName === "meta" && attrs.get("http-equiv")?.toLowerCase() === "refresh") {
|
|
2192
|
-
const content = attrs.get("content") ?? "";
|
|
2193
|
-
const url = /^([^;]*;\s*url\s*=\s*)(.*)$/i.exec(decodeEntities(content));
|
|
2194
|
-
const inside = url && mirroredHref(url[2], mirror);
|
|
2195
|
-
if (inside) setAttribute(ms, html, chunkStart, openEnd, attrChunk, "content", `${url[1]}${inside}`);
|
|
2196
|
-
}
|
|
2197
|
-
}
|
|
2198
|
-
const key = attrs.get(attr);
|
|
2199
|
-
const attrMapRaw = attrs.get(attrsAttr);
|
|
2200
|
-
if (key === void 0 && attrMapRaw === void 0) continue;
|
|
2201
|
-
const args = parseArgs(attrs.get(argsAttr));
|
|
2202
|
-
if (key) {
|
|
2203
|
-
if (!v.has(key)) missing.add(key);
|
|
2204
|
-
else if (!VOID.has(tagName) && !attrChunk.trimEnd().endsWith("/")) {
|
|
2205
|
-
used.add(key);
|
|
2206
|
-
const close = findClose(html, tagName, openEnd, inSkip);
|
|
2207
|
-
if (close) {
|
|
2208
|
-
const text = t(key, args);
|
|
2209
|
-
const own = parseArgs(attrs.get(linksAttr));
|
|
2210
|
-
const links = own ? globalLinks ? {
|
|
2211
|
-
...globalLinks,
|
|
2212
|
-
...own
|
|
2213
|
-
} : own : globalLinks;
|
|
2214
|
-
const content = attrs.has(richAttr) ? richToHtml(parseTags(text), richTags, links) : escapeHtml(text);
|
|
2215
|
-
if (html.slice(openEnd, close.contentEnd) !== content) {
|
|
2216
|
-
if (openEnd === close.contentEnd) ms.appendLeft(openEnd, content);
|
|
2217
|
-
else ms.overwrite(openEnd, close.contentEnd, content);
|
|
2218
|
-
}
|
|
2219
|
-
filled.push([openEnd, close.contentEnd]);
|
|
2220
|
-
}
|
|
2221
|
-
}
|
|
2222
|
-
}
|
|
2223
|
-
if (attrMapRaw !== void 0) {
|
|
2224
|
-
const map = parseArgs(attrMapRaw);
|
|
2225
|
-
if (map) for (const [name, attrKey] of Object.entries(map)) {
|
|
2226
|
-
if (typeof attrKey !== "string") continue;
|
|
2227
|
-
if (!v.has(attrKey)) {
|
|
2228
|
-
missing.add(attrKey);
|
|
2229
|
-
continue;
|
|
2230
|
-
}
|
|
2231
|
-
used.add(attrKey);
|
|
2232
|
-
const value = safeAttribute(name, t(attrKey, args));
|
|
2233
|
-
if (value !== void 0) setAttribute(ms, html, chunkStart, openEnd, attrChunk, name, value);
|
|
2234
|
-
}
|
|
2235
|
-
}
|
|
2236
|
-
}
|
|
2237
|
-
if (options.alternates?.length) injectAlternates(ms, html, options.alternates);
|
|
2238
|
-
if (options.redirect) injectRedirect(ms, html, options.redirect);
|
|
2239
|
-
if (options.inlineCatalog && options.locale !== sourceLocale) {
|
|
2240
|
-
const slice = pageSlice(v, options.locale, used);
|
|
2241
|
-
if (Object.keys(slice).length > 0) injectCatalog(ms, html, slice);
|
|
2242
|
-
}
|
|
2243
|
-
return {
|
|
2244
|
-
html: ms.toString(),
|
|
2245
|
-
missing: [...missing],
|
|
2246
|
-
used: [...used]
|
|
2247
|
-
};
|
|
2248
|
-
}
|
|
2249
|
-
function pageSlice(v, locale, used) {
|
|
2250
|
-
const slice = {};
|
|
2251
|
-
for (const key of used) {
|
|
2252
|
-
const found = v.inspect(key);
|
|
2253
|
-
slice[key] = found?.from === locale ? found.source : "";
|
|
2254
|
-
}
|
|
2255
|
-
return slice;
|
|
2256
|
-
}
|
|
2257
|
-
function publicPath(rel) {
|
|
2258
|
-
const path = `/${rel.replace(/(^|\/)index\.html$/, "$1")}`;
|
|
2259
|
-
return path.length > 1 ? path.replace(/\/+$/, "") : path;
|
|
2028
|
+
return out;
|
|
2260
2029
|
}
|
|
2261
2030
|
function mirroredHref(raw, mirror) {
|
|
2262
2031
|
if (raw === void 0) return void 0;
|
|
@@ -2270,265 +2039,547 @@ function mirroredHref(raw, mirror) {
|
|
|
2270
2039
|
if (!mirror.pages.has(publicPath(path.replace(/^\//, "")))) return void 0;
|
|
2271
2040
|
return `${base}${mirror.prefix}${inside}`;
|
|
2272
2041
|
}
|
|
2273
|
-
function stripBase(path, base) {
|
|
2274
|
-
if (!path.startsWith(base)) return path;
|
|
2275
|
-
const rest = path.slice(base.length);
|
|
2276
|
-
if (rest === "") return "/";
|
|
2277
|
-
return rest.startsWith("/") ? rest : path;
|
|
2042
|
+
function stripBase(path, base) {
|
|
2043
|
+
if (!path.startsWith(base)) return path;
|
|
2044
|
+
const rest = path.slice(base.length);
|
|
2045
|
+
if (rest === "") return "/";
|
|
2046
|
+
return rest.startsWith("/") ? rest : path;
|
|
2047
|
+
}
|
|
2048
|
+
function normalizeBase(base) {
|
|
2049
|
+
const trimmed = base?.replace(/^\/+/, "").replace(/\/+$/, "");
|
|
2050
|
+
return trimmed ? `/${trimmed}` : "";
|
|
2051
|
+
}
|
|
2052
|
+
const REDIRECT_BODY = "(function(){var T=/^[a-z]{2,3}(-[a-z]{4})?(-([a-z]{2}|[0-9]{3}))?$/i;function M(t,g){t=String(t||\"\");if(S.indexOf(t)>=0)return t;if(g&&!T.test(t))return;while(t){var i=t.lastIndexOf(\"-\");if(i<0)return;t=t.slice(0,i);if(S.indexOf(t)>=0)return t}}var p=location.pathname;if(B){if(p.indexOf(B)!==0)return;p=p.slice(B.length)||\"/\";if(p.charAt(0)!==\"/\")return}if(M(p.split(\"/\")[1],1))return;var w;if(K)try{w=M(localStorage.getItem(K))}catch(e){}if(!w){var l=navigator.languages||[navigator.language];for(var i=0;i<l.length&&!w;i++)w=M(l[i])}if(!w||w===D)return;location.replace(B+\"/\"+w+p+location.search+location.hash)})()";
|
|
2053
|
+
function jsLiteral(value) {
|
|
2054
|
+
return JSON.stringify(value ?? null).replace(/</g, "\\u003c");
|
|
2055
|
+
}
|
|
2056
|
+
function redirectScript(options) {
|
|
2057
|
+
return `<script>var ${[
|
|
2058
|
+
`S=${jsLiteral(options.locales)}`,
|
|
2059
|
+
`D=${jsLiteral(options.sourceLocale)}`,
|
|
2060
|
+
`B=${jsLiteral(normalizeBase(options.base))}`,
|
|
2061
|
+
`K=${options.storageKey === false ? "null" : jsLiteral(options.storageKey ?? LOCALE_STORAGE_KEY)}`
|
|
2062
|
+
].join(",")};${REDIRECT_BODY};<\/script>`;
|
|
2063
|
+
}
|
|
2064
|
+
function injectRedirect(ms, html, options) {
|
|
2065
|
+
const block = `${REDIRECT_OPEN}${redirectScript(options)}${REDIRECT_CLOSE}`;
|
|
2066
|
+
const from = html.indexOf(REDIRECT_OPEN);
|
|
2067
|
+
if (from !== -1) {
|
|
2068
|
+
const to = html.indexOf(REDIRECT_CLOSE, from);
|
|
2069
|
+
if (to !== -1) {
|
|
2070
|
+
const end = to + 24;
|
|
2071
|
+
if (html.slice(from, end) !== block) ms.overwrite(from, end, block);
|
|
2072
|
+
return;
|
|
2073
|
+
}
|
|
2074
|
+
}
|
|
2075
|
+
const head = /<head\b(?:"[^"]*"|'[^']*'|[^"'>])*>/i.exec(html);
|
|
2076
|
+
if (head) ms.appendRight(head.index + head[0].length, block);
|
|
2077
|
+
}
|
|
2078
|
+
function injectAlternates(ms, html, alternates) {
|
|
2079
|
+
const links = alternates.map((a) => `<link rel="alternate" hreflang="${escapeAttr(a.hreflang)}" href="${escapeAttr(a.href)}">`).join("");
|
|
2080
|
+
const block = `${HREFLANG_OPEN}${links}${HREFLANG_CLOSE}`;
|
|
2081
|
+
const from = html.indexOf(HREFLANG_OPEN);
|
|
2082
|
+
if (from !== -1) {
|
|
2083
|
+
const to = html.indexOf(HREFLANG_CLOSE, from);
|
|
2084
|
+
if (to !== -1) {
|
|
2085
|
+
const end = to + 24;
|
|
2086
|
+
if (html.slice(from, end) !== block) ms.overwrite(from, end, block);
|
|
2087
|
+
return;
|
|
2088
|
+
}
|
|
2089
|
+
}
|
|
2090
|
+
const head = /<\/head\s*>/i.exec(html);
|
|
2091
|
+
if (head) ms.appendLeft(head.index, block);
|
|
2092
|
+
}
|
|
2093
|
+
function injectCatalog(ms, html, messages) {
|
|
2094
|
+
const json = JSON.stringify(messages).replace(/<\//g, "<\\/");
|
|
2095
|
+
const tag = `<script ${CATALOG_SCRIPT} type="application/json">${json}<\/script>`;
|
|
2096
|
+
const block = `${CATALOG_OPEN}${tag}${CATALOG_CLOSE}`;
|
|
2097
|
+
const from = html.indexOf(CATALOG_OPEN);
|
|
2098
|
+
if (from !== -1) {
|
|
2099
|
+
const to = html.indexOf(CATALOG_CLOSE, from);
|
|
2100
|
+
if (to !== -1) {
|
|
2101
|
+
const end = to + 23;
|
|
2102
|
+
if (html.slice(from, end) !== block) ms.overwrite(from, end, block);
|
|
2103
|
+
return;
|
|
2104
|
+
}
|
|
2105
|
+
}
|
|
2106
|
+
const head = /<\/head\s*>/i.exec(html);
|
|
2107
|
+
if (head) ms.appendLeft(head.index, block);
|
|
2108
|
+
}
|
|
2109
|
+
const TITLE_TAG = /<title\b([^>]*)>/i;
|
|
2110
|
+
const REFRESH_META = /<meta\b[^>]*http-equiv\s*=\s*["']?refresh/i;
|
|
2111
|
+
const NOINDEX_META = /<meta\b[^>]*name\s*=\s*["']?robots["']?[^>]*content\s*=\s*["'][^"']*noindex/i;
|
|
2112
|
+
function isIndexable(html) {
|
|
2113
|
+
return !REFRESH_META.test(html) && !NOINDEX_META.test(html);
|
|
2114
|
+
}
|
|
2115
|
+
function headIsBound(html, attr) {
|
|
2116
|
+
if (REFRESH_META.test(html)) return true;
|
|
2117
|
+
const title = TITLE_TAG.exec(html);
|
|
2118
|
+
return title ? title[1].includes(attr) : true;
|
|
2119
|
+
}
|
|
2120
|
+
async function renderSite(cfg, options = {}) {
|
|
2121
|
+
const site = join(cfg.root, options.site ?? cfg.render.site ?? "dist");
|
|
2122
|
+
const locales = options.locales ?? cfg.locales;
|
|
2123
|
+
const attribute = options.attribute ?? cfg.render.attribute;
|
|
2124
|
+
const base = normalizeBase(options.base ?? cfg.render.base);
|
|
2125
|
+
const baseUrl = (options.baseUrl ?? cfg.render.baseUrl)?.replace(/\/+$/, "");
|
|
2126
|
+
const wantHreflang = (options.hreflang ?? cfg.render.hreflang ?? true) && baseUrl !== void 0;
|
|
2127
|
+
const patterns = options.exclude ?? cfg.render.exclude ?? [];
|
|
2128
|
+
const excluded = patterns.length > 0 ? picomatch(patterns) : () => false;
|
|
2129
|
+
const sitemap = options.sitemap ?? cfg.render.sitemap ?? false;
|
|
2130
|
+
const wantSitemap = sitemap !== false && baseUrl !== void 0;
|
|
2131
|
+
const clean = options.clean ?? cfg.render.clean ?? false;
|
|
2132
|
+
const wanted = options.redirect ?? cfg.render.redirect ?? false;
|
|
2133
|
+
const redirect = wanted === false ? void 0 : wanted === true ? {} : wanted;
|
|
2134
|
+
const script = redirect && {
|
|
2135
|
+
locales,
|
|
2136
|
+
sourceLocale: cfg.sourceLocale,
|
|
2137
|
+
base,
|
|
2138
|
+
storageKey: redirect.storageKey
|
|
2139
|
+
};
|
|
2140
|
+
const catalogs = loadCatalogs(cfg);
|
|
2141
|
+
const instances = new Map(locales.map((locale) => [locale, localeInstance(catalogs, locale, cfg.sourceLocale)]));
|
|
2142
|
+
if (clean) {
|
|
2143
|
+
for (const locale of locales) if (locale !== cfg.sourceLocale) rmSync(join(site, locale), {
|
|
2144
|
+
recursive: true,
|
|
2145
|
+
force: true
|
|
2146
|
+
});
|
|
2147
|
+
}
|
|
2148
|
+
const files = await glob("**/*.html", {
|
|
2149
|
+
cwd: site,
|
|
2150
|
+
absolute: true,
|
|
2151
|
+
ignore: locales.map((locale) => `${locale}/**`)
|
|
2152
|
+
});
|
|
2153
|
+
const pages = new Set(files.map((file) => publicPath(relative(site, file).replace(/\\/g, "/"))));
|
|
2154
|
+
const missing = {};
|
|
2155
|
+
const untranslatedHead = [];
|
|
2156
|
+
const urls = [];
|
|
2157
|
+
for (const file of files) {
|
|
2158
|
+
const html = readFileSync(file, "utf8");
|
|
2159
|
+
const rel = relative(site, file).replace(/\\/g, "/");
|
|
2160
|
+
if (locales.length > 1 && !headIsBound(html, attribute ?? "data-verbaly")) untranslatedHead.push(rel);
|
|
2161
|
+
const alternates = wantHreflang ? pageAlternates(baseUrl, rel, locales, cfg.sourceLocale) : [];
|
|
2162
|
+
if (wantHreflang && isIndexable(html) && !excluded(rel)) urls.push({
|
|
2163
|
+
rel,
|
|
2164
|
+
alternates
|
|
2165
|
+
});
|
|
2166
|
+
const routed = script && (redirect.on === "all" || rel === "index.html");
|
|
2167
|
+
for (const locale of locales) {
|
|
2168
|
+
const result = renderHtml(html, {
|
|
2169
|
+
locale,
|
|
2170
|
+
instance: instances.get(locale),
|
|
2171
|
+
sourceLocale: cfg.sourceLocale,
|
|
2172
|
+
attribute,
|
|
2173
|
+
richTags: options.richTags,
|
|
2174
|
+
richLinks: options.richLinks ?? cfg.render.links,
|
|
2175
|
+
alternates,
|
|
2176
|
+
mirror: locale === cfg.sourceLocale ? void 0 : {
|
|
2177
|
+
prefix: `/${locale}`,
|
|
2178
|
+
base,
|
|
2179
|
+
pages
|
|
2180
|
+
},
|
|
2181
|
+
redirect: routed && locale === cfg.sourceLocale ? script : void 0,
|
|
2182
|
+
inlineCatalog: cfg.render.inlineCatalog === true
|
|
2183
|
+
});
|
|
2184
|
+
for (const key of result.missing) {
|
|
2185
|
+
const list = missing[locale] ??= [];
|
|
2186
|
+
if (!list.includes(key)) list.push(key);
|
|
2187
|
+
}
|
|
2188
|
+
const out = locale === cfg.sourceLocale ? file : join(site, locale, rel);
|
|
2189
|
+
mkdirSync(dirname(out), { recursive: true });
|
|
2190
|
+
writeFileSync(out, result.html);
|
|
2191
|
+
}
|
|
2192
|
+
}
|
|
2193
|
+
if (wantSitemap && urls.length) writeFileSync(join(site, typeof sitemap === "string" ? sitemap : "sitemap-i18n.xml"), buildSitemap(urls));
|
|
2194
|
+
return {
|
|
2195
|
+
files: files.length,
|
|
2196
|
+
locales: [...locales],
|
|
2197
|
+
missing,
|
|
2198
|
+
untranslatedHead
|
|
2199
|
+
};
|
|
2200
|
+
}
|
|
2201
|
+
function formatRenderWarnings(result, sourceLocale) {
|
|
2202
|
+
const lines = [];
|
|
2203
|
+
for (const [locale, keys] of Object.entries(result.missing)) lines.push(` ${locale}: ${counted(keys.length, "key")} not pre-filled: ${keys.join(", ")}`);
|
|
2204
|
+
if (result.untranslatedHead.length > 0) lines.push(` ${counted(result.untranslatedHead.length, "page")} of ${result.files} ship the same <title> in every locale (${result.untranslatedHead[0]}), so search results read in ${sourceLocale}`, ` fix: bind the head too, <title data-verbaly="key"> and a meta through data-verbaly-attr`);
|
|
2205
|
+
return lines;
|
|
2206
|
+
}
|
|
2207
|
+
function pagePath(rel) {
|
|
2208
|
+
const path = rel.replace(/(^|\/)index\.html$/, "$1");
|
|
2209
|
+
return path ? `/${path}` : "/";
|
|
2210
|
+
}
|
|
2211
|
+
function pageAlternates(baseUrl, rel, locales, sourceLocale) {
|
|
2212
|
+
return alternateLinks({
|
|
2213
|
+
supported: locales,
|
|
2214
|
+
sourceLocale,
|
|
2215
|
+
path: pagePath(rel),
|
|
2216
|
+
routing: "prefix-except-source",
|
|
2217
|
+
baseUrl
|
|
2218
|
+
});
|
|
2219
|
+
}
|
|
2220
|
+
function buildSitemap(urls) {
|
|
2221
|
+
return `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">${urls.flatMap(({ alternates }) => {
|
|
2222
|
+
const alts = alternates.map((a) => `<xhtml:link rel="alternate" hreflang="${a.hreflang}" href="${escapeAttr(a.href)}"/>`).join("");
|
|
2223
|
+
return alternates.filter((a) => a.hreflang !== "x-default").map((self) => `<url><loc>${escapeAttr(self.href)}</loc>${alts}</url>`);
|
|
2224
|
+
}).join("")}</urlset>\n`;
|
|
2225
|
+
}
|
|
2226
|
+
function parseAttrs(chunk) {
|
|
2227
|
+
const attrs = /* @__PURE__ */ new Map();
|
|
2228
|
+
ATTR.lastIndex = 0;
|
|
2229
|
+
let m;
|
|
2230
|
+
while ((m = ATTR.exec(chunk)) !== null) attrs.set(m[1].toLowerCase(), m[2] ?? m[3] ?? m[4] ?? "");
|
|
2231
|
+
return attrs;
|
|
2232
|
+
}
|
|
2233
|
+
function parseArgs(raw) {
|
|
2234
|
+
if (!raw) return void 0;
|
|
2235
|
+
try {
|
|
2236
|
+
return JSON.parse(decodeEntities(raw));
|
|
2237
|
+
} catch {
|
|
2238
|
+
console.warn(`[verbaly] invalid args JSON: ${raw}`);
|
|
2239
|
+
return;
|
|
2240
|
+
}
|
|
2241
|
+
}
|
|
2242
|
+
function protectedRanges(html) {
|
|
2243
|
+
const ranges = [];
|
|
2244
|
+
const re = /<!--[\s\S]*?-->|<script\b[\s\S]*?<\/script\s*>|<style\b[\s\S]*?<\/style\s*>/gi;
|
|
2245
|
+
let m;
|
|
2246
|
+
while ((m = re.exec(html)) !== null) {
|
|
2247
|
+
const openEnd = m[0].startsWith("<!--") ? m.index : html.indexOf(">", m.index) + 1;
|
|
2248
|
+
ranges.push([openEnd, m.index + m[0].length]);
|
|
2249
|
+
}
|
|
2250
|
+
return ranges;
|
|
2251
|
+
}
|
|
2252
|
+
function findClose(html, tagName, from, inSkip) {
|
|
2253
|
+
const re = new RegExp(`<${tagName}(?=[\\s/>])(?:"[^"]*"|'[^']*'|[^"'>])*>|</${tagName}\\s*>`, "gi");
|
|
2254
|
+
re.lastIndex = from;
|
|
2255
|
+
let depth = 1;
|
|
2256
|
+
let m;
|
|
2257
|
+
while ((m = re.exec(html)) !== null) {
|
|
2258
|
+
if (inSkip(m.index)) continue;
|
|
2259
|
+
if (m[0][1] === "/") {
|
|
2260
|
+
depth -= 1;
|
|
2261
|
+
if (depth === 0) return { contentEnd: m.index };
|
|
2262
|
+
} else if (!m[0].endsWith("/>")) depth += 1;
|
|
2263
|
+
}
|
|
2264
|
+
return null;
|
|
2265
|
+
}
|
|
2266
|
+
function setAttribute(ms, html, chunkStart, openEnd, attrChunk, name, value) {
|
|
2267
|
+
const escaped = escapeAttr(value);
|
|
2268
|
+
const existing = new RegExp(`(\\s${name}\\s*=\\s*)("[^"]*"|'[^']*'|[^\\s>]+)`, "i").exec(attrChunk);
|
|
2269
|
+
if (existing) {
|
|
2270
|
+
const valueStart = chunkStart + existing.index + existing[1].length;
|
|
2271
|
+
const valueEnd = valueStart + existing[2].length;
|
|
2272
|
+
if (html.slice(valueStart, valueEnd) !== `"${escaped}"`) ms.overwrite(valueStart, valueEnd, `"${escaped}"`);
|
|
2273
|
+
return;
|
|
2274
|
+
}
|
|
2275
|
+
const insertAt = openEnd - (html.slice(openEnd - 2, openEnd) === "/>" ? 2 : 1);
|
|
2276
|
+
ms.appendLeft(insertAt, ` ${name}="${escaped}"`);
|
|
2278
2277
|
}
|
|
2279
|
-
function
|
|
2280
|
-
|
|
2281
|
-
|
|
2278
|
+
function richToHtml(nodes, allowed, links) {
|
|
2279
|
+
let out = "";
|
|
2280
|
+
for (const node of nodes) if (typeof node === "string") out += escapeHtml(node);
|
|
2281
|
+
else if (links?.[node.name] !== void 0) {
|
|
2282
|
+
const { href, target, rel } = normalizeLink(links[node.name]);
|
|
2283
|
+
let attrs = href !== void 0 ? ` href="${escapeAttr(href)}"` : "";
|
|
2284
|
+
if (target) attrs += ` target="${escapeAttr(target)}"`;
|
|
2285
|
+
if (rel) attrs += ` rel="${escapeAttr(rel)}"`;
|
|
2286
|
+
out += `<a${attrs}>${richToHtml(node.children, allowed, links)}</a>`;
|
|
2287
|
+
} else if (allowed.has(node.name)) out += VOID.has(node.name) ? `<${node.name}>` : `<${node.name}>${richToHtml(node.children, allowed, links)}</${node.name}>`;
|
|
2288
|
+
else out += richToHtml(node.children, allowed, links);
|
|
2289
|
+
return out;
|
|
2282
2290
|
}
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
return JSON.stringify(value ?? null).replace(/</g, "\\u003c");
|
|
2291
|
+
function escapeHtml(text) {
|
|
2292
|
+
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
2286
2293
|
}
|
|
2287
|
-
function
|
|
2288
|
-
return
|
|
2289
|
-
`S=${jsLiteral(options.locales)}`,
|
|
2290
|
-
`D=${jsLiteral(options.sourceLocale)}`,
|
|
2291
|
-
`B=${jsLiteral(normalizeBase(options.base))}`,
|
|
2292
|
-
`K=${options.storageKey === false ? "null" : jsLiteral(options.storageKey ?? LOCALE_STORAGE_KEY)}`
|
|
2293
|
-
].join(",")};${REDIRECT_BODY};<\/script>`;
|
|
2294
|
+
function escapeAttr(text) {
|
|
2295
|
+
return escapeHtml(text).replace(/"/g, """);
|
|
2294
2296
|
}
|
|
2295
|
-
function
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2297
|
+
function decodeEntities(text) {
|
|
2298
|
+
return text.replace(/"/g, "\"").replace(/'/g, "'").replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&");
|
|
2299
|
+
}
|
|
2300
|
+
//#endregion
|
|
2301
|
+
//#region src/wrap.ts
|
|
2302
|
+
const WRAP_ATTRS = /* @__PURE__ */ new Set([
|
|
2303
|
+
"title",
|
|
2304
|
+
"alt",
|
|
2305
|
+
"placeholder",
|
|
2306
|
+
"aria-label"
|
|
2307
|
+
]);
|
|
2308
|
+
const CODE_TAGS = /* @__PURE__ */ new Set([
|
|
2309
|
+
"code",
|
|
2310
|
+
"pre",
|
|
2311
|
+
"kbd",
|
|
2312
|
+
"samp",
|
|
2313
|
+
"var"
|
|
2314
|
+
]);
|
|
2315
|
+
const T_NAMES = /* @__PURE__ */ new Set(["t"]);
|
|
2316
|
+
const LETTER = /\p{L}/u;
|
|
2317
|
+
const WHITESPACE = /\s/;
|
|
2318
|
+
async function wrapProject(cfg, options = {}) {
|
|
2319
|
+
const files = (await glob(cfg.include, {
|
|
2320
|
+
cwd: cfg.root,
|
|
2321
|
+
ignore: cfg.exclude,
|
|
2322
|
+
absolute: true
|
|
2323
|
+
})).filter((file) => /\.[jt]sx$/.test(file));
|
|
2324
|
+
const result = {
|
|
2325
|
+
files: files.length,
|
|
2326
|
+
changed: [],
|
|
2327
|
+
wrapped: [],
|
|
2328
|
+
skipped: [],
|
|
2329
|
+
blocked: []
|
|
2330
|
+
};
|
|
2331
|
+
for (const file of files) {
|
|
2332
|
+
const code = readFileSync(file, "utf8");
|
|
2333
|
+
const label = relative(cfg.root, file).replaceAll("\\", "/");
|
|
2334
|
+
const out = wrapCode(code, label);
|
|
2335
|
+
result.wrapped.push(...out.wrapped);
|
|
2336
|
+
result.skipped.push(...out.skipped);
|
|
2337
|
+
if (out.code === void 0) continue;
|
|
2338
|
+
result.changed.push(label);
|
|
2339
|
+
if (!out.hasT) {
|
|
2340
|
+
result.blocked.push({
|
|
2341
|
+
file: label,
|
|
2342
|
+
texts: out.wrapped.length,
|
|
2343
|
+
client: out.client
|
|
2344
|
+
});
|
|
2345
|
+
continue;
|
|
2304
2346
|
}
|
|
2347
|
+
if (options.write) writeFileSync(file, out.code);
|
|
2305
2348
|
}
|
|
2306
|
-
|
|
2307
|
-
if (head) ms.appendRight(head.index + head[0].length, block);
|
|
2349
|
+
return result;
|
|
2308
2350
|
}
|
|
2309
|
-
function
|
|
2310
|
-
const
|
|
2311
|
-
const
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2351
|
+
function wrapCode(code, file) {
|
|
2352
|
+
const wrapped = [];
|
|
2353
|
+
const skipped = [];
|
|
2354
|
+
let ast;
|
|
2355
|
+
try {
|
|
2356
|
+
ast = parse$1(code, {
|
|
2357
|
+
sourceType: "module",
|
|
2358
|
+
errorRecovery: true,
|
|
2359
|
+
plugins: ["typescript", "jsx"]
|
|
2360
|
+
}).program;
|
|
2361
|
+
} catch {
|
|
2362
|
+
return {
|
|
2363
|
+
wrapped,
|
|
2364
|
+
skipped,
|
|
2365
|
+
hasT: false,
|
|
2366
|
+
client: false
|
|
2367
|
+
};
|
|
2368
|
+
}
|
|
2369
|
+
const hasT = declaresT(ast);
|
|
2370
|
+
const client = hasDirective(ast, "use client");
|
|
2371
|
+
const s = new MagicString(code);
|
|
2372
|
+
const visited = /* @__PURE__ */ new Set();
|
|
2373
|
+
let changed = false;
|
|
2374
|
+
walk$1(ast, (node) => {
|
|
2375
|
+
if (node.type !== "JSXElement" && node.type !== "JSXFragment") return;
|
|
2376
|
+
if (visited.has(node)) return;
|
|
2377
|
+
processElement(node);
|
|
2378
|
+
});
|
|
2379
|
+
return changed ? {
|
|
2380
|
+
code: s.toString(),
|
|
2381
|
+
wrapped,
|
|
2382
|
+
skipped,
|
|
2383
|
+
hasT,
|
|
2384
|
+
client
|
|
2385
|
+
} : {
|
|
2386
|
+
wrapped,
|
|
2387
|
+
skipped,
|
|
2388
|
+
hasT,
|
|
2389
|
+
client
|
|
2390
|
+
};
|
|
2391
|
+
function processElement(node) {
|
|
2392
|
+
visited.add(node);
|
|
2393
|
+
if (node.type === "JSXElement") {
|
|
2394
|
+
const opening = node.openingElement;
|
|
2395
|
+
const name = opening.name;
|
|
2396
|
+
if (name.type === "JSXIdentifier" && name.name === "Trans") return markSubtree(node);
|
|
2397
|
+
if (hasVerbalyAttr(opening)) return markSubtree(node);
|
|
2398
|
+
if (name.type === "JSXIdentifier" && CODE_TAGS.has(name.name)) return markSubtree(node);
|
|
2399
|
+
wrapAttributes(opening);
|
|
2400
|
+
}
|
|
2401
|
+
const children = node.children ?? [];
|
|
2402
|
+
const hasElementChild = children.some((c) => c.type === "JSXElement" || c.type === "JSXFragment");
|
|
2403
|
+
const hasText = children.some((c) => c.type === "JSXText" && LETTER.test(c.value) || c.type === "JSXExpressionContainer" && c.expression.type === "StringLiteral" && LETTER.test(c.expression.value));
|
|
2404
|
+
if (hasElementChild && hasText) {
|
|
2405
|
+
skip(node, snippet(children), "mixed text and markup: wrap by hand or use <Trans>");
|
|
2406
|
+
return markSubtree(node);
|
|
2407
|
+
}
|
|
2408
|
+
if (hasText) {
|
|
2409
|
+
wrapSegment(node, children);
|
|
2410
|
+
return markSubtree(node);
|
|
2319
2411
|
}
|
|
2412
|
+
for (const child of children) if (child.type === "JSXElement" || child.type === "JSXFragment") processElement(child);
|
|
2320
2413
|
}
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
const from = html.indexOf(CATALOG_OPEN);
|
|
2329
|
-
if (from !== -1) {
|
|
2330
|
-
const to = html.indexOf(CATALOG_CLOSE, from);
|
|
2331
|
-
if (to !== -1) {
|
|
2332
|
-
const end = to + 23;
|
|
2333
|
-
if (html.slice(from, end) !== block) ms.overwrite(from, end, block);
|
|
2414
|
+
function wrapSegment(parent, children) {
|
|
2415
|
+
const meaningful = children.filter((c) => c.type === "JSXText" && c.value.trim() !== "" || c.type === "JSXExpressionContainer" && c.expression.type !== "JSXEmptyExpression");
|
|
2416
|
+
if (meaningful.length === 0) return;
|
|
2417
|
+
const first = meaningful[0];
|
|
2418
|
+
const last = meaningful[meaningful.length - 1];
|
|
2419
|
+
if (first.type === "JSXExpressionContainer" && meaningful.length > 1) {
|
|
2420
|
+
skip(parent, snippet(children), "starts with an interpolation: wrap the sentence by hand");
|
|
2334
2421
|
return;
|
|
2335
2422
|
}
|
|
2423
|
+
let template = "";
|
|
2424
|
+
let report = "";
|
|
2425
|
+
for (let i = children.indexOf(first); i <= children.indexOf(last); i++) {
|
|
2426
|
+
const child = children[i];
|
|
2427
|
+
if (child.type === "JSXText") {
|
|
2428
|
+
let value = cleanJsxText(child.value);
|
|
2429
|
+
if (child === first) value = value.trimStart();
|
|
2430
|
+
if (child === last) value = value.trimEnd();
|
|
2431
|
+
template += escapeTemplate(value);
|
|
2432
|
+
report += value;
|
|
2433
|
+
} else if (child.type === "JSXExpressionContainer") {
|
|
2434
|
+
const expr = child.expression;
|
|
2435
|
+
if (expr.type === "JSXEmptyExpression") continue;
|
|
2436
|
+
if (usesT(expr)) {
|
|
2437
|
+
skip(parent, snippet(children), "already uses t: finish it by hand");
|
|
2438
|
+
return;
|
|
2439
|
+
}
|
|
2440
|
+
if (containsJsx(expr)) {
|
|
2441
|
+
skip(parent, snippet(children), "an expression renders markup: wrap by hand or use <Trans>");
|
|
2442
|
+
markSubtree(child);
|
|
2443
|
+
return;
|
|
2444
|
+
}
|
|
2445
|
+
if (expr.type === "StringLiteral") {
|
|
2446
|
+
template += escapeTemplate(expr.value);
|
|
2447
|
+
report += expr.value;
|
|
2448
|
+
} else {
|
|
2449
|
+
const source = code.slice(expr.start, expr.end);
|
|
2450
|
+
template += "${" + source + "}";
|
|
2451
|
+
report += "${" + source + "}";
|
|
2452
|
+
}
|
|
2453
|
+
} else return;
|
|
2454
|
+
}
|
|
2455
|
+
if (!LETTER.test(report)) return;
|
|
2456
|
+
if (isIdentifierLike(report)) return;
|
|
2457
|
+
const start = first.type === "JSXText" ? first.start + leadingWs(first.value) : first.start;
|
|
2458
|
+
const end = last.type === "JSXText" ? last.end - trailingWs(last.value) : last.end;
|
|
2459
|
+
s.overwrite(start, end, "{t`" + template + "`}");
|
|
2460
|
+
changed = true;
|
|
2461
|
+
wrapped.push({
|
|
2462
|
+
file,
|
|
2463
|
+
line: line(first),
|
|
2464
|
+
text: report,
|
|
2465
|
+
kind: "text"
|
|
2466
|
+
});
|
|
2467
|
+
}
|
|
2468
|
+
function wrapAttributes(opening) {
|
|
2469
|
+
for (const attr of opening.attributes) {
|
|
2470
|
+
if (attr.type !== "JSXAttribute") continue;
|
|
2471
|
+
const name = attr.name;
|
|
2472
|
+
if (name.type !== "JSXIdentifier" || !WRAP_ATTRS.has(name.name)) continue;
|
|
2473
|
+
const value = attr.value;
|
|
2474
|
+
if (value?.type !== "StringLiteral") continue;
|
|
2475
|
+
const raw = value.value;
|
|
2476
|
+
if (!LETTER.test(raw) || isIdentifierLike(raw)) continue;
|
|
2477
|
+
s.overwrite(value.start, value.end, "{t`" + escapeTemplate(raw) + "`}");
|
|
2478
|
+
changed = true;
|
|
2479
|
+
wrapped.push({
|
|
2480
|
+
file,
|
|
2481
|
+
line: line(value),
|
|
2482
|
+
text: raw,
|
|
2483
|
+
kind: "attribute",
|
|
2484
|
+
attribute: name.name
|
|
2485
|
+
});
|
|
2486
|
+
}
|
|
2336
2487
|
}
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
function isIndexable(html) {
|
|
2344
|
-
return !REFRESH_META.test(html) && !NOINDEX_META.test(html);
|
|
2345
|
-
}
|
|
2346
|
-
function headIsBound(html, attr) {
|
|
2347
|
-
if (REFRESH_META.test(html)) return true;
|
|
2348
|
-
const title = TITLE_TAG.exec(html);
|
|
2349
|
-
return title ? title[1].includes(attr) : true;
|
|
2350
|
-
}
|
|
2351
|
-
async function renderSite(cfg, options = {}) {
|
|
2352
|
-
const site = join(cfg.root, options.site ?? cfg.render.site ?? "dist");
|
|
2353
|
-
const locales = options.locales ?? cfg.locales;
|
|
2354
|
-
const attribute = options.attribute ?? cfg.render.attribute;
|
|
2355
|
-
const base = normalizeBase(options.base ?? cfg.render.base);
|
|
2356
|
-
const baseUrl = (options.baseUrl ?? cfg.render.baseUrl)?.replace(/\/+$/, "");
|
|
2357
|
-
const wantHreflang = (options.hreflang ?? cfg.render.hreflang ?? true) && baseUrl !== void 0;
|
|
2358
|
-
const patterns = options.exclude ?? cfg.render.exclude ?? [];
|
|
2359
|
-
const excluded = patterns.length > 0 ? picomatch(patterns) : () => false;
|
|
2360
|
-
const sitemap = options.sitemap ?? cfg.render.sitemap ?? false;
|
|
2361
|
-
const wantSitemap = sitemap !== false && baseUrl !== void 0;
|
|
2362
|
-
const clean = options.clean ?? cfg.render.clean ?? false;
|
|
2363
|
-
const wanted = options.redirect ?? cfg.render.redirect ?? false;
|
|
2364
|
-
const redirect = wanted === false ? void 0 : wanted === true ? {} : wanted;
|
|
2365
|
-
const script = redirect && {
|
|
2366
|
-
locales,
|
|
2367
|
-
sourceLocale: cfg.sourceLocale,
|
|
2368
|
-
base,
|
|
2369
|
-
storageKey: redirect.storageKey
|
|
2370
|
-
};
|
|
2371
|
-
const catalogs = loadCatalogs(cfg);
|
|
2372
|
-
const instances = new Map(locales.map((locale) => [locale, localeInstance(catalogs, locale, cfg.sourceLocale)]));
|
|
2373
|
-
if (clean) {
|
|
2374
|
-
for (const locale of locales) if (locale !== cfg.sourceLocale) rmSync(join(site, locale), {
|
|
2375
|
-
recursive: true,
|
|
2376
|
-
force: true
|
|
2488
|
+
function skip(node, text, reason) {
|
|
2489
|
+
skipped.push({
|
|
2490
|
+
file,
|
|
2491
|
+
line: line(node),
|
|
2492
|
+
text,
|
|
2493
|
+
reason
|
|
2377
2494
|
});
|
|
2378
2495
|
}
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
2388
|
-
for (const file of files) {
|
|
2389
|
-
const html = readFileSync(file, "utf8");
|
|
2390
|
-
const rel = relative(site, file).replace(/\\/g, "/");
|
|
2391
|
-
if (locales.length > 1 && !headIsBound(html, attribute ?? "data-verbaly")) untranslatedHead.push(rel);
|
|
2392
|
-
const alternates = wantHreflang ? pageAlternates(baseUrl, rel, locales, cfg.sourceLocale) : [];
|
|
2393
|
-
if (wantHreflang && isIndexable(html) && !excluded(rel)) urls.push({
|
|
2394
|
-
rel,
|
|
2395
|
-
alternates
|
|
2496
|
+
function snippet(children) {
|
|
2497
|
+
const start = children[0]?.start ?? 0;
|
|
2498
|
+
const end = children[children.length - 1]?.end ?? start;
|
|
2499
|
+
const text = code.slice(start, end).replace(/\s+/g, " ").trim();
|
|
2500
|
+
return text.length > 60 ? text.slice(0, 59) + "…" : text;
|
|
2501
|
+
}
|
|
2502
|
+
function markSubtree(node) {
|
|
2503
|
+
walk$1(node, (n) => {
|
|
2504
|
+
if (n.type === "JSXElement" || n.type === "JSXFragment") visited.add(n);
|
|
2396
2505
|
});
|
|
2397
|
-
const routed = script && (redirect.on === "all" || rel === "index.html");
|
|
2398
|
-
for (const locale of locales) {
|
|
2399
|
-
const result = renderHtml(html, {
|
|
2400
|
-
locale,
|
|
2401
|
-
instance: instances.get(locale),
|
|
2402
|
-
sourceLocale: cfg.sourceLocale,
|
|
2403
|
-
attribute,
|
|
2404
|
-
richTags: options.richTags,
|
|
2405
|
-
richLinks: options.richLinks ?? cfg.render.links,
|
|
2406
|
-
alternates,
|
|
2407
|
-
mirror: locale === cfg.sourceLocale ? void 0 : {
|
|
2408
|
-
prefix: `/${locale}`,
|
|
2409
|
-
base,
|
|
2410
|
-
pages
|
|
2411
|
-
},
|
|
2412
|
-
redirect: routed && locale === cfg.sourceLocale ? script : void 0,
|
|
2413
|
-
inlineCatalog: cfg.render.inlineCatalog === true
|
|
2414
|
-
});
|
|
2415
|
-
for (const key of result.missing) {
|
|
2416
|
-
const list = missing[locale] ??= [];
|
|
2417
|
-
if (!list.includes(key)) list.push(key);
|
|
2418
|
-
}
|
|
2419
|
-
const out = locale === cfg.sourceLocale ? file : join(site, locale, rel);
|
|
2420
|
-
mkdirSync(dirname(out), { recursive: true });
|
|
2421
|
-
writeFileSync(out, result.html);
|
|
2422
|
-
}
|
|
2423
2506
|
}
|
|
2424
|
-
if (wantSitemap && urls.length) writeFileSync(join(site, typeof sitemap === "string" ? sitemap : "sitemap-i18n.xml"), buildSitemap(urls));
|
|
2425
|
-
return {
|
|
2426
|
-
files: files.length,
|
|
2427
|
-
locales: [...locales],
|
|
2428
|
-
missing,
|
|
2429
|
-
untranslatedHead
|
|
2430
|
-
};
|
|
2431
2507
|
}
|
|
2432
|
-
function
|
|
2433
|
-
const
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
return
|
|
2508
|
+
function isIdentifierLike(text) {
|
|
2509
|
+
const trimmed = text.trim();
|
|
2510
|
+
if (WHITESPACE.test(trimmed)) return false;
|
|
2511
|
+
const first = [...trimmed].find((char) => LETTER.test(char));
|
|
2512
|
+
return first !== void 0 && first !== first.toUpperCase();
|
|
2437
2513
|
}
|
|
2438
|
-
function
|
|
2439
|
-
|
|
2440
|
-
return path ? `/${path}` : "/";
|
|
2514
|
+
function hasDirective(program, value) {
|
|
2515
|
+
return (program.directives ?? []).some((d) => d.value?.value === value);
|
|
2441
2516
|
}
|
|
2442
|
-
function
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2517
|
+
function declaresT(program) {
|
|
2518
|
+
let found = false;
|
|
2519
|
+
walk$1(program, (node) => {
|
|
2520
|
+
if (found) return;
|
|
2521
|
+
if (node.type === "ImportDeclaration") {
|
|
2522
|
+
for (const spec of node.specifiers) if (isTName(spec.local?.name)) found = true;
|
|
2523
|
+
return;
|
|
2524
|
+
}
|
|
2525
|
+
if (node.type === "VariableDeclarator" && bindsT(node.id)) found = true;
|
|
2526
|
+
if (node.type === "FunctionDeclaration" && isTName(node.id?.name)) found = true;
|
|
2527
|
+
if (node.params?.some((param) => bindsT(param))) found = true;
|
|
2449
2528
|
});
|
|
2529
|
+
return found;
|
|
2450
2530
|
}
|
|
2451
|
-
function
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2531
|
+
function bindsT(pattern) {
|
|
2532
|
+
if (!pattern) return false;
|
|
2533
|
+
if (pattern.type === "Identifier") return isTName(pattern.name);
|
|
2534
|
+
if (pattern.type === "AssignmentPattern") return bindsT(pattern.left);
|
|
2535
|
+
if (pattern.type === "ObjectPattern") return pattern.properties.some((prop) => prop.type === "RestElement" ? bindsT(prop.argument) : bindsT(prop.value));
|
|
2536
|
+
if (pattern.type === "ArrayPattern") return pattern.elements.some((el) => bindsT(el ?? void 0));
|
|
2537
|
+
return false;
|
|
2456
2538
|
}
|
|
2457
|
-
function
|
|
2458
|
-
|
|
2459
|
-
ATTR.lastIndex = 0;
|
|
2460
|
-
let m;
|
|
2461
|
-
while ((m = ATTR.exec(chunk)) !== null) attrs.set(m[1].toLowerCase(), m[2] ?? m[3] ?? m[4] ?? "");
|
|
2462
|
-
return attrs;
|
|
2539
|
+
function isTName(name) {
|
|
2540
|
+
return typeof name === "string" && T_NAMES.has(name);
|
|
2463
2541
|
}
|
|
2464
|
-
function
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
return;
|
|
2471
|
-
}
|
|
2542
|
+
function hasVerbalyAttr(opening) {
|
|
2543
|
+
return opening.attributes.some((attr) => {
|
|
2544
|
+
if (attr.type !== "JSXAttribute") return false;
|
|
2545
|
+
const name = attr.name;
|
|
2546
|
+
return name.type === "JSXIdentifier" && name.name.startsWith("data-verbaly");
|
|
2547
|
+
});
|
|
2472
2548
|
}
|
|
2473
|
-
function
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
}
|
|
2481
|
-
return ranges;
|
|
2549
|
+
function usesT(node) {
|
|
2550
|
+
let found = false;
|
|
2551
|
+
walk$1(node, (n) => {
|
|
2552
|
+
if (n.type === "TaggedTemplateExpression" && isTReference(n.tag, T_NAMES)) found = true;
|
|
2553
|
+
if (n.type === "CallExpression" && isTReference(n.callee, T_NAMES)) found = true;
|
|
2554
|
+
});
|
|
2555
|
+
return found;
|
|
2482
2556
|
}
|
|
2483
|
-
function
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
if (inSkip(m.index)) continue;
|
|
2490
|
-
if (m[0][1] === "/") {
|
|
2491
|
-
depth -= 1;
|
|
2492
|
-
if (depth === 0) return { contentEnd: m.index };
|
|
2493
|
-
} else if (!m[0].endsWith("/>")) depth += 1;
|
|
2494
|
-
}
|
|
2495
|
-
return null;
|
|
2557
|
+
function containsJsx(node) {
|
|
2558
|
+
let found = false;
|
|
2559
|
+
walk$1(node, (n) => {
|
|
2560
|
+
if (n.type === "JSXElement" || n.type === "JSXFragment") found = true;
|
|
2561
|
+
});
|
|
2562
|
+
return found;
|
|
2496
2563
|
}
|
|
2497
|
-
function
|
|
2498
|
-
|
|
2499
|
-
const existing = new RegExp(`(\\s${name}\\s*=\\s*)("[^"]*"|'[^']*'|[^\\s>]+)`, "i").exec(attrChunk);
|
|
2500
|
-
if (existing) {
|
|
2501
|
-
const valueStart = chunkStart + existing.index + existing[1].length;
|
|
2502
|
-
const valueEnd = valueStart + existing[2].length;
|
|
2503
|
-
if (html.slice(valueStart, valueEnd) !== `"${escaped}"`) ms.overwrite(valueStart, valueEnd, `"${escaped}"`);
|
|
2504
|
-
return;
|
|
2505
|
-
}
|
|
2506
|
-
const insertAt = openEnd - (html.slice(openEnd - 2, openEnd) === "/>" ? 2 : 1);
|
|
2507
|
-
ms.appendLeft(insertAt, ` ${name}="${escaped}"`);
|
|
2564
|
+
function escapeTemplate(text) {
|
|
2565
|
+
return text.replace(/[\\`]/g, "\\$&").replace(/\$\{/g, "\\${");
|
|
2508
2566
|
}
|
|
2509
|
-
function
|
|
2510
|
-
|
|
2511
|
-
for (const node of nodes) if (typeof node === "string") out += escapeHtml(node);
|
|
2512
|
-
else if (links?.[node.name] !== void 0) {
|
|
2513
|
-
const { href, target, rel } = normalizeLink(links[node.name]);
|
|
2514
|
-
let attrs = href !== void 0 ? ` href="${escapeAttr(href)}"` : "";
|
|
2515
|
-
if (target) attrs += ` target="${escapeAttr(target)}"`;
|
|
2516
|
-
if (rel) attrs += ` rel="${escapeAttr(rel)}"`;
|
|
2517
|
-
out += `<a${attrs}>${richToHtml(node.children, allowed, links)}</a>`;
|
|
2518
|
-
} else if (allowed.has(node.name)) out += VOID.has(node.name) ? `<${node.name}>` : `<${node.name}>${richToHtml(node.children, allowed, links)}</${node.name}>`;
|
|
2519
|
-
else out += richToHtml(node.children, allowed, links);
|
|
2520
|
-
return out;
|
|
2567
|
+
function leadingWs(text) {
|
|
2568
|
+
return text.length - text.trimStart().length;
|
|
2521
2569
|
}
|
|
2522
|
-
function
|
|
2523
|
-
return text.
|
|
2570
|
+
function trailingWs(text) {
|
|
2571
|
+
return text.length - text.trimEnd().length;
|
|
2524
2572
|
}
|
|
2525
|
-
function
|
|
2526
|
-
return
|
|
2573
|
+
function line(node) {
|
|
2574
|
+
return node.loc?.start?.line ?? 1;
|
|
2527
2575
|
}
|
|
2528
|
-
|
|
2529
|
-
|
|
2576
|
+
//#endregion
|
|
2577
|
+
//#region src/run.ts
|
|
2578
|
+
function formatCliError(error) {
|
|
2579
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
2580
|
+
return message.startsWith("[verbaly]") ? message : `[verbaly] ${message}`;
|
|
2530
2581
|
}
|
|
2531
2582
|
//#endregion
|
|
2532
|
-
export { LOCALE_MODULE_PREFIX, MessageRegistry, RESOLVED_VIRTUAL_ID, SOURCE_FILE_RE, check, clientCatalogs, collectOrigins, counted, createSourceFilter, doctor, effectiveDrafts, extractProject, formatCheckResult, formatCheckWarnings, formatDoctorEntry, formatRenderWarnings, formatStatusResult, formatTranslateFailures, generateDts, generateLocaleModule, generateRuntimeModule, init, isTransformTarget, loadCatalogs, loadConfig, loadDrafts, loadVirtualModule, markDrafts, needsIcu, needsRelative, pruneCatalogs, renderSite, resolveConfig, resolveProvider, resolveVirtualId, runBuildGate, saveDrafts, stableKey, status, syncCatalogs, transformCode, transformSource, translateCatalogs, wrapProject, writeCatalog, writeDts };
|
|
2583
|
+
export { DRAFTS_FILE, LOCALE_MODULE_PREFIX, MessageRegistry, RESOLVED_VIRTUAL_ID, SOURCE_FILE_RE, check, clearDrafts, clientCatalogs, collectOrigins, counted, createSourceFilter, doctor, effectiveDrafts, extractProject, formatCheckResult, formatCheckWarnings, formatCliError, formatDoctorEntry, formatRenderWarnings, formatStatusResult, formatTranslateFailures, generateDts, generateLocaleModule, generateRuntimeModule, init, isTransformTarget, loadCatalogs, loadConfig, loadDrafts, loadVirtualModule, markDrafts, needsIcu, needsRelative, pruneCatalogs, readCatalog, renderSite, resolveConfig, resolveProvider, resolveVirtualId, runBuildGate, saveDrafts, stableKey, status, syncCatalogs, targetLocales, transformCode, transformSource, translateCatalogs, validateMessage, validatePair, wrapProject, writeCatalog, writeDts };
|
|
2533
2584
|
|
|
2534
2585
|
//# sourceMappingURL=index.js.map
|