import User from "../models/User" ; const markDownImage = /!\[.*\]\((.*)\)/ ; param text param n export const getArticleAbstract = ( text , n ) : string => { if ( ! text || n <= 0 ) { return "" ; } else { return text . replace ( markDownImage , "" ) . substr ( 0 , n ) ; } } ; export const getArticleCoverImage = ( text ) : string => { if ( ! text ) { return "" ; } else { const matched = text . match ( markDownImage ) ; return matched && matched . length >= 2 ? matched [ 1 ] : "" ; } } ; param ids param userDictionary export const getNameListString = ( ids , userDictionary , separator ? ) : string => { if ( ! ids || ids . length === 0 ) { return "" ; } else { separator = separator ? separator : ", " ; return ids . map ( ( id ) => userDictionary [ id ] ? userDictionary [ id ] . name : "" ) . filter ( ( value ) => ! ! value ) . join ( separator ) ; } } ; export const getAllNames = ( userDictionary ) : string [ ] => { if ( ! userDictionary ) { return [ ] ; } return Object . values ( userDictionary ) . map ( ( user ) : string => user . name ) ; } ; param content param userDictionary export const getMentionedUserId = ( content , userDictionary ) : string [ ] => { if ( ! content ) { return [ ] ; } const ids = [ ] ; const userArray = Object . values ( userDictionary ) ; const userNameListString = getNameListString ( Object . keys ( userDictionary ) , userDictionary , "|" ) ; const toMatch = new RegExp ( `@(${ userNameListString }) ` , "g" ) ; const results = content . match ( toMatch ) ; if ( results ) { results . forEach ( ( result ) => { const name = result . substring ( 1 , result . length - 1 ) ; const found = userArray . findIndex ( ( user ) => user . name === name ) ; if ( found >= 0 ) { const existing = ids . findIndex ( ( id ) => id === userArray [ found ] . _id ) ; if ( existing < 0 ) { ids . push ( userArray [ found ] . _id ) ; } } } ) ; } return ids ; } ;