mirror of
https://github.com/marty-oehme/scripts.git
synced 2024-11-18 07:48:07 +00:00
Merge pull request #37 from toindev/toc-links
Extend insert-toc to generate links to sections
This commit is contained in:
commit
5f2eb256ea
2 changed files with 36 additions and 14 deletions
|
@ -2,9 +2,9 @@
|
||||||
"name": "Insert Table of Contents (TOC)",
|
"name": "Insert Table of Contents (TOC)",
|
||||||
"identifier": "insert-toc",
|
"identifier": "insert-toc",
|
||||||
"script": "insert-toc.qml",
|
"script": "insert-toc.qml",
|
||||||
"authors": ["@flopp"],
|
"authors": ["@flopp", "@toindev"],
|
||||||
"platforms": ["linux", "macos", "windows"],
|
"platforms": ["linux", "macos", "windows"],
|
||||||
"version": "0.0.1",
|
"version": "0.0.2",
|
||||||
"minAppVersion": "17.06.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."
|
"description" : "This script scans the current note for headlines, generates a table of contents, and inserts a the TOC into the note."
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import QOwnNotesTypes 1.0
|
||||||
|
|
||||||
Script {
|
Script {
|
||||||
property string tocTitle
|
property string tocTitle
|
||||||
|
property bool tocLinks
|
||||||
|
|
||||||
property variant settingsVariables: [
|
property variant settingsVariables: [
|
||||||
{
|
{
|
||||||
|
@ -15,6 +16,13 @@ Script {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"default": "Table of Contents",
|
"default": "Table of Contents",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"identifier": "tocLinks",
|
||||||
|
"name": "Generate links to sections",
|
||||||
|
"description": "",
|
||||||
|
"type": "boolean",
|
||||||
|
"default": false,
|
||||||
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
|
@ -28,13 +36,21 @@ Script {
|
||||||
if (match) {
|
if (match) {
|
||||||
toc.push({
|
toc.push({
|
||||||
"depth": match[1].length,
|
"depth": match[1].length,
|
||||||
"title": match[2].trim()
|
"title": match[2].trim(),
|
||||||
|
"link": extractLink(match[2].trim())
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return toc;
|
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) {
|
function normalizeDepths(toc) {
|
||||||
var min = -1;
|
var min = -1;
|
||||||
for (var n = 0; n < toc.length; n++) {
|
for (var n = 0; n < toc.length; n++) {
|
||||||
|
@ -73,13 +89,19 @@ Script {
|
||||||
for (var n = 0; n < toc.length; n++) {
|
for (var n = 0; n < toc.length; n++) {
|
||||||
var depth = toc[n].depth;
|
var depth = toc[n].depth;
|
||||||
var title = toc[n].title;
|
var title = toc[n].title;
|
||||||
|
var link = toc[n].link;
|
||||||
if (depth > lastDepth) {
|
if (depth > lastDepth) {
|
||||||
indexByDepth[depth] = 1;
|
indexByDepth[depth] = 1;
|
||||||
} else {
|
} else {
|
||||||
indexByDepth[depth] += 1;
|
indexByDepth[depth] += 1;
|
||||||
}
|
}
|
||||||
lastDepth = depth;
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue