astrotype 0.1.5 → 0.1.6

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.
@@ -18,6 +18,38 @@ function toCssRules(ruleMap) {
18
18
  .join("\n\n");
19
19
  }
20
20
 
21
+ function fontFamilyFallback(font) {
22
+ const raw =
23
+ font?.font?.provider === "google"
24
+ ? String(font?.font?.family || font?.title || "")
25
+ : String(font?.title || "");
26
+ const normalized = raw.replace(/ Variable$/, "").replace(/"/g, '\\"');
27
+ return `"${normalized}"`;
28
+ }
29
+
30
+ function withPairingFontFallbacks(pairing, fonts) {
31
+ const headingFont = fonts[0];
32
+ const bodyFont = fonts[1];
33
+ const monoFont = fonts[2];
34
+ const css = JSON.parse(JSON.stringify(pairing.css || {}));
35
+ const root = { ...css[":root"] };
36
+
37
+ if (headingFont) {
38
+ const variable = headingFont.font?.variable || "--font-heading";
39
+ root["--font-heading"] = `var(${variable}, ${fontFamilyFallback(headingFont)})`;
40
+ }
41
+ if (bodyFont) {
42
+ const variable = bodyFont.font?.variable || "--font-body";
43
+ root["--font-body"] = `var(${variable}, ${fontFamilyFallback(bodyFont)})`;
44
+ }
45
+ if (monoFont) {
46
+ const variable = monoFont.font?.variable || "--font-mono";
47
+ root["--font-mono"] = `var(${variable}, ${fontFamilyFallback(monoFont)})`;
48
+ }
49
+ css[":root"] = root;
50
+ return css;
51
+ }
52
+
21
53
  function toUtilityClassRules() {
22
54
  return `.font-heading {
23
55
  font-family: var(--font-heading);
@@ -55,7 +87,8 @@ export async function addPairingCommand(pairingName, options = {}) {
55
87
  process.exit(1);
56
88
  }
57
89
 
58
- const cssText = `${toCssRules(pairing.css)}
90
+ const cssRulesWithFallbacks = withPairingFontFallbacks(pairing, fonts);
91
+ const cssText = `${toCssRules(cssRulesWithFallbacks)}
59
92
 
60
93
  ${toUtilityClassRules()}
61
94
  `;
@@ -38,10 +38,14 @@ function toAstroFontDescriptor(font) {
38
38
  if (provider === "npm") {
39
39
  const npmPackage = font.font.npmPackage || font.font.package;
40
40
  if (!npmPackage) return null;
41
- let file = null;
42
41
  if (npmPackage === "geist") {
43
- if (font.name === "geist") file = "font/sans.css";
44
- if (font.name === "geist-mono") file = "font/mono.css";
42
+ return {
43
+ name: font.name,
44
+ familyName: font.name === "geist-mono" ? "Geist Mono" : "Geist",
45
+ cssVariable,
46
+ provider: "google",
47
+ options: null,
48
+ };
45
49
  }
46
50
  return {
47
51
  name: font.name,
@@ -50,7 +54,6 @@ function toAstroFontDescriptor(font) {
50
54
  provider: "npm",
51
55
  options: {
52
56
  package: npmPackage,
53
- ...(file ? { file } : {}),
54
57
  },
55
58
  };
56
59
  }
@@ -58,11 +61,15 @@ function toAstroFontDescriptor(font) {
58
61
  }
59
62
 
60
63
  function serializeAstroFontDescriptor(descriptor) {
64
+ const providerCall =
65
+ descriptor.provider === "npm"
66
+ ? 'fontProviders.npm({ cdn: "https://cdn.jsdelivr.net/npm/" })'
67
+ : `fontProviders.${descriptor.provider}()`;
61
68
  const optionsLine = descriptor.options
62
69
  ? `,\n options: ${JSON.stringify(descriptor.options)}`
63
70
  : "";
64
71
  return ` {
65
- provider: fontProviders.${descriptor.provider}(),
72
+ provider: ${providerCall},
66
73
  name: ${JSON.stringify(descriptor.familyName)},
67
74
  cssVariable: ${JSON.stringify(descriptor.cssVariable)}${optionsLine}
68
75
  }`;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "astrotype",
3
3
  "description": "Astro-first font registry and CLI for installing pairings or single fonts.",
4
4
  "type": "module",
5
- "version": "0.1.5",
5
+ "version": "0.1.6",
6
6
  "private": false,
7
7
  "keywords": [
8
8
  "astro",