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.

Support html 5 videos, audio and iframe (eg: youtube integration)

Smaug673915bf02943dd1
diff --git a/backend/utils/escape-html.go b/backend/utils/escape-html.go
index c2b20329..761bcdf4 100644
--- a/backend/utils/escape-html.go
+++ b/backend/utils/escape-html.go
@@ -85,6 +85,13 @@ func InitBluemonday() {
// Disable automatic rel="nofollow"
policy.RequireNoFollowOnLinks(false)
+ // Images videos audio
+ policy.AllowAttrs("src", "alt", "title", "width", "height").OnElements("img")
+ policy.AllowAttrs("src", "controls", "alt").OnElements("audio", "video")
+
+ // Iframe (use with caution)
+ policy.AllowAttrs("src", "width", "height", "allow", "allowfullscreen", "frameborder").OnElements("iframe")
+
// -------------------------------
// ELEMENTS THAT MUST HAVE NO ATTRIBUTES
// -------------------------------
diff --git a/frontend/app/helpers/markdown/index.ts b/frontend/app/helpers/markdown/index.ts
index 1d818a13..9b911bce 100644
--- a/frontend/app/helpers/markdown/index.ts
+++ b/frontend/app/helpers/markdown/index.ts
@@ -10,7 +10,7 @@ import highlight from 'markdown-it-highlightjs';
// @ts-expect-error no types provided
import mark from 'markdown-it-mark';
import colorPlugin from './colors';
-import { subscriptPlugin, superscriptPlugin, footNotePlugin } from './other';
+import { subscriptPlugin, superscriptPlugin, footNotePlugin, html5MediaPlugin } from './other';
import { markdownItCheckbox } from './checkbox';
import { markdownItKatexPlugin } from './katex';
@@ -32,6 +32,7 @@ md.use(colorPlugin, {
enableBracketSyntax: true, // also parse [text]{color=value}
allowHex: true, // allow #rgb and #rrggbb
});
+md.use(html5MediaPlugin);
export default function compile(str: string = ''): string {
// Replace &lt; &gt; &amp; to < > & (to avoid markdown-it escape)
diff --git a/frontend/app/helpers/markdown/other.ts b/frontend/app/helpers/markdown/other.ts
index 48e87d6e..e76610eb 100644
--- a/frontend/app/helpers/markdown/other.ts
+++ b/frontend/app/helpers/markdown/other.ts
@@ -127,4 +127,30 @@ function footNotePlugin(md: MarkdownIt) {
};
}
-export { superscriptPlugin, subscriptPlugin, footNotePlugin };
+const VIDEO_REGEX = /\.(mp4|webm|ogg|mov|avi|mkv|flv|wmv)(\?.*)?$/i;
+const AUDIO_REGEX = /\.(mp3|wav|ogg|m4a|flac|aac)(\?.*)?$/i;
+
+function html5MediaPlugin(md: MarkdownIt) {
+ // Handle <video> and <audio> tags on ![alt](src)
+ const defaultImageRenderer =
+ md.renderer.rules.image ||
+ function (tokens, idx, options, env, self) {
+ return self.renderToken(tokens, idx, options);
+ };
+
+ md.renderer.rules.image = (tokens, idx, options, env, self) => {
+ const token = tokens[idx];
+ if (!token) return '';
+ const src = token.attrGet('src') || '';
+ const alt = token.content || '';
+
+ if (src.match(VIDEO_REGEX)) {
+ return `<video controls alt="${md.utils.escapeHtml(alt)}" src="${md.utils.escapeHtml(src)}"></video>`;
+ } else if (src.match(AUDIO_REGEX)) {
+ return `<audio controls alt="${md.utils.escapeHtml(alt)}" src="${md.utils.escapeHtml(src)}"></audio>`;
+ }
+ return defaultImageRenderer(tokens, idx, options, env, self);
+ };
+}
+
+export { superscriptPlugin, subscriptPlugin, footNotePlugin, html5MediaPlugin };
diff --git a/frontend/app/styles/theme.scss b/frontend/app/styles/theme.scss
index a8fc5b90..8eb8fbd6 100644
--- a/frontend/app/styles/theme.scss
+++ b/frontend/app/styles/theme.scss
@@ -101,6 +101,22 @@ img {
margin-bottom: 5px;
}
+audio,
+video {
+ display: block;
+ width: 100%;
+ margin: 10px 0;
+ border-radius: 6px;
+ background: var(--bg-ui);
+}
+
+iframe {
+ max-width: 100%;
+ border: none;
+ border-radius: 6px;
+ margin: 10px 0;
+}
+
input,
select,
textarea {