|
| 1 | +import QtQml 2.0 |
| 2 | + |
| 3 | +/** |
| 4 | + * This script uses kanboard-cli to fetch the title of Kanboard task URLs. |
| 5 | + * |
| 6 | + * Dependency: |
| 7 | + * https://github.com/digital-blueprint/kanboard-cli |
| 8 | + */ |
| 9 | +QtObject { |
| 10 | + property string kanboardCliPath |
| 11 | + property string kanbanUrlPrefixes |
| 12 | + |
| 13 | + property variant settingsVariables: [ |
| 14 | + { |
| 15 | + "identifier": "kanboardCliPath", |
| 16 | + "name": "kanboard-cli path", |
| 17 | + "description": "Please select the path to your kanboard-cli executable:", |
| 18 | + "type": "file", |
| 19 | + "default": "kanboard-cli" |
| 20 | + }, |
| 21 | + { |
| 22 | + "identifier": "kanbanUrlPrefixes", |
| 23 | + "name": "Kanban URL prefixes", |
| 24 | + "description": "Enter Kanboard base URL prefixes, one per line. Matching task URLs below those prefixes will be resolved with kanboard-cli.", |
| 25 | + "type": "text", |
| 26 | + "default": "https://kanboard.example.com" |
| 27 | + }, |
| 28 | + ] |
| 29 | + |
| 30 | + /** |
| 31 | + * This function is called before QOwnNotes tries to fetch a webpage title |
| 32 | + * in the link dialog. |
| 33 | + * |
| 34 | + * @param url string the URL whose title should be fetched |
| 35 | + * @return string the title for the URL, or an empty string to let QOwnNotes fetch it |
| 36 | + */ |
| 37 | + function fetchUrlTitleHook(url) { |
| 38 | + var taskId = taskIdFromConfiguredPrefix(url); |
| 39 | + |
| 40 | + if (taskId === "") { |
| 41 | + return ""; |
| 42 | + } |
| 43 | + |
| 44 | + var result = script.startSynchronousProcess(kanboardCliPath, ["--json", "task", "get", taskId]); |
| 45 | + |
| 46 | + try { |
| 47 | + var task = JSON.parse(String(result)); |
| 48 | + return task.title || ""; |
| 49 | + } catch (error) { |
| 50 | + script.log("Could not fetch Kanboard task title: " + error); |
| 51 | + } |
| 52 | + |
| 53 | + return ""; |
| 54 | + } |
| 55 | + |
| 56 | + function taskIdFromConfiguredPrefix(url) { |
| 57 | + var prefixes = kanbanUrlPrefixes.split(/\r?\n/); |
| 58 | + |
| 59 | + for (var i = 0; i < prefixes.length; i++) { |
| 60 | + var prefix = prefixes[i].trim(); |
| 61 | + |
| 62 | + if (prefix === "" || url.indexOf(prefix) !== 0) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + var rest = url.substring(prefix.length); |
| 67 | + var taskIdMatch = /^(?:\/)?task\/(\d+)(?:[/?#]|$)/.exec(rest); |
| 68 | + |
| 69 | + if (taskIdMatch !== null) { |
| 70 | + return taskIdMatch[1]; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return ""; |
| 75 | + } |
| 76 | +} |
0 commit comments