Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/features/messages/markdown-preparation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const isLiteralMarkdownContext = (type: string) =>
"html",
].includes(type);

function literalRanges(
export function literalRanges(
tree: MarkdownNode,
includeLinks: boolean,
): readonly LiteralRange[] {
Expand Down
19 changes: 19 additions & 0 deletions src/features/messages/message-edit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,25 @@ describe("message edit round trips", () => {
const row = firstRow([{ ...original, tags: [["h", "room"]] }]);
expect(messageEditText(row, profiles)).toBe(original.content);
});
it.each([
"[ref]: <https://example.com/a @Sam>",
'[ref]: https://example.com "Ask @Sam"',
"<!-- @Sam -->",
'<span title="@Sam">Hi</span>',
'[[a]](https://example.com "See @Sam")',
'[a](https://example.com\n "See @Sam")',
"[hello\n@Sam](https://example.com)",
"> @Sam",
])("links a name beside literal Markdown, not inside it: %s", (literal) => {
const content = `<b>@Sam</b>\n\n${literal}`;
const tags = [
["h", "room"],
["p", person],
];
expect(
messageEditText(firstRow([{ ...original, content, tags }]), profiles),
).toBe(`<b>[@Sam](${profileTarget(person)})</b>\n\n${literal}`);
});
});

it("rejects changed attachment links while allowing text-only edits", () => {
Expand Down
27 changes: 18 additions & 9 deletions src/features/messages/message-edit.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { projectMarkdownAttachments } from "../relay/message-content";
import {
projectMarkdownAttachments,
scanMarkdown,
} from "../relay/message-content";
import type { AgentLibrary } from "../agents/library";
import type { ChannelMessage, Profile } from "../relay/contracts";
import { npubEncode } from "nostr-tools/nip19";
import { literalRanges } from "./markdown-preparation";
import type { MentionRecipient } from "./mention-draft";
import { profileMentionParts } from "./profile-mentions";

Expand All @@ -27,14 +31,19 @@ export function messageEditText(
agents: AgentLibrary["identities"] = [],
) {
const { attachmentSeams: _seams, ...source } = row;
return profileMentionParts(
{ ...source, content: row.sourceContent ?? row.content },
profiles,
agents,
)
.map(({ text, target }) =>
target ? profileMentionLink(text, target) : text,
)
const content = row.sourceContent ?? row.content;
const ranges = literalRanges(scanMarkdown(content).tree, true);
let offset = 0;
let next = 0;
return profileMentionParts({ ...source, content }, profiles, agents)
.map(({ text, target }) => {
const start = offset;
offset += text.length;
while ((ranges[next]?.end ?? Infinity) <= start) next++;
return target && (ranges[next]?.start ?? Infinity) >= offset
? profileMentionLink(text, target)
: text;
})
.join("");
}

Expand Down
Loading