diff --git a/doc/plugins.md b/doc/plugins.md index 663657b4e..2062378bb 100644 --- a/doc/plugins.md +++ b/doc/plugins.md @@ -90,22 +90,34 @@ name of a function exported by the named module. See for how to export a function. For the module name you can omit the `.js` suffix, and if the file is `index.js` -you can use just the directory name. +you can use just the directory name. You can also omit the module name entirely, +in which case it defaults to the plugin name (e.g., `ep_example`). -You can also omit the function name and separating colon. If you do, Etherpad -will look for an exported function whose name matches the name of the hook -(e.g., `authenticate`). You cannot omit the function name if the module name -contains a colon. +You can also omit the function name. If you do, Etherpad will look for an +exported function whose name matches the name of the hook (e.g., +`authenticate`). -For example, all of the following will cause the `authorize` hook to call the -`exports.authorize` function in `index.js` from the `ep_example` plugin: +If either the module name or the function name is omitted (or both), the colon +may also be omitted unless the provided module name contains a colon. (So if the +module name is `C:\foo.js` then the hook function specification with the +function name omitted would be `"C:\\foo.js:"`.) + +Examples: Suppose the plugin name is `ep_example`. All of the following are +equivalent, and will cause the `authorize` hook to call the `exports.authorize` +function in `index.js` from the `ep_example` plugin: * `"authorize": "ep_example/index.js:authorize"` +* `"authorize": "ep_example/index.js:"` * `"authorize": "ep_example/index.js"` * `"authorize": "ep_example/index:authorize"` +* `"authorize": "ep_example/index:"` * `"authorize": "ep_example/index"` * `"authorize": "ep_example:authorize"` +* `"authorize": "ep_example:"` * `"authorize": "ep_example"` +* `"authorize": ":authorize"` +* `"authorize": ":"` +* `"authorize": ""` ### Client hooks and server hooks diff --git a/src/static/js/pluginfw/plugins.js b/src/static/js/pluginfw/plugins.js index be16e1781..b0d1093b1 100644 --- a/src/static/js/pluginfw/plugins.js +++ b/src/static/js/pluginfw/plugins.js @@ -58,11 +58,13 @@ const callInit = async () => { } exports.pathNormalization = function (part, hook_fn_name, hook_name) { - const parts = hook_fn_name.split(':'); - const functionName = (parts.length > 1) ? parts.pop() : hook_name; + const tmp = hook_fn_name.split(':'); // hook_fn_name might be something like 'C:\\foo.js:myFunc'. + // If there is a single colon assume it's 'filename:funcname' not 'C:\\filename'. + const functionName = (tmp.length > 1 ? tmp.pop() : null) || hook_name; + const moduleName = tmp.join(':') || part.plugin; const packageDir = path.dirname(defs.plugins[part.plugin].package.path); - const fileName = path.normalize(path.join(packageDir, parts.join(':'))); - return fileName + ((functionName == null) ? '' : (':' + functionName)); + const fileName = path.normalize(path.join(packageDir, moduleName)); + return `${fileName}:${functionName}`; } exports.update = async function () {