Skip to content

Commit f7595c5

Browse files
committed
Update util.ts
1 parent 36b5d33 commit f7595c5

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/util.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ export async function archiveConversation(browser: Browser, id: string) {
9292
// Script tags
9393
const scriptTags = document.getElementsByTagName("script")
9494
while (scriptTags.length > 0) scriptTags[0]!.remove()
95+
96+
// Remove inline CSS variables to make the later step of removing unused CSS variables easier
97+
// <div style="--a: 0px"> ...
98+
// <div style='--a: 0px'> ...
99+
for (const elWithStyleAttribute of document.querySelectorAll("[style]")) {
100+
if (elWithStyleAttribute.getAttribute("style")!.includes("--")) elWithStyleAttribute.removeAttribute("style")
101+
}
95102
})
96103

97104
// https://github.com/gildas-lormeau/single-file-cli/blob/v2.0.75/single-file-cli-api.js#L258
@@ -115,24 +122,21 @@ export async function archiveConversation(browser: Browser, id: string) {
115122
)
116123

117124
const fileContent = pageData.content
118-
// Remove Google Sans family and Google Symbol font face
125+
// Remove fonts
119126
.replaceAll(/@font-face\s*{[^}]*}/g, (fontFaceRule: string) => {
120127
const fontFamilyMatch = fontFaceRule.match(/font-family:\s*(?<quote>['"]?)(?<fontFamily>[^'"]+)\k<quote>/)
121128
const fontFamily = fontFamilyMatch?.groups?.["fontFamily"]?.trim() ?? ""
122129

123130
if (includesKatex && fontFamily.startsWith("KaTeX")) return fontFaceRule
124131
else return ""
125132
})
133+
// Remove unused CSS variables
126134
.replaceAll(
127135
// --a: 0px; } .class { ...
128-
// <div style="--a: 0px"> ...
129-
// <div style='--a: 0px'> ...
130-
/(?<variableName>--[a-zA-Z0-9\-]+)\s*:\s*(?<value>[^;\n\}]+)\s*[;\n]?(?<curlyBrace>\})?(?<quote>["'])?/gm,
131-
(_match, variableName: string, value: string, curlyBrace: string | undefined, quote: string | undefined) => {
132-
const suffix = `${curlyBrace ?? ""}${quote ?? ""}`
133-
console.log(_match, suffix)
134-
if (variablesUsedInDocument.has(variableName)) return `${variableName}:${value};${suffix}`
135-
return suffix
136+
/(?<variableName>--[a-zA-Z0-9\-]+)\s*:\s*(?<value>[^;\n\}]+)\s*[;\n]?(?<curlyBrace>\})?/gm,
137+
(_match, variableName: string, value: string, curlyBrace: string | undefined = "") => {
138+
if (variablesUsedInDocument.has(variableName)) return `${variableName}:${value};${curlyBrace}`
139+
return curlyBrace
136140
}
137141
)
138142

0 commit comments

Comments
 (0)