import marked from 'marked' ; import highlightJs from '@root/src/shared/utils/highlight' ; export function getOS ( ua ) { const isWindowsPhone = /(?:Windows Phone)/ . test ( ua ) ; const isSymbian = /(?:SymbianOS)/ . test ( ua ) || isWindowsPhone ; const isFireFox = /(?:Firefox)/ . test ( ua ) ; const isChrome = /(?:Chrome|CriOS)/ . test ( ua ) ; const isAndroid = /(?:Android)/ . test ( ua ) ; const isTablet = /(?:iPad|PlayBook)/ . test ( ua ) || ( isAndroid && ! /(?:Mobile)/ . test ( ua ) ) || ( isFireFox && /(?:Tablet)/ . test ( ua ) ) ; const isPhone = /(?:iPhone)/ . test ( ua ) && ! isTablet ; const isPc = ! isPhone && ! isAndroid && ! isSymbian ; return { isChrome , isFireFox , isTablet , isPhone , isAndroid , isPc } ; } param { string } userAgent returns export const isPC = ( ua ) => { const os = getOS ( ua ) ; return os . isPc ; } ; param { string } userAgent returns export const mobileType = ( userAgent ) => { if ( userAgent . indexOf ( 'Android' ) > - 1 || userAgent . indexOf ( 'Linux' ) > - 1 ) { return 'Android' ; } else if ( userAgent . indexOf ( 'iPhone' ) > - 1 ) { return 'iPhone' ; } else if ( userAgent . indexOf ( 'iPad' ) > - 1 ) { return 'iPad' ; } else if ( userAgent . indexOf ( 'Windows Phone' ) > - 1 ) { return 'Windows Phone' ; } else { return 'none' ; } } ; export param { string } userAgent returns export function browserType ( userAgent ) { const isOpera = userAgent . indexOf ( 'Opera' ) > - 1 ; const isIE = userAgent . indexOf ( 'compatible' ) > - 1 && userAgent . indexOf ( 'MSIE' ) > - 1 && ! isOpera ; const isIE11 = userAgent . indexOf ( 'Trident' ) > - 1 && userAgent . indexOf ( 'rv:11.0' ) > - 1 ; const isEdge = userAgent . indexOf ( 'Edge' ) > - 1 && ! isIE ; const isFF = userAgent . indexOf ( 'Firefox' ) > - 1 ; const isSafari = userAgent . indexOf ( 'Safari' ) > - 1 && userAgent . indexOf ( 'Chrome' ) === - 1 ; const isChrome = userAgent . indexOf ( 'Chrome' ) > - 1 && userAgent . indexOf ( 'Safari' ) > - 1 ; if ( isIE ) { const reIE = new RegExp ( 'MSIE (\\d+\\.\\d+);' ) ; reIE . test ( userAgent ) ; const fIEVersion = parseFloat ( RegExp [ '$1' ] ) ; if ( fIEVersion === 7 ) return 'IE7' ; else if ( fIEVersion === 8 ) return 'IE8' ; else if ( fIEVersion === 9 ) return 'IE9' ; else if ( fIEVersion === 10 ) return 'IE10' ; else return 'IE7以下' ; } if ( isIE11 ) return 'IE11' ; if ( isEdge ) return 'Edge' ; if ( isFF ) return 'FF' ; if ( isOpera ) return 'Opera' ; if ( isSafari ) return 'Safari' ; if ( isChrome ) return 'Chrome' ; } param obj export const deepClone = ( obj ) => { return JSON . parse ( JSON . stringify ( obj ) ) ; } ; param map param val export const filterObjByValue = ( map , val = '' ) => { const obj = { } ; for ( const key in map ) { if ( map [ key ] !== val ) { obj [ key ] = map [ key ] ; } } return obj ; } ; param object param keys export const filterObjByKey = < T extends AnyObject > ( map , keys = [ ] ) => { return Object . keys ( deepClone ( map ) ) . filter ( ( key ) => keys . every ( ( k ) => k !== key ) ) . reduce ( ( obj , key ) => { obj [ key ] = map [ key ] ; return obj ; } , { } as AnyObject ) as T ; } ; export const formartMd = ( md ) => { const renderer = new marked . Renderer ( ) ; renderer . image = ( href , title , text ) => { return `<img class="preview-image" src="${ href }" alt="${ text }" ${ title ? `title="${ title }"` : '' }/>` ; } ; renderer . code = ( code , lang ) => { code = `${ highlightJs . highlightAuto ( code ) . value }<span class="copy-code-btn">${ lang ? `${ lang } ` : '' }复制代码</span>` ; return `<pre><code ${ lang ? `class="${ renderer . options . langPrefix }${ lang }"` : '' }>${ code }</code></pre>\n` ; } ; renderer . codespan = ( text ) => { return `<code class="code-span">${ text }</code>` ; } ; marked . setOptions ( { renderer , gfm : true , breaks : true , pedantic : false , smartLists : true , smartypants : false } ) ; const html = marked ( md ) ; return html ; } ; export const randNumber = ( min , max ) => { return min + Math . floor ( Math . random ( ) * ( max - min ) ) ; } ;