Skip to content
Open
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
85 changes: 12 additions & 73 deletions webapp/static/js/repo-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,6 @@ async function renderMarkdownPreview(content) {
await loadMarkdownDependencies();
}

await ensureHighlightJsLoaded();

if (typeof MarkdownLiveRenderer !== 'undefined' && MarkdownLiveRenderer.isSupported()) {
// רינדור ה-Markdown ל-HTML
const html = await MarkdownLiveRenderer.render(content);
Expand Down Expand Up @@ -342,80 +340,21 @@ function enhanceMarkdownFallback(root) {
*/
async function loadMarkdownDependencies() {
const scripts = [
{
src: 'https://cdn.jsdelivr.net/npm/markdown-it@14/dist/markdown-it.min.js',
isReady: () => typeof window.markdownit === 'function'
},
{
src: 'https://cdn.jsdelivr.net/npm/highlight.js@11/highlight.min.js',
isReady: () => window.hljs && typeof window.hljs.highlightElement === 'function'
}
'https://cdn.jsdelivr.net/npm/markdown-it@14/dist/markdown-it.min.js',
'https://cdn.jsdelivr.net/npm/highlight.js@11/highlight.min.js'
];

for (const { src, isReady } of scripts) {
await loadExternalScript(src, isReady);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Race condition in concurrent script loading logic

Medium Severity

The simplified loadMarkdownDependencies function only checks if a script element exists in the DOM but doesn't verify the script has actually finished loading. When called concurrently (e.g., rapid navigation between markdown files), a second call may find the script element added by the first call and skip waiting, even though the script hasn't loaded yet. This causes code to proceed before window.markdownit or window.hljs are available, resulting in users seeing raw markdown text instead of properly rendered content. The removed scriptLoadPromises caching and isReady callbacks specifically prevented this race condition.

Fix in Cursor Fix in Web

}
}

async function ensureHighlightJsLoaded() {
if (window.hljs && typeof window.hljs.highlightElement === 'function') {
return;
}
await loadExternalScript(
'https://cdn.jsdelivr.net/npm/highlight.js@11/highlight.min.js',
() => window.hljs && typeof window.hljs.highlightElement === 'function'
);
}

const scriptLoadPromises = new Map();

async function loadExternalScript(src, isReady) {
const readyCheck = typeof isReady === 'function' ? isReady : null;
if (readyCheck && readyCheck()) {
return;
}
if (scriptLoadPromises.has(src)) {
return scriptLoadPromises.get(src);
}

const existing = document.querySelector(`script[src="${src}"]`);
if (existing) {
if (readyCheck && readyCheck()) {
existing.dataset.loaded = '1';
return;
}
if (existing.dataset.loaded === '1') {
return;
}
if (!readyCheck && (existing.readyState === 'complete' || existing.readyState === 'loaded')) {
existing.dataset.loaded = '1';
return;
for (const src of scripts) {
if (!document.querySelector(`script[src="${src}"]`)) {
await new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = src;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
const existingPromise = new Promise((resolve, reject) => {
existing.addEventListener('load', () => {
existing.dataset.loaded = '1';
resolve();
}, { once: true });
existing.addEventListener('error', (event) => {
reject(event);
}, { once: true });
});
scriptLoadPromises.set(src, existingPromise);
return existingPromise;
}

const promise = new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = src;
script.onload = () => {
script.dataset.loaded = '1';
resolve();
};
script.onerror = reject;
document.head.appendChild(script);
});
scriptLoadPromises.set(src, promise);
return promise;
}
}

// ========================================
Expand Down
Loading