-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix(media): serve and render .md files with inline Markdown preview #6277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
eac6c3c
9aa79b4
df044c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2499,6 +2499,7 @@ const _SVG_EXTS=/\.svg$/i; | |||||||||||
| const _AUDIO_EXTS=/\.(mp3|ogg|wav|m4a|aac|flac|wma|opus|webm|oga)$/i; | ||||||||||||
| const _VIDEO_EXTS=/\.(mp4|webm|mkv|mov|avi|ogv|m4v)$/i; | ||||||||||||
| const _CSV_EXTS=/\.csv$/i; | ||||||||||||
| const _MD_EXTS=/\.(md|mkd|mkdn)$/i; | ||||||||||||
| const _EXCALIDRAW_EXTS=/\.excalidraw$/i; | ||||||||||||
| // ── Media playback speed controls ───────────────────────────────────────── | ||||||||||||
| const MEDIA_PLAYBACK_RATES=[0.5,0.75,1,1.25,1.5,2]; | ||||||||||||
|
|
@@ -2661,6 +2662,9 @@ function _inlineMediaHtmlForRef(ref, sessionId, altText){ | |||||||||||
| if(_HTML_EXTS.test(ref)){ | ||||||||||||
| return `<div class="html-preview-load" data-path="${esc(ref)}"><span class="html-preview-spinner">⏳</span> ${esc(typeof t==='function'?t('html_loading'):'Loading')}...</div>`; | ||||||||||||
| } | ||||||||||||
| if(_MD_EXTS.test(ref)){ | ||||||||||||
| return `<div class="md-inline-load" data-path="${esc(ref)}"><span class="md-preview-spinner">⏳</span> ${esc(typeof t==='function'?t('md_loading'):'Loading')}...</div>`; | ||||||||||||
| } | ||||||||||||
| const fname=esc(ref.split('/').pop()||ref); | ||||||||||||
| if(/\.(patch|diff)$/i.test(ref)) return `<div class="diff-inline-load" data-path="${esc(ref)}">${esc(typeof t==='function'?t('diff_loading'):'Loading diff')} ${fname}...</div>`; | ||||||||||||
| if(_CSV_EXTS.test(ref)) return `<div class="csv-inline-load" data-path="${esc(ref)}">${esc(typeof t==='function'?t('csv_loading'):'Loading')} ${fname}...</div>`; | ||||||||||||
|
|
@@ -18087,6 +18091,7 @@ function postProcessRenderedMessages(container) { | |||||||||||
| loadExcalidrawInline(container); | ||||||||||||
| loadPdfInline(container); | ||||||||||||
| loadHtmlInline(container); | ||||||||||||
| loadMarkdownInline(container); | ||||||||||||
| renderMermaidBlocks(container); | ||||||||||||
| renderKatexBlocks(container); | ||||||||||||
| initTreeViews(container); | ||||||||||||
|
|
@@ -18654,6 +18659,34 @@ function loadHtmlInline(container){ | |||||||||||
| }); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| const MD_MAX_SIZE = 256 * 1024; | ||||||||||||
|
|
||||||||||||
| function loadMarkdownInline(container){ | ||||||||||||
|
Comment on lines
+18662
to
+18664
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||||||||
| const root = container || document; | ||||||||||||
| root.querySelectorAll('.md-inline-load:not([data-loaded])').forEach(el => { | ||||||||||||
| el.setAttribute('data-loaded', '1'); | ||||||||||||
| const path = el.dataset.path; | ||||||||||||
| const fname = path.split('/').pop() || path; | ||||||||||||
| const mediaSessionId = (typeof S !== 'undefined' && S && S.session && S.session.session_id) ? String(S.session.session_id) : ''; | ||||||||||||
| const publicMediaUrl = 'api/media?path=' + encodeURIComponent(path); | ||||||||||||
| const mediaUrl = publicMediaUrl + (mediaSessionId ? '&session_id=' + encodeURIComponent(mediaSessionId) : ''); | ||||||||||||
| const downloadUrl = mediaUrl + '&download=1'; | ||||||||||||
| fetch(mediaUrl) | ||||||||||||
|
greptile-apps[bot] marked this conversation as resolved.
|
||||||||||||
| .then(r => { if (!r.ok) throw new Error(r.status); return r.text(); }) | ||||||||||||
| .then(text => { | ||||||||||||
| if (text.length > MD_MAX_SIZE) { | ||||||||||||
| el.outerHTML = `<div class="md-inline-fallback"><a class="msg-media-link" href="${esc(downloadUrl)}" download="${esc(fname)}">📎 ${esc(fname)}</a><br><span style="color:var(--muted);font-size:12px">${esc(typeof t === 'function' ? t('md_too_large') : 'File too large for preview')}</span></div>`; | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
| const rendered = renderMd(text); | ||||||||||||
| el.outerHTML = `<div class="md-inline-wrap"><div class="md-inline-header"><span class="md-preview-title">${esc(fname)}</span><a class="msg-media-link" href="${esc(downloadUrl)}" download="${esc(fname)}">📎 ${esc(fname)}</a></div><div class="md-inline-content">${rendered}</div></div>`; | ||||||||||||
| }) | ||||||||||||
| .catch(() => { | ||||||||||||
| el.outerHTML = `<div class="md-inline-fallback"><a class="msg-media-link" href="${esc(downloadUrl)}" download="${esc(fname)}">📎 ${esc(fname)}</a><br><span style="color:var(--muted);font-size:12px">${esc(typeof t === 'function' ? t('md_error') : 'Error loading markdown')}</span></div>`; | ||||||||||||
| }); | ||||||||||||
| }); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| function renderMermaidBlocks(container){ | ||||||||||||
| const root=container||document; | ||||||||||||
| const blocks=root.querySelectorAll('.mermaid-block:not([data-rendered])'); | ||||||||||||
|
|
||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
t('md_loading'),t('md_too_large'), andt('md_error')are called throughout_inlineMediaHtmlForRefandloadMarkdownInline, but none of these keys are registered ini18n.js. Thet()function's final fallback (line 26001 ofi18n.js:if (val === undefined) return key;) means every user will literally see"md_loading"in the loading spinner,"md_too_large"in the size-cap path, and"md_error"in the catch block — permanently, not just transiently. Comparehtml_loading,html_too_large, andhtml_error, which are defined across all supported locales. The hardcoded English strings in the ternary ('File too large for preview','Loading','Error loading markdown') are dead code becausetis always a function in production.