import Prism , { Grammar } from 'prismjs' ; import loadLanguages from 'prismjs/components/' ; import MarkdownIt from 'markdown-it' ; import { RenderRule } from 'markdown-it/lib/renderer' ; interface Options { /**
     * Prism plugins to load.
     */ plugins : string [ ] ; param prism init : ( prism : typeof Prism ) => void ; /**
     * The language to use for code blocks that specify a language that Prism does not know.
     */ defaultLanguageForUnknown ? : string ; /**
     * The language to use for code blocks that do not specify a language.
     */ defaultLanguageForUnspecified ? : string ; /**
     * Shorthand to set both {@code defaultLanguageForUnknown} and {@code defaultLanguageForUnspecified} to the same value. Will be copied
     * to each option if it is set to {@code undefined}.
     */ defaultLanguage ? : string ; } const DEFAULTS = { plugins : [ ] , init : ( ) => { } , defaultLanguageForUnknown : undefined , defaultLanguageForUnspecified : undefined , defaultLanguage : undefined } ; param lang return function loadPrismLang ( lang ) { if ( ! lang ) return undefined ; let langObject = Prism . languages [ lang ] ; if ( langObject === undefined ) { loadLanguages ( [ lang ] ) ; langObject = Prism . languages [ lang ] ; } return langObject ; } param name throws function loadPrismPlugin ( name ) { try { require ( `prismjs/plugins/${ name }/prism-${ name }` ) ; } catch ( e ) { throw new Error ( `Cannot load Prism plugin "${ name }". Please check the spelling.` ) ; } } param options param lang return function selectLanguage ( options , lang ) { let langToUse = lang ; if ( langToUse === '' && options . defaultLanguageForUnspecified !== undefined ) { langToUse = options . defaultLanguageForUnspecified ; } let prismLang = loadPrismLang ( langToUse ) ; if ( prismLang === undefined && options . defaultLanguageForUnknown !== undefined ) { langToUse = options . defaultLanguageForUnknown ; prismLang = loadPrismLang ( langToUse ) ; } return [ langToUse , prismLang ] ; } param markdownit param options param text param lang return If Prism knows the language that  selectLanguage returns for `lang`, the `text` highlighted for that language. Otherwise, `text`
 *  html-escaped.
  function highlight ( markdownit , options , text , lang ) { const [ langToUse , prismLang ] = selectLanguage ( options , lang ) ; return prismLang ? Prism . highlight ( text , prismLang , langToUse ) : markdownit . utils . escapeHtml ( text ) ; } param markdownit param lang return function languageClass ( markdownit , lang ) { return markdownit . options . langPrefix + markdownit . utils . escapeHtml ( lang ) ; } param markdownit param options param existingRule function applyCodeAttributes ( markdownit , options , existingRule ) { return ( tokens , idx , renderOptions , env , self ) => { const fenceToken = tokens [ idx ] ; const info = fenceToken . info ? markdownit . utils . unescapeAll ( fenceToken . info ) . trim ( ) : '' ; const lang = info . split ( /(\s+)/g ) [ 0 ] ; const [ langToUse ] = selectLanguage ( options , lang ) ; if ( ! langToUse ) { return existingRule ( tokens , idx , renderOptions , env , self ) ; } else { fenceToken . info = langToUse ; const existingResult = existingRule ( tokens , idx , renderOptions , env , self ) ; const langClass = languageClass ( markdownit , langToUse ) ; return existingResult . replace ( /<((?:pre|code)[^>]*?)(?:\s+class="([^"]*)"([^>]*))?>/g , ( match , tagStart , existingClasses ? , tagEnd ? ) => existingClasses ?. includes ( langClass ) ? match : `<${ tagStart } class="${ existingClasses ? `${ existingClasses } ` : '' }${ langClass }"${ tagEnd || '' }>` ) ; } } ; } param options param optionName throws function checkLanguageOption ( options , optionName ) { const language = options [ optionName ] ; if ( language !== undefined && loadPrismLang ( language ) === undefined ) { throw new Error ( `Bad option ${ optionName }: There is no Prism language '${ language }'.` ) ; } } /**
 * Initialisation function of the plugin. This function is not called directly by clients, but is rather provided
 * to MarkdownIt’s  MarkdownIt . use function.
 *
 *  param markdownit param useroptions export default function markdownItPrism ( markdownit , useroptions ) { const options = Object . assign ( { } , DEFAULTS , useroptions ) ; checkLanguageOption ( options , 'defaultLanguage' ) ; checkLanguageOption ( options , 'defaultLanguageForUnknown' ) ; checkLanguageOption ( options , 'defaultLanguageForUnspecified' ) ; options . defaultLanguageForUnknown = options . defaultLanguageForUnknown || options . defaultLanguage ; options . defaultLanguageForUnspecified = options . defaultLanguageForUnspecified || options . defaultLanguage ; options . plugins . forEach ( loadPrismPlugin ) ; options . init ( Prism ) ; markdownit . options . highlight = ( text , lang ) => highlight ( markdownit , options , text , lang ) ; markdownit . renderer . rules . fence = applyCodeAttributes ( markdownit , options , markdownit . renderer . rules . fence || ( ( ) => '' ) ) ; }