-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcss.js
More file actions
153 lines (124 loc) · 4.2 KB
/
Copy pathcss.js
File metadata and controls
153 lines (124 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { read, write, fs, path } from "./node_utils.js";
import subsetFont from "subset-font";
const config = read("./pages/config.json");
async function downloadAndSubset(fontUrl, fontPath) {
try {
console.log(`Fetching CSS from: ${fontUrl}`);
const cssResponse = await fetch(fontUrl);
const cssText = await cssResponse.text();
const ttfUrl = cssText.match(/url\((.*?)\)/)[1].replace(/['"]/g, "");
const fontResponse = await fetch(ttfUrl);
const arrayBuffer = await fontResponse.arrayBuffer();
const inputBuffer = Buffer.from(arrayBuffer);
fs.writeFileSync(fontPath, inputBuffer);
let characters = "";
for (let i = 32; i <= 255; i++) characters += String.fromCharCode(i);
const subsetBuffer = await subsetFont(inputBuffer, characters, {
targetFormat: "woff2",
});
// Use the specific filename passed in
fs.writeFileSync(fontPath, subsetBuffer);
console.log(`Saved: ${fontPath}`);
} catch (e) {
console.log(e);
}
}
function camelCase(str) {
return str
.split(" ")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join("");
}
export async function getFontCSS(theme) {
const fonts = [theme.headingFont, theme.bodyFont];
let preloads = [];
for (const fontName of fonts) {
const fileName = `${camelCase(fontName)}.woff2`;
const fontPath = `./docs/public/${fileName}`;
if (!fs.existsSync(fontPath)) {
// Corrected the URL construction
const googleUrl = `https://fonts.googleapis.com/css2?family=${fontName.replace(/\s+/g, "+")}&subset=latin,latin-ext&display=swap`;
await downloadAndSubset(googleUrl, fontPath);
}
preloads.push(["link", { rel: "preload", href: `/${fileName}`, as: "font", type: "font/woff2", crossorigin: "" }]);
}
return { preloads };
}
const getHue = (hex) => {
try {
const r = parseInt(hex.slice(1, 3), 16) / 255;
const g = parseInt(hex.slice(3, 5), 16) / 255;
const b = parseInt(hex.slice(5, 7), 16) / 255;
const max = Math.max(r, g, b),
min = Math.min(r, g, b),
d = max - min;
if (d === 0) return 0;
let h = max === r ? (g - b) / d + (g < b ? 6 : 0) : max === g ? (b - r) / d + 2 : (r - g) / d + 4;
return h * 60;
} catch (e) {
return 0;
}
};
export async function printCSS() {
let css = `/* AUTOGENERATED, DO NOT EDIT MANUALLY, SEE css.js */
@import "tailwindcss";
@plugin "@tailwindcss/typography";
@theme {
/* === Your core parameters === */
--font-body: '${config.theme.bodyFont}', sans-serif;
--font-heading: '${config.theme.headingFont}', sans-serif;
--color-accent: ${config.theme.accentColor};
--color-primary: ${config.theme.accentPrimary};
--accent-angle: ${getHue(config.theme.accentColor)}deg;
--primary-angle: ${getHue(config.theme.primaryColor)}deg;
/* Apply directly to Tailwind font vars */
--font-sans: var(--font-body);
--font-display: var(--font-heading);
}
@font-face {
font-family: '${config.theme.bodyFont}';
font-style: normal;
font-weight: 400;
font-display: swap;
src: url(/${camelCase(config.theme.bodyFont)}.woff2);
}
@font-face {
font-family: '${config.theme.headingFont}';
font-style: normal;
font-weight: 400;
font-display: swap;
src: url(/${camelCase(config.theme.headingFont)}.woff2);
}
/* You can also include other global styles */
body {
font-family: var(--font-body);
}
h1, h2, h3, h4, h5, h6 {
font-family: var(--font-heading);
}
.legal * {
opacity: 1 !important; /* fully visible */
color: black !important;
transform: scale(1) rotate(0) translate(0) !important; /* no scaling or translation */
}
@keyframes scrolled {
to {
opacity: 1; /* fully visible */
transform: scale(1) rotate(0) translate(0); /* no scaling or translation */
}
}\n\n`;
config.theme.styles?.forEach(({ selector, cssClass, scroll }) => {
if (scroll) {
css += `${selector} {
${cssClass};
animation: scrolled linear both;
animation-timeline: view();
animation-range: entry 30% cover 30%;
}\n\n`;
} else {
css += `${selector} { ${cssClass}; }\n\n`;
}
});
const baseDir = path.resolve("");
fs.writeFileSync(baseDir + "/docs/.vitepress/theme/style.css", css, "utf8");
}