Skip to content

Commit 36b5d33

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

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

src/util.ts

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
import { glob, readFile, readdir, unlink, writeFile } from "fs/promises"
2-
import { join } from "path"
1+
import { glob, readFile, readdir, unlink, writeFile } from "fs/promises";
2+
import { join } from "path";
3+
4+
5+
6+
import type { Browser } from "playwright";
7+
8+
9+
10+
import { ARCHIVE_DIR, HOOK_SCRIPT, SCRIPT, ZIP_SCRIPT } from "./constants.js";
11+
12+
313

4-
import type { Browser } from "playwright"
514

6-
import { ARCHIVE_DIR, HOOK_SCRIPT, SCRIPT, ZIP_SCRIPT } from "./constants.js"
715

816
export async function getGeminiIdsFromMarkdowns(): Promise<Set<string>> {
917
const ids = new Set<string>()
@@ -97,13 +105,36 @@ export async function archiveConversation(browser: Browser, id: string) {
97105
removeFrames: true,
98106
insertSingleFileComment: true
99107
})
100-
const fileContent = pageData.content.replaceAll(/@font-face\s*{[^}]*}/g, (fontFaceRule: string) => {
101-
const fontFamilyMatch = fontFaceRule.match(/font-family:\s*(?<quote>['"]?)(?<fontFamily>[^'"]+)\k<quote>/)
102-
const fontFamily = fontFamilyMatch?.groups?.["fontFamily"]?.trim() ?? ""
103108

104-
if (includesKatex && fontFamily.startsWith("KaTeX")) return fontFaceRule
105-
else return ""
106-
})
109+
const variablesUsedInDocument = new Set(
110+
// Variable values could contain other values, so /var\(([^\)]+)/g won't work
111+
// e.g. --a: var(--b, var(--c));
112+
Array.from(pageData.content.matchAll(/var\s*\(\s*(?<variableName>--[a-zA-z0-9\-]+)/g)).map(
113+
regExpExecArray => regExpExecArray.groups!["variableName"]!
114+
)
115+
)
116+
117+
const fileContent = pageData.content
118+
// Remove Google Sans family and Google Symbol font face
119+
.replaceAll(/@font-face\s*{[^}]*}/g, (fontFaceRule: string) => {
120+
const fontFamilyMatch = fontFaceRule.match(/font-family:\s*(?<quote>['"]?)(?<fontFamily>[^'"]+)\k<quote>/)
121+
const fontFamily = fontFamilyMatch?.groups?.["fontFamily"]?.trim() ?? ""
122+
123+
if (includesKatex && fontFamily.startsWith("KaTeX")) return fontFaceRule
124+
else return ""
125+
})
126+
.replaceAll(
127+
// --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+
}
137+
)
107138

108139
// Remove illegal filename chars
109140
const sanitizedTitle = title.replace(/[\\/:*?"<>|\n]/g, "").substring(0, 100)
@@ -126,4 +157,4 @@ export function buildCommitMessage(added: string[], deleted: string[]): string {
126157
if (deleted.length > 0) msg += `\nDeleted conversations: ${deleted.join(", ")}`
127158

128159
return msg
129-
}
160+
}

0 commit comments

Comments
 (0)