From 45fd167d0b9b2836e163f95dd65fab4327d9c781 Mon Sep 17 00:00:00 2001 From: PREVOST Romain Date: Mon, 20 Aug 2018 14:32:41 +0200 Subject: [PATCH 1/2] Extend insert-toc to generate links to sections --- insert-toc/info.json | 2 +- insert-toc/insert-toc.qml | 46 +++++++++++++++++++++++++++++---------- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/insert-toc/info.json b/insert-toc/info.json index 2987cd0..caf0a00 100644 --- a/insert-toc/info.json +++ b/insert-toc/info.json @@ -2,7 +2,7 @@ "name": "Insert Table of Contents (TOC)", "identifier": "insert-toc", "script": "insert-toc.qml", - "authors": ["@flopp"], + "authors": ["@flopp", "@toindev"], "platforms": ["linux", "macos", "windows"], "version": "0.0.1", "minAppVersion": "17.06.2", diff --git a/insert-toc/insert-toc.qml b/insert-toc/insert-toc.qml index 00b577b..842038d 100644 --- a/insert-toc/insert-toc.qml +++ b/insert-toc/insert-toc.qml @@ -1,12 +1,13 @@ import QtQml 2.2 import QOwnNotesTypes 1.0 -/// Extract a 'table of contents' from the current note's headings and insert +/// Extract a 'table of contents' from the current note's headings and insert /// it into the note. -Script { +Script { property string tocTitle - + property bool tocLinks + property variant settingsVariables: [ { "identifier": "tocTitle", @@ -15,12 +16,19 @@ Script { "type": "string", "default": "Table of Contents", }, + { + "identifier": "tocLinks", + "name": "Generate links to sections", + "description": "", + "type": "boolean", + "default": false, + }, ] - + function init() { script.registerCustomAction("insertToc", "Insert TOC", "TOC", "", true) } - + function extractTOC(lines) { var toc = []; for (var n = 0; n < lines.length; n++) { @@ -28,13 +36,21 @@ Script { if (match) { toc.push({ "depth": match[1].length, - "title": match[2].trim() + "title": match[2].trim(), + "link": extractLink(match[2].trim()) }); } } return toc; } - + + function extractLink(title) { + var lowercase = title.toLowerCase() + var spaceReplaced = lowercase.replace(/ /g, "-") + var invalidCharsRemoved = spaceReplaced.replace(/[^A-Za-zÀ-ÿ-_]/g, "") + return invalidCharsRemoved; + } + function normalizeDepths(toc) { var min = -1; for (var n = 0; n < toc.length; n++) { @@ -46,10 +62,10 @@ Script { for (var n = 0; n < toc.length; n++) { toc[n].depth -= min; } - + return toc; } - + function indent(depth) { var s = ""; for (var i = 0; i < depth; i++) { @@ -57,13 +73,13 @@ Script { } return s; } - + function customActionInvoked(action) { if (action == "insertToc") { var lines = script.currentNote().noteText.split("\n"); var toc = extractTOC(lines); toc = normalizeDepths(toc); - + script.noteTextEditWrite("\n" + tocTitle + "\n\n"); var indexByDepth = {}; for (var n = 0; n < toc.length; n++) { @@ -73,13 +89,19 @@ Script { for (var n = 0; n < toc.length; n++) { var depth = toc[n].depth; var title = toc[n].title; + var link = toc[n].link; if (depth > lastDepth) { indexByDepth[depth] = 1; } else { indexByDepth[depth] += 1; } lastDepth = depth; - script.noteTextEditWrite(indent(depth) + indexByDepth[depth] + ". " + title + "\n"); + + if (tocLinks) { + script.noteTextEditWrite(indent(depth) + indexByDepth[depth] + ". [" + title + "](#" + link + ")\n"); + } else { + script.noteTextEditWrite(indent(depth) + indexByDepth[depth] + ". " + title + "\n"); + } } } } From 93dcea5f945bc762438e06b8b8b4f8739bf97e76 Mon Sep 17 00:00:00 2001 From: PREVOST Romain Date: Mon, 20 Aug 2018 15:49:01 +0200 Subject: [PATCH 2/2] Update insert-toc version number --- insert-toc/info.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/insert-toc/info.json b/insert-toc/info.json index caf0a00..ad19306 100644 --- a/insert-toc/info.json +++ b/insert-toc/info.json @@ -4,7 +4,7 @@ "script": "insert-toc.qml", "authors": ["@flopp", "@toindev"], "platforms": ["linux", "macos", "windows"], - "version": "0.0.1", + "version": "0.0.2", "minAppVersion": "17.06.2", "description" : "This script scans the current note for headlines, generates a table of contents, and inserts a the TOC into the note." }