Skip to content

📚 The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.

Try to add video preview in markdown (failed)

Dedmen Millerb34aad32cbd6
diff --git a/frontend/app/helpers/markdown/index.ts b/frontend/app/helpers/markdown/index.ts
index 4b2605b8..c2c3a4a2 100644
--- a/frontend/app/helpers/markdown/index.ts
+++ b/frontend/app/helpers/markdown/index.ts
@@ -8,6 +8,7 @@ import { advancedBlocks } from './cards';
// @ts-expect-error no types provided
import underline from 'markdown-it-underline';
import highlight from 'markdown-it-highlightjs';
+//import html5Media from 'markdown-it-html5-media'; // Doesn't work, always not found
// @ts-expect-error no types provided
import mark from 'markdown-it-mark';
import katex from 'katex';
@@ -29,6 +30,8 @@ md.use(colorPlugin, {
allowHex: true, // allow #rgb and #rrggbb
allowedNamedColors: ['red', 'crimson', 'rebeccapurple'], // restrict named colors (null = all standard names)
});
+//md.use(html5Media);
+md.use(imageVideoHandler);
function markdownItKatexPlugin(md: any) {
md.inline.ruler.after('escape', 'katex', (state: any, silent: any) => {
@@ -120,8 +123,63 @@ function markdownItCheckbox(md: any) {
}
});
}
-// Définir une règle de rendu pour injecter les cases à cocher
+function imageVideoHandler(md: any) {
+ const oldImageRenderer = md.renderer.rules.image;
+//debugger; // This is triggered
+ md.renderer.rules['image'] = function (tokens, idx, options, env, self) {
+//debugger; // This never triggers
+
+ const validAudioExtensions = ['aac', 'm4a', 'mp3', 'oga', 'ogg', 'wav'];
+ const validVideoExtensions = ['mp4', 'm4v', 'ogv', 'webm', 'mpg', 'mpeg'];
+
+ const guessMediaType = (url) => {
+ const extensionMatch = url.match(/\.([^/.]+)$/);
+ if (extensionMatch === null)
+ return 'image';
+ const extension = extensionMatch[1];
+ if (validAudioExtensions.indexOf(extension.toLowerCase()) != -1)
+ return 'audio';
+ else if (validVideoExtensions.indexOf(extension.toLowerCase()) != -1)
+ return 'video';
+ else
+ return 'image';
+ }
+
+ const renderMedia = (tokens, idx, options, env, md) => {
+ const token = tokens[idx];
+ const type = token.type;
+ if (type !== 'video' && type !== 'audio')
+ return '';
+ let attrs = ' controls'; // Enforce showing controls
+
+ // We'll always have a URL for non-image media: they are detected by URL
+ const url = token.attrs[token.attrIndex('src')][1];
+
+ // Title is set like this: ![descriptive text](video.mp4 "title")
+ const title = token.attrIndex('title') != -1 ?
+ ` title="${md.utils.escapeHtml(token.attrs[token.attrIndex('title')][1])}"` :
+ '';
+
+ const description = token.content ? md.utils.escapeHtml(token.content) : '';
+
+ return `<${type} src="${url}"${title}${attrs}>\n` +
+ `${description}\n` +
+ `</${type}>`;
+ }
+
+ const mediaType = guessMediaType(href);
+ if (mediaType == 'video' || mediaType == 'audio')
+ {
+ tokens[idx].type = mediaType;
+ return renderMedia(tokens, idx, options, env, self);
+ };
+
+ return oldImageRenderer(tokens, idx, options, env, self);
+ }
+}
+
+// Définir une règle de rendu pour injecter les cases à cocher
export default function compile(str: string = ''): string {
// Replace &lt; &gt; &amp; to < > & (to avoid markdown-it escape)
str = str.replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&amp;/g, '&');
diff --git a/frontend/bun.lock b/frontend/bun.lock
index 2933a54d..b695811c 100644
--- a/frontend/bun.lock
+++ b/frontend/bun.lock
@@ -31,6 +31,7 @@
"markdown-it-highlightjs": "^4.2.0",
"markdown-it-mark": "^4.0.0",
"markdown-it-underline": "^1.0.1",
+ "markdown-it-html5-media": "^0.8.0",
"nuxt": "^4.2.2",
"pdfjs-dist": "^5.4.530",
"pinia": "^3.0.4",
@@ -1660,6 +1661,8 @@
"markdown-it-underline": ["[email protected]", "", {}, "sha512-J597ni39vPHIH1ONVZoDvQKUUXkOqoB93bm6Fc/5Deu6XaWMXrT0xf2m2r9qZCA8dncWJ5V8d5PyGkpmQuy/vg=="],
+ "markdown-it-html5-media": ["[email protected]", "", {}, "sha512-D3jTRc9LWLuivVn5+U13eT2ErSVB05v46ETukSheREnt+SLaK6aWh6w2y4HsYv3MmN5smDHlb+5P6WG+3ArbKw=="],
+
"math-intrinsics": ["[email protected]", "", {}, "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g=="],
"mathml-tag-names": ["[email protected]", "", {}, "sha512-APMBEanjybaPzUrfqU0IMU5I0AswKMH7k8OTLs0vvV4KZpExkTkY87nR/zpbuTPj+gARop7aGUbl11pnDfW6xg=="],
diff --git a/frontend/package.json b/frontend/package.json
index 62eaf507..ba2e526c 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -44,6 +44,7 @@
"markdown-it-highlightjs": "^4.2.0",
"markdown-it-mark": "^4.0.0",
"markdown-it-underline": "^1.0.1",
+ "markdown-it-html5-media": "^0.8.0",
"nuxt": "^4.2.2",
"pdfjs-dist": "^5.4.530",
"pinia": "^3.0.4",