import is from "is" ; import Immutable from "immutable" ; import Promise from "../../utils/promise" ; import editHTMLElement from "./editHTMLElement" ; param { Array < String > } return { string } function getLanguageForClass ( classNames ) { return Immutable . List ( classNames ) . map ( ( cl ) => { if ( cl . search ( "lang-" ) === 0 ) { return cl . slice ( "lang-" . length ) ; } if ( cl . search ( "language-" ) === 0 ) { return cl . slice ( "language-" . length ) ; } return null ; } ) . find ( ( cl ) => { return Boolean ( cl ) ; } ) ; } param { Function param { HTMLDom } $ return { Promise } function highlightCode ( highlight , $ ) { return editHTMLElement ( $ , "code" , ( $code ) => { const classNames = ( $code . attr ( "class" ) || "" ) . split ( " " ) ; const lang = getLanguageForClass ( classNames ) ; const source = $code . text ( ) ; return Promise ( highlight ( lang , source ) ) . then ( ( r ) => { if ( is . string ( r . html ) ) { $code . html ( r . html ) ; } else { $code . text ( r . text ) ; } } ) ; } ) ; } export default highlightCode ;