feat: Add @mention support to comments (#5001)
* Refactor, remove confusing 'packages' language * Basic notifications when mentioned in comment * fix: Incorrect trimming of comments * test
This commit is contained in:
@@ -1,30 +0,0 @@
|
||||
import { buildDocument } from "@server/test/factories";
|
||||
import parseMentions from "./parseMentions";
|
||||
|
||||
it("should not parse normal links as mentions", async () => {
|
||||
const document = await buildDocument({
|
||||
text: `# Header
|
||||
|
||||
[link not mention](http://google.com)`,
|
||||
});
|
||||
const result = parseMentions(document);
|
||||
expect(result.length).toBe(0);
|
||||
});
|
||||
|
||||
it("should return an array of mentions", async () => {
|
||||
const document = await buildDocument({
|
||||
text: `# Header
|
||||
|
||||
@[Alan Kay](mention://2767ba0e-ac5c-4533-b9cf-4f5fc456600e/user/34095ac1-c808-45c0-8c6e-6c554497de64) :wink:
|
||||
|
||||
More text
|
||||
|
||||
@[Bret Victor](mention://34095ac1-c808-45c0-8c6e-6c554497de64/user/2767ba0e-ac5c-4533-b9cf-4f5fc456600e) :fire:`,
|
||||
});
|
||||
const result = parseMentions(document);
|
||||
expect(result.length).toBe(2);
|
||||
expect(result[0].id).toBe("2767ba0e-ac5c-4533-b9cf-4f5fc456600e");
|
||||
expect(result[1].id).toBe("34095ac1-c808-45c0-8c6e-6c554497de64");
|
||||
expect(result[0].modelId).toBe("34095ac1-c808-45c0-8c6e-6c554497de64");
|
||||
expect(result[1].modelId).toBe("2767ba0e-ac5c-4533-b9cf-4f5fc456600e");
|
||||
});
|
||||
@@ -1,35 +0,0 @@
|
||||
import { Node } from "prosemirror-model";
|
||||
import { Document, Revision } from "@server/models";
|
||||
import DocumentHelper from "@server/models/helpers/DocumentHelper";
|
||||
|
||||
/**
|
||||
* Parse a list of mentions contained in a document or revision
|
||||
*
|
||||
* @param document Document or Revision
|
||||
* @returns An array of mentions in passed document or revision
|
||||
*/
|
||||
export default function parseMentions(
|
||||
document: Document | Revision
|
||||
): Record<string, string>[] {
|
||||
const node = DocumentHelper.toProsemirror(document);
|
||||
const mentions: Record<string, string>[] = [];
|
||||
|
||||
function findMentions(node: Node) {
|
||||
if (
|
||||
node.type.name === "mention" &&
|
||||
!mentions.some((m) => m.id === node.attrs.id)
|
||||
) {
|
||||
mentions.push(node.attrs);
|
||||
}
|
||||
|
||||
if (!node.content.size) {
|
||||
return;
|
||||
}
|
||||
|
||||
node.content.descendants(findMentions);
|
||||
}
|
||||
|
||||
findMentions(node);
|
||||
|
||||
return mentions;
|
||||
}
|
||||
Reference in New Issue
Block a user