1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-08-05 05:25:29 +02:00

fix: Preserve newlines in markdown with mentions

This commit is contained in:
Maksim Eltyshev 2025-07-24 16:11:15 +02:00
parent 001621eec7
commit 4effc0ce23
3 changed files with 40 additions and 44 deletions

View file

@ -7,7 +7,7 @@ import sup from '@diplodoc/transform/lib/plugins/sup';
import monospace from '@diplodoc/transform/lib/plugins/monospace'; import monospace from '@diplodoc/transform/lib/plugins/monospace';
import code from '@diplodoc/transform/lib/plugins/code'; import code from '@diplodoc/transform/lib/plugins/code';
import imsize from '@diplodoc/transform/lib/plugins/imsize'; 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 table from '@diplodoc/transform/lib/plugins/table';
import note from '@diplodoc/transform/lib/plugins/notes'; import note from '@diplodoc/transform/lib/plugins/notes';
import cut from '@diplodoc/transform/lib/plugins/cut'; import cut from '@diplodoc/transform/lib/plugins/cut';
@ -35,7 +35,7 @@ export default [
monospace, monospace,
code, code,
(md) => md.use(imsize, { enableInlineStyling: true }), (md) => md.use(imsize, { enableInlineStyling: true }),
video, // video,
table, table,
(md) => md.use(note, { notesAutotitle: false, log: console }), (md) => md.use(note, { notesAutotitle: false, log: console }),
cut, cut,

View file

@ -52,9 +52,9 @@ export default (md) => {
return; return;
} }
token.children.forEach((childrenToken, index) => { token.children.forEach((currentToken, index) => {
if (childrenToken.type === 'link_open') { if (currentToken.type === 'link_open') {
process(childrenToken, token.children[index + 1]); process(currentToken, token.children[index + 1]);
} }
}); });
}); });

View file

@ -3,51 +3,47 @@
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md * Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
*/ */
import { MENTION_REGEX } from '../../utils/mentions';
export default (md) => { export default (md) => {
md.core.ruler.push('mention', ({ tokens }) => { md.core.ruler.push('mention', ({ tokens }) => {
tokens.forEach((token) => { tokens.forEach((token) => {
if (token.type === 'inline' && token.content) { if (!token.children) {
const matches = [...token.content.matchAll(MENTION_REGEX)]; return;
}
if (matches.length > 0) { for (let i = 0; i < token.children.length - 3; i += 1) {
const newChildren = []; const currentToken = token.children[i];
let lastIndex = 0; const linkOpenToken = token.children[i + 1];
const textToken = token.children[i + 2];
const linkCloseToken = token.children[i + 3];
matches.forEach((match) => { if (
// Add text before the mention currentToken.type === 'text' &&
if (match.index > lastIndex) { currentToken.content.endsWith('@') &&
newChildren.push({ linkOpenToken.type === 'link_open' &&
type: 'text', textToken.type === 'text' &&
content: token.content.slice(lastIndex, match.index), linkCloseToken.type === 'link_close'
level: token.level, ) {
}); const userId = linkOpenToken.attrGet('href');
} const { content: name } = textToken;
// Add mention token if (currentToken.content.length === 1) {
newChildren.push({ token.children.splice(i, 1);
type: 'mention', i -= 1;
meta: { } else {
display: match[1], currentToken.content = currentToken.content.slice(0, -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,
});
} }
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 // eslint-disable-next-line no-param-reassign
md.renderer.rules.mention = (tokens, index) => { md.renderer.rules.mention = (tokens, index) => {
const { display, userId } = tokens[index].meta; const { userId, name } = tokens[index].meta;
return `<span class="mention" data-user-id="${userId}">@${display}</span>`; return `<span class="mention" data-user-id="${userId}">@${name}</span>`;
}; };
}; };