2020-10-21 00:23:55 +01:00
import { PluginStates } from '../reducer' ;
2020-11-05 16:58:23 +00:00
import { ExtraRendererRule } from '@joplinapp/renderer/MdToHtml' ;
2020-10-21 22:52:58 +01:00
import { ContentScriptType } from '../api/types' ;
2020-10-21 00:23:55 +01:00
export default function contentScriptsToRendererRules ( plugins :PluginStates ) : ExtraRendererRule [ ] {
const output :ExtraRendererRule [ ] = [ ] ;
for ( const pluginId in plugins ) {
const plugin = plugins [ pluginId ] ;
2020-10-21 22:52:58 +01:00
const contentScripts = plugin . contentScripts [ ContentScriptType . MarkdownItPlugin ] ;
if ( ! contentScripts ) continue ;
2020-10-21 00:23:55 +01:00
2020-10-21 22:52:58 +01:00
for ( const contentScript of contentScripts ) {
const module = require ( contentScript . path ) ;
if ( ! module . default || typeof module . default !== 'function' ) throw new Error ( ` Content script must export a function under the "default" key: Plugin: ${ pluginId } : Script: ${ contentScript . id } ` ) ;
2020-10-21 00:23:55 +01:00
2020-10-21 22:52:58 +01:00
const loadedModule = module . default ( { } ) ;
if ( ! loadedModule . plugin ) throw new Error ( ` Content script must export a "plugin" key: Plugin: ${ pluginId } : Script: ${ contentScript . id } ` ) ;
output . push ( {
id : contentScript.id ,
module : loadedModule ,
} ) ;
2020-10-21 00:23:55 +01:00
}
}
return output ;
}