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.

feat: Katex snippets enabled only in maths mode and new snippets preferences to enable or disable them

Smaug673955bb53bd6211
diff --git a/frontend/app/components/MarkdownEditor/EditorPreferences.vue b/frontend/app/components/MarkdownEditor/EditorPreferences.vue
index 9a388f9e..bba01668 100644
--- a/frontend/app/components/MarkdownEditor/EditorPreferences.vue
+++ b/frontend/app/components/MarkdownEditor/EditorPreferences.vue
@@ -16,6 +16,10 @@
<label for="category">Display editor statistics</label>
<AppToggle v-model="store.get('editorDisplayStats').value" />
</div>
+ <div class="row">
+ <label for="category">Enable snippets</label>
+ <AppToggle v-model="store.get('editorSnippetsEnabled').value" />
+ </div>
</form>
</div>
</template>
diff --git a/frontend/app/components/MarkdownEditor/LazyMarkdownEditor.vue b/frontend/app/components/MarkdownEditor/LazyMarkdownEditor.vue
index 6f02460d..139934d6 100644
--- a/frontend/app/components/MarkdownEditor/LazyMarkdownEditor.vue
+++ b/frontend/app/components/MarkdownEditor/LazyMarkdownEditor.vue
@@ -282,21 +282,88 @@ const updateListener = EditorView.updateListener.of(v => {
autoSaveConditional();
}
});
+function isInsideMath(context: CompletionContext): boolean {
+ const doc = context.state.doc.toString();
+ const pos = context.pos;
+ const len = doc.length;
+ const tokens: { pos: number; type: '$' | '$$' }[] = [];
+
+ // Tokenize: find $ and $$ that are NOT escaped (preceded by backslash)
+ for (let i = 0; i < len; i++) {
+ if (doc[i] !== '$') continue;
+ // count backslashes immediately before to check escaping
+ let j = i - 1;
+ let backslashes = 0;
+ while (j >= 0 && doc[j] === '\\') {
+ backslashes++;
+ j--;
+ }
+ if (backslashes % 2 === 1) continue; // escaped -> ignore
+
+ if (i + 1 < len && doc[i + 1] === '$') {
+ tokens.push({ pos: i, type: '$$' });
+ i++; // skip second $
+ } else {
+ tokens.push({ pos: i, type: '$' });
+ }
+ }
+
+ // Count $$ before the cursor
+ const blockDollarPositions = tokens.filter(t => t.type === '$$').map(t => t.pos);
+ const blockCountBefore = blockDollarPositions.filter(p => p < pos).length;
+ // If odd number of $$ before cursor -> we're inside a $$...$$ block
+ if (blockCountBefore % 2 === 1) return true;
+
+ // Build intervals of $$ pairs to exclude single $ inside them
+ const blockIntervals: Array<[number, number]> = [];
+ for (let i = 0; i + 1 < blockDollarPositions.length; i += 2) {
+ const start = blockDollarPositions[i] ?? 0;
+ const end = (blockDollarPositions[i + 1] ?? 0) + 2; // end index after closing $$
+ blockIntervals.push([start, end]);
+ }
+
+ // Helper: check if position is inside any $$ interval
+ const inBlock = (p: number) => blockIntervals.some(([s, e]) => p >= s && p < e);
+
+ // Filter single $ tokens that are NOT inside a $$ interval
+ const singleDollarPositions = tokens.filter(t => t.type === '$' && !inBlock(t.pos)).map(t => t.pos);
+
+ const singleCountBefore = singleDollarPositions.filter(p => p < pos).length;
+
+ // If odd -> inside inline $...$
+ return singleCountBefore % 2 === 1;
+}
function snippetSource(context: CompletionContext) {
- const snippets = preferences.get('snippets').value;
+ if (!preferences.get('editorSnippetsEnabled').value) return null;
+ const allSnippets = preferences.get('snippets').value;
const word = context.matchBefore(/[\w!:]+/);
-
if (!word && !context.explicit) return null;
+
+ const inMath = isInsideMath(context);
+
return {
from: word ? word.from : context.pos,
- options: [...snippets, ...katexSnippets].map(s => ({
- label: s.label,
- detail: 'Snippet',
- type: 'snippet',
- apply: snippet(s.label),
- })),
+ options: [
+ // snippets "normaux" toujours disponibles
+ ...allSnippets.map(s => ({
+ label: s.id,
+ detail: 'Snippet',
+ type: 'snippet',
+ apply: snippet(s.label),
+ })),
+
+ // snippets KaTeX seulement dans $...$ ou $$...$$
+ ...(inMath
+ ? katexSnippets.map(s => ({
+ label: s.id,
+ detail: 'KaTeX',
+ type: 'snippet',
+ apply: snippet(s.label),
+ }))
+ : []),
+ ],
};
}
@@ -337,7 +404,6 @@ onMounted(() => {
window.addEventListener('keydown', handleGlobalKeys);
});
-// ... [REST OF COMPONENT INCHANGED] ...
onBeforeUnmount(() => {
if (preferences.get('documentAutoSave').value) {
updateDocumentContent();
diff --git a/frontend/app/composables/Preferences.ts b/frontend/app/composables/Preferences.ts
index 9c891a21..8c2ea74a 100644
--- a/frontend/app/composables/Preferences.ts
+++ b/frontend/app/composables/Preferences.ts
@@ -30,20 +30,21 @@ export const DEFAULT_PREFERENCES = {
navigation: true as boolean,
},
snippets: [
- { id: '!blue', label: ':::blue\n${0}\n:::' },
- { id: '!green', label: ':::green\n${0}\n:::' },
- { id: '!yellow', label: ':::yellow\n${0}\n:::' },
- { id: '!grey', label: ':::grey\n${0}\n:::' },
- { id: '!details', label: ':::details\n${0}\n:::' },
- { id: '!center', label: ':::center\n${0}\n:::' },
- { id: '!m', label: '${0}$' },
- { id: '!property', label: ':::property ${0}\n\n:::' },
- { id: '!warning', label: ':::warning ${0}\n\n:::' },
+ { id: ':::blue', label: ':::blue\n${0}\n:::' },
+ { id: ':::green', label: ':::green\n${0}\n:::' },
+ { id: ':::yellow', label: ':::yellow\n${0}\n:::' },
+ { id: ':::grey', label: ':::grey\n${0}\n:::' },
+ { id: ':::details', label: ':::details\n${0}\n:::' },
+ { id: ':::center', label: ':::center\n${0}\n:::' },
+ { id: ':::m', label: '${0}$' },
+ { id: ':::property', label: ':::property ${0}\n\n:::' },
+ { id: ':::warning', label: ':::warning ${0}\n\n:::' },
] as ANode<string>[],
editorFontFamily: 'JetBrains Mono' as string,
editorFontSize: 14 as number,
editorSpellCheck: true as boolean,
editorDisplayStats: false as boolean,
+ editorSnippetsEnabled: true as boolean,
developerMode: false as boolean,
};
diff --git a/frontend/app/pages/dashboard/settings/_views/preferences.vue b/frontend/app/pages/dashboard/settings/_views/preferences.vue
index 67c45a65..9147dadc 100644
--- a/frontend/app/pages/dashboard/settings/_views/preferences.vue
+++ b/frontend/app/pages/dashboard/settings/_views/preferences.vue
@@ -8,7 +8,10 @@
<div v-for="opt in section.options" :key="opt.key" class="form-group">
<div>
- <label>{{ opt.label }}</label>
+ <label
+ >{{ opt.label }} <tag v-if="opt.tag" class="blue">{{ opt.tag }}</tag></label
+ >
+
<p class="description">{{ opt.description }}</p>
</div>
<!-- Toggle -->
@@ -63,8 +66,12 @@ const preferencesStore = usePreferences();
// @ts-expect-error unknown type
const p = preferencesStore.get as <K extends PreferenceKey>(key: K) => ReturnType<unknown>;
+type InterfaceOption = Option & {
+ tag?: string;
+};
+
const colorMode = useColorMode();
-const options: Array<{ label: string; options: Option[] }> = [
+const options: Array<{ label: string; options: InterfaceOption[] }> = [
{
label: 'General',
options: [
@@ -210,6 +217,13 @@ const options: Array<{ label: string; options: Option[] }> = [
type: 'toggle',
key: 'editorDisplayStats',
},
+ {
+ label: 'Enable Snippets',
+ tag: 'New',
+ description: 'Enable or disable editor snippets functionality.',
+ type: 'toggle',
+ key: 'editorSnippetsEnabled',
+ },
],
},
{