@@ -17,6 +17,14 @@ export type OutputOptions = {
1717 allowTextFormatting ?: boolean ;
1818 allowInlineMarkdown ?: boolean ;
1919 allowBlockMarkdown ?: boolean ;
20+ /**
21+ * if true it will remove the nickname of the person from the message
22+ */
23+ stripNickname ?: boolean ;
24+ /**
25+ * a map of regex patterns to replace nicknames with, used when stripNickname is true
26+ */
27+ nickNameReplacement ?: Map < RegExp , string > ;
2028} ;
2129
2230const textToCustomHtml = ( node : Text , opts : OutputOptions ) : string => {
@@ -99,21 +107,34 @@ const ignoreHTMLParseInlineMD = (text: string): string =>
99107 ( txt ) => parseInlineMD ( txt )
100108 ) . join ( '' ) ;
101109
110+ /**
111+ * convert slate internal representation to a custom HTML string that can be sent to the server
112+ * @param node slate node
113+ * @param opts options for output
114+ * @returns custom HTML string
115+ */
102116export const toMatrixCustomHTML = (
103117 node : Descendant | Descendant [ ] ,
104118 opts : OutputOptions
105119) : string => {
106120 let markdownLines = '' ;
107121 const parseNode = ( n : Descendant , index : number , targetNodes : Descendant [ ] ) => {
108122 if ( opts . allowBlockMarkdown && 'type' in n && n . type === BlockType . Paragraph ) {
109- const line = toMatrixCustomHTML ( n , {
123+ let line = toMatrixCustomHTML ( n , {
110124 ...opts ,
111125 allowInlineMarkdown : false ,
112126 allowBlockMarkdown : false ,
113127 } )
114128 . replace ( / < b r \/ > $ / , '\n' )
115129 . replace ( / ^ ( \\ * ) & g t ; / , '$1>' ) ;
116130
131+ // strip nicknames if needed
132+ if ( opts . stripNickname && opts . nickNameReplacement ) {
133+ opts . nickNameReplacement ?. keys ( ) . forEach ( ( key ) => {
134+ const replacement = opts . nickNameReplacement ! . get ( key ) ?? '' ;
135+ line = line . replaceAll ( key , replacement ) ;
136+ } ) ;
137+ }
117138 markdownLines += line ;
118139 if ( index === targetNodes . length - 1 ) {
119140 return parseBlockMD ( markdownLines , ignoreHTMLParseInlineMD ) ;
@@ -175,12 +196,37 @@ const elementToPlainText = (node: CustomElement, children: string): string => {
175196 }
176197} ;
177198
178- export const toPlainText = ( node : Descendant | Descendant [ ] , isMarkdown : boolean ) : string => {
179- if ( Array . isArray ( node ) ) return node . map ( ( n ) => toPlainText ( n , isMarkdown ) ) . join ( '' ) ;
180- if ( Text . isText ( node ) )
199+ /**
200+ * convert slate internal representation to a plain text string that can be sent to the server
201+ * @param node the slate node
202+ * @param isMarkdown set true if it's a markdown formatted text
203+ * @param stripNickname whether to strip nicknames
204+ * @param nickNameReplacement the nickname replacement
205+ * @returns the plain text we want to send
206+ */
207+ export const toPlainText = (
208+ node : Descendant | Descendant [ ] ,
209+ isMarkdown : boolean ,
210+ stripNickname = false ,
211+ nickNameReplacement ?: Map < RegExp , string >
212+ ) : string => {
213+ if ( Array . isArray ( node ) )
214+ return node . map ( ( n ) => toPlainText ( n , isMarkdown , stripNickname , nickNameReplacement ) ) . join ( '' ) ;
215+ if ( Text . isText ( node ) ) {
216+ if ( stripNickname && nickNameReplacement ) {
217+ let { text } = node ;
218+ nickNameReplacement ?. keys ( ) . forEach ( ( key ) => {
219+ const replacement = nickNameReplacement . get ( key ) ?? '' ;
220+ text = text . replaceAll ( key , replacement ) ;
221+ } ) ;
222+ return isMarkdown
223+ ? unescapeMarkdownBlockSequences ( text , unescapeMarkdownInlineSequences )
224+ : text ;
225+ }
181226 return isMarkdown
182227 ? unescapeMarkdownBlockSequences ( node . text , unescapeMarkdownInlineSequences )
183228 : node . text ;
229+ }
184230
185231 const children = node . children . map ( ( n ) => toPlainText ( n , isMarkdown ) ) . join ( '' ) ;
186232 return elementToPlainText ( node , children ) ;
@@ -208,10 +254,27 @@ export const trimCommand = (cmdName: string, str: string) => {
208254 return str . slice ( match [ 0 ] . length ) ;
209255} ;
210256
257+ /**
258+ * Type representing Mentions
259+ */
211260export type MentionsData = {
261+ /**
262+ * a boolean to denote if it's a room mention
263+ */
212264 room : boolean ;
265+ /**
266+ * a set of user ids that are mentioned in the message
267+ */
213268 users : Set < string > ;
214269} ;
270+
271+ /**
272+ * get the mentions in a message
273+ * @param mx the matrix client
274+ * @param roomId the room id we will send the message in
275+ * @param editor the slate editor
276+ * @returns the mentions in a message {@link MentionsData}
277+ */
215278export const getMentions = ( mx : MatrixClient , roomId : string , editor : Editor ) : MentionsData => {
216279 const mentionData : MentionsData = {
217280 room : false ,
0 commit comments