1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-08-04 21:15:25 +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 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,

View file

@ -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]);
}
});
});

View file

@ -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 `<span class="mention" data-user-id="${userId}">@${display}</span>`;
const { userId, name } = tokens[index].meta;
return `<span class="mention" data-user-id="${userId}">@${name}</span>`;
};
};