@ -1,5 +1,11 @@
import { describe , expect , it } from "vitest" ;
import { markdownToMatrixHtml } from "./format.js" ;
import { markdownToMatrixHtml , renderMarkdownToMatrixHtmlWithMentions } from "./format.js" ;
function createMentionClient ( selfUserId = "@bot:example.org" ) {
return {
getUserId : async ( ) = > selfUserId ,
} as unknown as import ( "./sdk.js" ) . MatrixClient ;
}
describe ( "markdownToMatrixHtml" , ( ) = > {
it ( "renders basic inline formatting" , ( ) = > {
@ -43,4 +49,225 @@ describe("markdownToMatrixHtml", () => {
const html = markdownToMatrixHtml ( "line1\nline2" ) ;
expect ( html ) . toContain ( "<br" ) ;
} ) ;
it ( "renders qualified Matrix user mentions as matrix.to links and m.mentions metadata" , async ( ) = > {
const result = await renderMarkdownToMatrixHtmlWithMentions ( {
markdown : "hello @alice:example.org" ,
client : createMentionClient ( ) ,
} ) ;
expect ( result . html ) . toContain ( 'href="https://matrix.to/#/%40alice%3Aexample.org"' ) ;
expect ( result . mentions ) . toEqual ( {
user_ids : [ "@alice:example.org" ] ,
} ) ;
} ) ;
it ( "url-encodes matrix.to hrefs for valid mxids with path characters" , async ( ) = > {
const result = await renderMarkdownToMatrixHtmlWithMentions ( {
markdown : "hello @foo/bar:example.org" ,
client : createMentionClient ( ) ,
} ) ;
expect ( result . html ) . toContain ( 'href="https://matrix.to/#/%40foo%2Fbar%3Aexample.org"' ) ;
expect ( result . mentions ) . toEqual ( {
user_ids : [ "@foo/bar:example.org" ] ,
} ) ;
} ) ;
it ( "treats mxids that begin with room as user mentions" , async ( ) = > {
const result = await renderMarkdownToMatrixHtmlWithMentions ( {
markdown : "hello @room:example.org" ,
client : createMentionClient ( ) ,
} ) ;
expect ( result . html ) . toContain ( 'href="https://matrix.to/#/%40room%3Aexample.org"' ) ;
expect ( result . mentions ) . toEqual ( {
user_ids : [ "@room:example.org" ] ,
} ) ;
} ) ;
it ( "treats hyphenated room-prefixed mxids as user mentions" , async ( ) = > {
const result = await renderMarkdownToMatrixHtmlWithMentions ( {
markdown : "hello @room-admin:example.org" ,
client : createMentionClient ( ) ,
} ) ;
expect ( result . html ) . toContain ( 'href="https://matrix.to/#/%40room-admin%3Aexample.org"' ) ;
expect ( result . mentions ) . toEqual ( {
user_ids : [ "@room-admin:example.org" ] ,
} ) ;
} ) ;
it ( "keeps explicit room mentions as room mentions" , async ( ) = > {
const result = await renderMarkdownToMatrixHtmlWithMentions ( {
markdown : "hello @room" ,
client : createMentionClient ( ) ,
} ) ;
expect ( result . html ) . toContain ( "@room" ) ;
expect ( result . html ) . not . toContain ( "matrix.to" ) ;
expect ( result . mentions ) . toEqual ( {
room : true ,
} ) ;
} ) ;
it ( "treats sentence-ending room mentions as room mentions" , async ( ) = > {
const result = await renderMarkdownToMatrixHtmlWithMentions ( {
markdown : "hello @room." ,
client : createMentionClient ( ) ,
} ) ;
expect ( result . html ) . toContain ( "hello @room." ) ;
expect ( result . html ) . not . toContain ( "matrix.to" ) ;
expect ( result . mentions ) . toEqual ( {
room : true ,
} ) ;
} ) ;
it ( "treats colon-suffixed room mentions as room mentions" , async ( ) = > {
const result = await renderMarkdownToMatrixHtmlWithMentions ( {
markdown : "hello @room:" ,
client : createMentionClient ( ) ,
} ) ;
expect ( result . html ) . toContain ( "hello @room:" ) ;
expect ( result . html ) . not . toContain ( "matrix.to" ) ;
expect ( result . mentions ) . toEqual ( {
room : true ,
} ) ;
} ) ;
it ( "trims punctuation before storing mentioned user ids" , async ( ) = > {
const result = await renderMarkdownToMatrixHtmlWithMentions ( {
markdown : "hello @alice:example.org." ,
client : createMentionClient ( ) ,
} ) ;
expect ( result . html ) . toContain ( 'href="https://matrix.to/#/%40alice%3Aexample.org"' ) ;
expect ( result . html ) . toContain ( "@alice:example.org</a>." ) ;
expect ( result . mentions ) . toEqual ( {
user_ids : [ "@alice:example.org" ] ,
} ) ;
} ) ;
it ( "does not emit mentions for mxid-like tokens with path suffixes" , async ( ) = > {
const result = await renderMarkdownToMatrixHtmlWithMentions ( {
markdown : "hello @alice:example.org/path" ,
client : createMentionClient ( ) ,
} ) ;
expect ( result . html ) . toContain ( "@alice:example.org/path" ) ;
expect ( result . html ) . not . toContain ( "matrix.to" ) ;
expect ( result . mentions ) . toEqual ( { } ) ;
} ) ;
it ( "accepts bracketed homeservers in matrix mentions" , async ( ) = > {
const result = await renderMarkdownToMatrixHtmlWithMentions ( {
markdown : "hello @alice:[2001:db8::1]" ,
client : createMentionClient ( ) ,
} ) ;
expect ( result . html ) . toContain ( 'href="https://matrix.to/#/%40alice%3A%5B2001%3Adb8%3A%3A1%5D"' ) ;
expect ( result . mentions ) . toEqual ( {
user_ids : [ "@alice:[2001:db8::1]" ] ,
} ) ;
} ) ;
it ( "accepts bracketed homeservers with ports in matrix mentions" , async ( ) = > {
const result = await renderMarkdownToMatrixHtmlWithMentions ( {
markdown : "hello @alice:[2001:db8::1]:8448." ,
client : createMentionClient ( ) ,
} ) ;
expect ( result . html ) . toContain (
'href="https://matrix.to/#/%40alice%3A%5B2001%3Adb8%3A%3A1%5D%3A8448"' ,
) ;
expect ( result . html ) . toContain ( "@alice:[2001:db8::1]:8448</a>." ) ;
expect ( result . mentions ) . toEqual ( {
user_ids : [ "@alice:[2001:db8::1]:8448" ] ,
} ) ;
} ) ;
it ( "leaves bare localpart text unmentioned" , async ( ) = > {
const result = await renderMarkdownToMatrixHtmlWithMentions ( {
markdown : "hello @alice" ,
client : createMentionClient ( ) ,
} ) ;
expect ( result . html ) . not . toContain ( "matrix.to" ) ;
expect ( result . mentions ) . toEqual ( { } ) ;
} ) ;
it ( "does not convert escaped qualified mentions" , async ( ) = > {
const result = await renderMarkdownToMatrixHtmlWithMentions ( {
markdown : "\\@alice:example.org" ,
client : createMentionClient ( ) ,
} ) ;
expect ( result . html ) . toContain ( "@alice:example.org" ) ;
expect ( result . html ) . not . toContain ( "matrix.to" ) ;
expect ( result . mentions ) . toEqual ( { } ) ;
} ) ;
it ( "does not convert escaped room mentions" , async ( ) = > {
const result = await renderMarkdownToMatrixHtmlWithMentions ( {
markdown : "\\@room" ,
client : createMentionClient ( ) ,
} ) ;
expect ( result . html ) . toContain ( "@room" ) ;
expect ( result . mentions ) . toEqual ( { } ) ;
} ) ;
it ( "restores escaped mentions in markdown link labels without linking them" , async ( ) = > {
const result = await renderMarkdownToMatrixHtmlWithMentions ( {
markdown : "[\\@alice:example.org](https://example.com)" ,
client : createMentionClient ( ) ,
} ) ;
expect ( result . html ) . toContain ( '<a href="https://example.com">@alice:example.org</a>' ) ;
expect ( result . html ) . not . toContain ( "matrix.to" ) ;
expect ( result . mentions ) . toEqual ( { } ) ;
} ) ;
it ( "keeps backslashes inside code spans" , async ( ) = > {
const result = await renderMarkdownToMatrixHtmlWithMentions ( {
markdown : "`\\@alice:example.org`" ,
client : createMentionClient ( ) ,
} ) ;
expect ( result . html ) . toContain ( "<code>\\@alice:example.org</code>" ) ;
expect ( result . mentions ) . toEqual ( { } ) ;
} ) ;
it ( "does not convert mentions inside code spans" , async ( ) = > {
const result = await renderMarkdownToMatrixHtmlWithMentions ( {
markdown : "`@alice:example.org`" ,
client : createMentionClient ( ) ,
} ) ;
expect ( result . html ) . toContain ( "<code>@alice:example.org</code>" ) ;
expect ( result . html ) . not . toContain ( "matrix.to" ) ;
expect ( result . mentions ) . toEqual ( { } ) ;
} ) ;
it ( "keeps backslashes inside tilde fenced code blocks" , async ( ) = > {
const result = await renderMarkdownToMatrixHtmlWithMentions ( {
markdown : "~~~\n\\@alice:example.org\n~~~" ,
client : createMentionClient ( ) ,
} ) ;
expect ( result . html ) . toContain ( "<pre><code>\\@alice:example.org\n</code></pre>" ) ;
expect ( result . mentions ) . toEqual ( { } ) ;
} ) ;
it ( "keeps backslashes inside indented code blocks" , async ( ) = > {
const result = await renderMarkdownToMatrixHtmlWithMentions ( {
markdown : " \\@alice:example.org" ,
client : createMentionClient ( ) ,
} ) ;
expect ( result . html ) . toContain ( "<pre><code>\\@alice:example.org\n</code></pre>" ) ;
expect ( result . mentions ) . toEqual ( { } ) ;
} ) ;
} ) ;