You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-06-24 23:26:50 +02:00
Plugins: Add support for external CodeMirror plugins (#4015)
This commit is contained in:
43
packages/lib/services/plugins/utils/loadContentScripts.ts
Normal file
43
packages/lib/services/plugins/utils/loadContentScripts.ts
Normal file
@ -0,0 +1,43 @@
|
||||
import { PluginStates } from '../reducer';
|
||||
import { ContentScriptType } from '../api/types';
|
||||
import { dirname } from '@joplin/renderer/pathUtils';
|
||||
|
||||
export interface ExtraContentScript {
|
||||
id: string;
|
||||
module: any;
|
||||
assetPath: string;
|
||||
}
|
||||
|
||||
export function contentScriptsToRendererRules(plugins: PluginStates): ExtraContentScript[] {
|
||||
return loadContentScripts(plugins, ContentScriptType.MarkdownItPlugin);
|
||||
}
|
||||
|
||||
export function contentScriptsToCodeMirrorPlugin(plugins: PluginStates): ExtraContentScript[] {
|
||||
return loadContentScripts(plugins, ContentScriptType.CodeMirrorPlugin);
|
||||
}
|
||||
|
||||
function loadContentScripts(plugins: PluginStates, scriptType: ContentScriptType): ExtraContentScript[] {
|
||||
const output: ExtraContentScript[] = [];
|
||||
|
||||
for (const pluginId in plugins) {
|
||||
const plugin = plugins[pluginId];
|
||||
const contentScripts = plugin.contentScripts[scriptType];
|
||||
if (!contentScripts) continue;
|
||||
|
||||
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}`);
|
||||
|
||||
const loadedModule = module.default({});
|
||||
if (!loadedModule.plugin && !loadedModule.codeMirrorResources && !loadedModule.codeMirrorOptions) throw new Error(`Content script must export a "plugin" key or a list of CodeMirror assets or define a CodeMirror option: Plugin: ${pluginId}: Script: ${contentScript.id}`);
|
||||
|
||||
output.push({
|
||||
id: contentScript.id,
|
||||
module: loadedModule,
|
||||
assetPath: dirname(contentScript.path),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
Reference in New Issue
Block a user