Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions desktop-app/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,24 @@ console.log("✓ Copied assets/ → resources/assets/ (excluding GIF demos)");
/**
* Validates the cryptographic integrity of a file against an expected SHA-384 hash.
*/
function verifyIntegrity(filePath, expectedSha384) {
function verifyIntegrity(filePath, expectedHash) {
return new Promise((resolve, reject) => {
if (!expectedSha384) {
if (!expectedHash) {
resolve(true); // Skip validation if no hash is provided (e.g., relative fonts)
return;
}

const hash = crypto.createHash("sha384");
const algo = expectedHash.startsWith("sha512-") ? "sha512" : "sha384";
const hash = crypto.createHash(algo);
const stream = fs.createReadStream(filePath);

stream.on("data", data => hash.update(data));
stream.on("end", () => {
const calculated = "sha384-" + hash.digest("base64");
if (calculated === expectedSha384) {
const calculated = algo + "-" + hash.digest("base64");
if (calculated === expectedHash) {
resolve(true);
} else {
reject(new Error(`Integrity mismatch for ${path.basename(filePath)}:\nExpected: ${expectedSha384}\nCalculated: ${calculated}`));
reject(new Error(`Integrity mismatch for ${path.basename(filePath)}:\nExpected: ${expectedHash}\nCalculated: ${calculated}`));
}
});
stream.on("error", reject);
Expand Down Expand Up @@ -187,6 +188,19 @@ async function prepareOfflineDependencies() {
downloads.push(downloadFile("https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/fonts/bootstrap-icons.woff2", path.join(fontDir, "bootstrap-icons.woff2"), null));
downloads.push(downloadFile("https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/fonts/bootstrap-icons.woff", path.join(fontDir, "bootstrap-icons.woff"), null));

// Dynamic / Lazy-loaded dependencies to download manually for offline capability
const dynamicLibs = [
{
url: "https://cdnjs.cloudflare.com/ajax/libs/abcjs/6.5.2/abcjs-basic-min.js",
dest: path.join(LIBS_DIR, "abcjs-basic-min.js"),
hash: "sha512-QJ21PAOSw5KSiQ12gnP74qwLRAEn9GZtrFI0yY1akCLLpcEaC7xwZ7BiONZ/7pyrfUADyh7sHnI3SYHifO+tmg=="
}
];

for (const lib of dynamicLibs) {
downloads.push(downloadFile(lib.url, lib.dest, lib.hash));
}

// Wait for all downloads and cryptographic validations to finish
try {
await Promise.all(downloads);
Expand Down
7 changes: 7 additions & 0 deletions desktop-app/resources/js/preview-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
let librariesLoaded = false;
let markedConfigured = false;
let mermaidIdCounter = 0;
let abcIdCounter = 0;

const markedOptions = {
gfm: true,
Expand Down Expand Up @@ -312,6 +313,11 @@ function configureMarked() {
return `<div class="mermaid-container is-loading"><div class="mermaid" id="${uniqueId}" data-original-code="${encodeURIComponent(code)}">${escapeHtml(code)}</div></div>`;
}

if (language === "abc") {
const uniqueId = `abc-notation-worker-${abcIdCounter++}`;
return `<div class="abc-container is-loading"><div class="abc-notation" id="${uniqueId}" data-original-code="${encodeURIComponent(code)}">${escapeHtml(code)}</div></div>`;
}

const validLanguage = hljs && hljs.getLanguage(language) ? language : "plaintext";
const highlightedCode = hljs
? hljs.highlight(code, { language: validLanguage }).value
Expand Down Expand Up @@ -481,6 +487,7 @@ self.onmessage = function(event) {
const options = data.options || {};
ensureLibraries(options.libraryUrls || {});
mermaidIdCounter = 0;
abcIdCounter = 0;
const result = renderSegmentedMarkdown(data.markdown || "", options);
self.postMessage({
type: "render-result",
Expand Down
Loading
Loading