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: improved markdown syntax (#561)

Smaug358a37f3dea3
diff --git a/frontend/app/components/MarkdownSyntax.vue b/frontend/app/components/MarkdownSyntax.vue
index adcbc80a..f67e6719 100644
--- a/frontend/app/components/MarkdownSyntax.vue
+++ b/frontend/app/components/MarkdownSyntax.vue
@@ -42,9 +42,19 @@
<p>
<strong>{{ t('markdown.syntax.linksAndImages') }}:</strong>
- Use <code>[links](https://example.com)</code> and <code>![images](image.png)</code>.
+ Use <code>[links](https://example.com)</code> and <code>![images](image.png)</code>. <br />
+ Resize images with <code>![image | WIDTH](image.png)</code> or <code>![image | WIDTH](image.png)</code>.
</p>
-
+ <div class="center" style="flex-direction: row">
+ <div class="center">
+ <img src="/Logo/Alexandrie-logo-light.svg" alt="Example Image" style="width: 200px; height: auto" />
+ <code>![image | 200x200](image.png)</code>
+ </div>
+ <div class="center">
+ <img src="/Logo/Alexandrie-logo-light.svg" alt="Example Image" style="width: 150px; height: auto" />
+ <code>![image | 150x150](image.png)</code>
+ </div>
+ </div>
<!-- COLORED BLOCKS -->
<h2><Icon name="layers" fill="var(--primary)" /> {{ t('markdown.syntax.coloredBlocks') }}</h2>
<p>Use colored blocks with the syntax <code>:::color TITLE</code>.</p>
diff --git a/frontend/app/components/Node/Document/ContentCompiled.vue b/frontend/app/components/Node/Document/ContentCompiled.vue
index 631a668b..6c5d38c1 100644
--- a/frontend/app/components/Node/Document/ContentCompiled.vue
+++ b/frontend/app/components/Node/Document/ContentCompiled.vue
@@ -4,6 +4,7 @@
ref="rootElement"
:class="['markdown-preview', `${theme}-theme`, `document-content`]"
:style="{ fontSize: documentFontSize + 'px', fontFamily: documentFontFamily, lineHeight: documentLineHeight }"
+ @change="handleCheckboxChange"
v-html="node?.content_compiled"
/>
</template>
@@ -15,6 +16,7 @@ import type { Node } from '~/stores';
const props = defineProps<{ node?: Partial<Node> }>();
const preferences = usePreferencesStore();
+const nodesStore = useNodesStore();
const theme = computed(() => {
if (props.node?.theme) return props.node.theme;
@@ -28,6 +30,53 @@ const rootElement = ref<HTMLElement>();
defineExpose({ rootElement });
+const updateDocumentContent = debounce(() => {
+ nodesStore.update(nodesStore.nodes.get(props.node!.id!) as Node);
+}, 1000);
+
+const updateMarkdownCheckbox = (markdown: string, indexToTarget: number, shouldCheck: boolean): string => {
+ let currentIndex = 0;
+
+ return markdown.replace(/^([ \t]*[-*+]\s+\[)([ xX]?)(\].*)/gm, (match, before, status, after) => {
+ if (currentIndex === indexToTarget) {
+ currentIndex++;
+ const newStatus = shouldCheck ? 'x' : ' ';
+ return `${before}${newStatus}${after}`;
+ }
+ currentIndex++;
+ return match;
+ });
+};
+
+const handleCheckboxChange = (event: Event) => {
+ const target = event.target as HTMLInputElement;
+
+ if (target && target.type === 'checkbox' && target.hasAttribute('data-checkbox-index')) {
+ const checkboxIndex = parseInt(target.getAttribute('data-checkbox-index') || '0', 10);
+ const isChecked = target.checked;
+
+ if (!props.node?.content) return;
+
+ const updatedMarkdown = updateMarkdownCheckbox(props.node.content, checkboxIndex, isChecked);
+
+ // Update the DOM attribute to reflect the new state
+ if (isChecked) target.setAttribute('checked', '');
+ else target.removeAttribute('checked');
+
+ const updatedHtml = rootElement.value?.innerHTML || '';
+ nodesStore.nodes.set(
+ props.node.id!,
+ {
+ ...props.node,
+ content: updatedMarkdown,
+ content_compiled: updatedHtml,
+ } as Node,
+ true,
+ );
+ updateDocumentContent();
+ }
+};
+
onMounted(() => {
const unsub = subscribeDrawioCacheInvalidated(() => {
rerenderImages(rootElement.value!);
diff --git a/frontend/app/helpers/markdown/checkbox.ts b/frontend/app/helpers/markdown/checkbox.ts
index ec18811a..da5d4eff 100644
--- a/frontend/app/helpers/markdown/checkbox.ts
+++ b/frontend/app/helpers/markdown/checkbox.ts
@@ -2,9 +2,9 @@ import type MarkdownIt from 'markdown-it';
import type { StateCore } from 'markdown-it/dist/index.cjs.js';
function markdownItCheckbox(md: MarkdownIt) {
- let checkboxIdCounter = 0;
// Add a core rule to process checkboxes
md.core.ruler.after('inline', 'checkbox', (state: StateCore) => {
+ let checkboxIdCounter = 0;
// Iterate through all tokens in the state
for (let i = 0; i < state.tokens.length; i++) {
const token = state.tokens[i];
@@ -16,13 +16,12 @@ function markdownItCheckbox(md: MarkdownIt) {
if (match) {
const isChecked = match[1]?.toLowerCase() === 'x';
const label = match[2];
- const checkboxId = `check-${checkboxIdCounter++}`;
// Inject HTML tokens for rendering checkboxes
const checkboxToken = new state.Token('html_inline', '', 0);
checkboxToken.content = `
<label class="checkbox-container">
- <input type="checkbox" ${isChecked ? 'checked' : ''} id="check-${checkboxId}">
+ <input type="checkbox" ${isChecked ? 'checked' : ''} data-checkbox-index="${checkboxIdCounter++}">
<span>${label}</span>
</label>
`;
diff --git a/frontend/app/helpers/markdown/other.ts b/frontend/app/helpers/markdown/other.ts
index 239765fd..8a43c923 100644
--- a/frontend/app/helpers/markdown/other.ts
+++ b/frontend/app/helpers/markdown/other.ts
@@ -131,7 +131,6 @@ 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) {
@@ -141,14 +140,38 @@ function html5MediaPlugin(md: MarkdownIt) {
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 || '';
+ let alt = token.content || '';
+
+ // Parse: "Alt text | 100x200", "Alt | 100x", "Alt | x200", "Alt | 100"
+ const match = alt.match(/^(.*?)\s*\|\s*(?:(\d+)?x(\d+)?|(\d+))\s*$/);
+
+ if (match) {
+ alt = match[1]!.trim();
+ token.content = alt;
+
+ const width = match[4] || match[2];
+ const height = match[3];
+
+ if (width) {
+ token.attrSet('width', width);
+ token.attrJoin('style', `width:${width}px;`);
+ }
+ if (height) {
+ token.attrSet('height', height);
+ token.attrJoin('style', `height:${height}px;`);
+ }
+ }
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)) {
+ }
+
+ 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);
};
}
diff --git a/frontend/app/pages/dashboard/docs/[id].vue b/frontend/app/pages/dashboard/docs/[id].vue
index 99c8a278..0bcaa05a 100644
--- a/frontend/app/pages/dashboard/docs/[id].vue
+++ b/frontend/app/pages/dashboard/docs/[id].vue
@@ -46,13 +46,18 @@ const docSize = preferencesStore.get('docSize');
const elementComponent = ref<InstanceType<typeof NodeDocumentContentCompiled>>();
const element = computed(() => elementComponent.value?.rootElement as HTMLElement | undefined);
-const node = ref<Node | undefined>();
const error = ref<false | string>(false);
const documentId = computed(() => route.params.id as string);
const next = computed(() => nodesTree.nextDocument(node.value?.id));
const previous = computed(() => nodesTree.prevDocument(node.value?.id));
+
+const node = computed<Node | undefined>(() => {
+ if (!documentId.value) return undefined;
+ return documentsStore.getById(documentId.value);
+});
+
const width = computed(() => {
if (docSize.value == 2) return '980px';
if (docSize.value == 1) return '800px';
@@ -62,27 +67,21 @@ const width = computed(() => {
async function loadDocument(id: string) {
error.value = false;
- if (documentsStore.isFetching) {
- node.value = undefined;
- return;
- }
+ if (documentsStore.isFetching) return;
- const docFromStore = documentsStore.getById(id);
+ const cached = documentsStore.getById(id);
- if (!docFromStore) {
- node.value = undefined;
+ if (!cached) {
error.value = 'Document not found';
return;
}
- if (docFromStore.partial) {
+ if (cached.partial) {
try {
- node.value = await documentsStore.fetch({ id });
+ await documentsStore.fetch({ id });
} catch (err: unknown) {
error.value = (err as Error).message || 'Failed to fetch document';
}
- } else {
- node.value = docFromStore;
}
if (node.value) useHead({ title: node.value.name });
diff --git a/frontend/app/stores/nodes.store.ts b/frontend/app/stores/nodes.store.ts
index c4231370..6e42882a 100644
--- a/frontend/app/stores/nodes.store.ts
+++ b/frontend/app/stores/nodes.store.ts
@@ -221,7 +221,7 @@ export const useNodesStore = defineStore('nodes', () => {
shared: shared,
permissions: result.permissions,
};
- nodes.value.set(opts.id, updatedNode);
+ nodes.value.set(opts.id, updatedNode, true);
return updatedNode as 'id' extends keyof T ? Node : IndexedCollection;
} else {
if (!request.result) return nodes.value as 'id' extends keyof T ? Node : IndexedCollection;