2020-10-21 00:23:55 +01:00
import { PluginStates } from '../reducer' ;
2020-11-07 15:59:37 +00:00
import { ExtraRendererRule } from '@joplin/renderer/MdToHtml' ;
2020-10-21 22:52:58 +01:00
import { ContentScriptType } from '../api/types' ;
2020-10-21 00:23:55 +01:00
2020-11-12 19:13:28 +00:00
export default function contentScriptsToRendererRules ( plugins : PluginStates ) : ExtraRendererRule [ ] {
const output : ExtraRendererRule [ ] = [ ] ;
2020-10-21 00:23:55 +01:00
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 ;
}