From 4effc0ce2325f35b3de5340cc7a3ba18f23070b9 Mon Sep 17 00:00:00 2001 From: Maksim Eltyshev Date: Thu, 24 Jul 2025 16:11:15 +0200 Subject: [PATCH] fix: Preserve newlines in markdown with mentions --- client/src/configs/markdown-plugins/index.js | 4 +- client/src/configs/markdown-plugins/link.js | 6 +- .../src/configs/markdown-plugins/mention.js | 74 +++++++++---------- 3 files changed, 40 insertions(+), 44 deletions(-) diff --git a/client/src/configs/markdown-plugins/index.js b/client/src/configs/markdown-plugins/index.js index ab0f075a..06ee7dc1 100644 --- a/client/src/configs/markdown-plugins/index.js +++ b/client/src/configs/markdown-plugins/index.js @@ -7,7 +7,7 @@ import sup from '@diplodoc/transform/lib/plugins/sup'; import monospace from '@diplodoc/transform/lib/plugins/monospace'; import code from '@diplodoc/transform/lib/plugins/code'; import imsize from '@diplodoc/transform/lib/plugins/imsize'; -import video from '@diplodoc/transform/lib/plugins/video'; +// import video from '@diplodoc/transform/lib/plugins/video'; import table from '@diplodoc/transform/lib/plugins/table'; import note from '@diplodoc/transform/lib/plugins/notes'; import cut from '@diplodoc/transform/lib/plugins/cut'; @@ -35,7 +35,7 @@ export default [ monospace, code, (md) => md.use(imsize, { enableInlineStyling: true }), - video, + // video, table, (md) => md.use(note, { notesAutotitle: false, log: console }), cut, diff --git a/client/src/configs/markdown-plugins/link.js b/client/src/configs/markdown-plugins/link.js index 34b932a0..09f03fa6 100644 --- a/client/src/configs/markdown-plugins/link.js +++ b/client/src/configs/markdown-plugins/link.js @@ -52,9 +52,9 @@ export default (md) => { return; } - token.children.forEach((childrenToken, index) => { - if (childrenToken.type === 'link_open') { - process(childrenToken, token.children[index + 1]); + token.children.forEach((currentToken, index) => { + if (currentToken.type === 'link_open') { + process(currentToken, token.children[index + 1]); } }); }); diff --git a/client/src/configs/markdown-plugins/mention.js b/client/src/configs/markdown-plugins/mention.js index 511efbd4..273185f9 100644 --- a/client/src/configs/markdown-plugins/mention.js +++ b/client/src/configs/markdown-plugins/mention.js @@ -3,51 +3,47 @@ * Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md */ -import { MENTION_REGEX } from '../../utils/mentions'; - export default (md) => { md.core.ruler.push('mention', ({ tokens }) => { tokens.forEach((token) => { - if (token.type === 'inline' && token.content) { - const matches = [...token.content.matchAll(MENTION_REGEX)]; + if (!token.children) { + return; + } - if (matches.length > 0) { - const newChildren = []; - let lastIndex = 0; + for (let i = 0; i < token.children.length - 3; i += 1) { + const currentToken = token.children[i]; + const linkOpenToken = token.children[i + 1]; + const textToken = token.children[i + 2]; + const linkCloseToken = token.children[i + 3]; - matches.forEach((match) => { - // Add text before the mention - if (match.index > lastIndex) { - newChildren.push({ - type: 'text', - content: token.content.slice(lastIndex, match.index), - level: token.level, - }); - } + if ( + currentToken.type === 'text' && + currentToken.content.endsWith('@') && + linkOpenToken.type === 'link_open' && + textToken.type === 'text' && + linkCloseToken.type === 'link_close' + ) { + const userId = linkOpenToken.attrGet('href'); + const { content: name } = textToken; - // Add mention token - newChildren.push({ - type: 'mention', - meta: { - display: match[1], - userId: match[2], - }, - level: token.level, - }); - - lastIndex = match.index + match[0].length; - }); - - // Add remaining text after last mention - if (lastIndex < token.content.length) { - newChildren.push({ - type: 'text', - content: token.content.slice(lastIndex), - level: token.level, - }); + if (currentToken.content.length === 1) { + token.children.splice(i, 1); + i -= 1; + } else { + currentToken.content = currentToken.content.slice(0, -1); } - token.children = newChildren; // eslint-disable-line no-param-reassign + const mentionToken = { + ...currentToken, + type: 'mention', + meta: { + userId, + name, + }, + }; + + token.children.splice(i + 1, 3, mentionToken); + i += 1; } } }); @@ -55,7 +51,7 @@ export default (md) => { // eslint-disable-next-line no-param-reassign md.renderer.rules.mention = (tokens, index) => { - const { display, userId } = tokens[index].meta; - return `@${display}`; + const { userId, name } = tokens[index].meta; + return `@${name}`; }; };