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.

Add copy code button to block code and improve color theme

Smaug6739bf996f2eb838
diff --git a/frontend/app/helpers/markdown/code-block.ts b/frontend/app/helpers/markdown/code-block.ts
new file mode 100644
index 00000000..cdd90398
--- /dev/null
+++ b/frontend/app/helpers/markdown/code-block.ts
@@ -0,0 +1,42 @@
+import type MarkdownIt from 'markdown-it';
+
+/**
+ * Markdown-it plugin to add a "copy to clipboard" button to code blocks.
+ * @param md - The MarkdownIt instance
+ */
+export const copyCodePlugin = (md: MarkdownIt) => {
+ // Sauvegarde du renderer par défaut des blocs <pre><code>
+ const defaultFence = md.renderer.rules.fence;
+
+ md.renderer.rules.fence = function (tokens, idx, options, env, self) {
+ const token = tokens[idx];
+ const rawCode = token?.content || '';
+
+ // HTML original rendu par markdown-it
+ const original = defaultFence ? defaultFence(tokens, idx, options, env, self) : self.renderToken(tokens, idx, options);
+
+ // On enveloppe dans un container pour pouvoir placer le bouton
+ return `
+<div class="code-block-wrapper">
+ <button class="code-copy-btn" data-code="${md.utils.escapeHtml(rawCode)}" aria-label="Copy code">
+ ${COPY_ICON}
+ </button>
+ ${original}
+</div>
+ `;
+ };
+};
+
+// Icon svg "copy"
+const COPY_ICON = `<svg xmlns="http://www.w3.org/2000/svg" height="18px" viewBox="0 -960 960 960" width="18px"><path d="M160-80q-33 0-56.5-23.5T80-160v-480q0-33 23.5-56.5T160-720h80v-80q0-33 23.5-56.5T320-880h480q33 0 56.5 23.5T880-800v480q0 33-23.5 56.5T800-240h-80v80q0 33-23.5 56.5T640-80H160Zm160-240h480v-480H320v480Z"/></svg>`;
+
+document.addEventListener('click', e => {
+ const btn = (e.target as HTMLElement)?.closest('.code-copy-btn') as HTMLButtonElement | null;
+ if (!btn) return;
+
+ const code = btn.dataset.code ?? '';
+ navigator.clipboard.writeText(code);
+
+ btn.classList.add('copied');
+ setTimeout(() => btn.classList.remove('copied'), 800);
+});
diff --git a/frontend/app/helpers/markdown/index.ts b/frontend/app/helpers/markdown/index.ts
index cd200493..54cd6c30 100644
--- a/frontend/app/helpers/markdown/index.ts
+++ b/frontend/app/helpers/markdown/index.ts
@@ -3,6 +3,7 @@ import MarkdownIt from 'markdown-it';
import { containerPlugin } from './container';
import { anchorPlugin } from './anchor';
import { containerSvg } from './containers-svg';
+import { copyCodePlugin } from './code-block';
// @ts-expect-error no types provided
import underline from 'markdown-it-underline';
import highlight from 'markdown-it-highlightjs';
@@ -15,6 +16,7 @@ const md = new MarkdownIt({ html: true });
md.use(containerPlugin);
md.use(anchorPlugin);
md.use(containerSvg);
+md.use(copyCodePlugin);
md.use(underline);
md.use(mark);
md.use(markdownItKatexPlugin);
diff --git a/frontend/app/styles/code.scss b/frontend/app/styles/code.scss
index cb655517..8f60f32d 100644
--- a/frontend/app/styles/code.scss
+++ b/frontend/app/styles/code.scss
@@ -1,135 +1,150 @@
-/* stylelint-disable */
pre {
display: inline-block;
width: 100%;
margin: 12px 0;
border-radius: 8px;
- background: #3b4244;
+ background: #292d3e;
word-break: break-all;
}
+/* Inline code */
code {
margin: 2px;
- padding: 3px;
+ padding: 3px 6px;
border-radius: 5px;
color: var(--font-color);
- background: var(--bg-contrast);
+ background: rgba(255, 255, 255, 0.06);
}
+/* Block code */
pre code.hljs {
display: block;
font-family: 'JetBrains Mono', monospace;
font-size: 14px;
overflow-x: auto;
+ padding: 1em;
+ background: #292d3e;
+ color: #abb2bf;
}
-
-
-pre>code,
-code.hljs {
- padding: 0.75em;
- color: #fff;
- background: #3b4244;
- text-wrap: wrap;
-}
-
+/* Base */
.hljs {
- color: #aabbcf;
+ color: #abb2bf;
}
+/* Comments / punctuation */
.hljs-comment,
-.hljs-punctuation,
-.hljs-tag {
- color: #7a7e85
+.hljs-quote {
+ color: #676e95;
+ font-style: italic;
}
+.hljs-tag,
+.hljs-punctuation {
+ color: #abb2bf;
+}
-
-.hljs-attribute,
-.hljs-doctag,
+/* Keywords & core language */
.hljs-keyword,
.hljs-meta .hljs-keyword,
-.hljs-name,
-.hljs-selector-tag {
- color: #398ee7;
+.hljs-selector-tag,
+.hljs-doctag,
+.hljs-name {
+ color: #89ddff;
}
-.hljs-deletion,
-.hljs-quote,
+/* Class / ID selectors */
.hljs-selector-class,
.hljs-selector-id,
-.hljs-template-tag,
.hljs-type {
- color: #79d984
+ color: #56b6c2;
+ /* turquoise élégant */
}
-.hljs-number {
- color: yellow
+/* Literals & builtins */
+.hljs-literal,
+.hljs-built_in {
+ color: #56b6c2;
}
-.hljs-string {
- color: #b492ec
+/* Attributes */
+.hljs-attribute,
+.hljs-attr {
+ color: #f07178;
+ /* beige/or très lisible */
}
-.hljs-section,
-.hljs-title {
- font-weight: 700;
- color: #79d984
+/* Numbers */
+.hljs-number {
+ color: #d19a66;
+ /* orange foncé */
}
-.hljs-title.function_ {
- color: #f84
+/* Strings */
+.hljs-string,
+.hljs-meta .hljs-string {
+ color: #c3e88d;
+ /* vert frais et lisible */
}
-.hljs-attr,
-.hljs-params {
- color: #ffa500
+/* Functions */
+.hljs-title.function_ {
+ color: #61afef;
+ /* bleu moderne */
}
-.hljs-variable {
- color: cyan;
+/* Classes */
+.hljs-title.class_ {
+ color: #82aaff;
}
-.hljs-title.class_ {
- color: #6a7be6
+/* Variables */
+.hljs-variable {
+ color: #e06c75;
+ /* rouge lisible, pas agressif */
}
-.hljs-link,
-.hljs-operator,
+/* RegExp / operators / misc */
.hljs-regexp,
+.hljs-operator,
+.hljs-symbol,
.hljs-selector-attr,
.hljs-selector-pseudo,
-.hljs-symbol {
- color: #ffa500
+.hljs-link {
+ color: #d19a66;
}
-.hljs-property {
- color: #f84
+/* Sections / Titles */
+.hljs-section,
+.hljs-title {
+ font-weight: 600;
+ color: #61afef;
}
-.hljs-addition,
-.hljs-bullet,
-.hljs-code {
- color: #5da873
+/* Diff additions/deletions */
+.hljs-addition {
+ color: #98c379;
}
-.hljs-built_in,
-.hljs-literal {
- color: #79d984;
+.hljs-deletion {
+ color: #e06c75;
}
-.hljs-meta {
- color: #1f7199
+.hljs-bullet,
+.hljs-code {
+ color: #98c379;
}
-.hljs-meta .hljs-string {
- color: #38a
+/* Meta */
+.hljs-meta {
+ color: #61afef;
}
+/* Emphasis / Strong */
.hljs-emphasis {
- font-style: italic
+ font-style: italic;
}
.hljs-strong {
- font-weight: 700
+ font-weight: 700;
}
\ No newline at end of file
diff --git a/frontend/app/styles/markdown.scss b/frontend/app/styles/markdown.scss
index 18183dc3..9b72191b 100644
--- a/frontend/app/styles/markdown.scss
+++ b/frontend/app/styles/markdown.scss
@@ -291,4 +291,37 @@
.columns .column {
margin: 0 10px;
flex: 1;
+}
+
+// *************** CODE BLOCKS **************** //
+// Copy Button Styles
+.code-block-wrapper {
+ position: relative;
+}
+
+.code-copy-btn {
+ position: absolute;
+ top: 22px;
+ right: 14px;
+ padding: 4px;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+
+ fill: #e3e4e6;
+ cursor: pointer;
+ opacity: 0;
+ transition: opacity .15s ease, background-color .15s ease, border-color .15s ease;
+}
+
+.code-block-wrapper:hover .code-copy-btn {
+ opacity: 1;
+}
+
+.code-copy-btn:hover {
+ background: #b0aeb91a;
+}
+
+.code-copy-btn.copied {
+ fill: #2ac27a;
}
\ No newline at end of file