2018-01-26 15:29:34 +00:00
|
|
|
import QtQml 2.2
|
|
|
|
import QOwnNotesTypes 1.0
|
|
|
|
|
2018-08-20 12:32:41 +00:00
|
|
|
/// Extract a 'table of contents' from the current note's headings and insert
|
2018-01-26 15:29:34 +00:00
|
|
|
/// it into the note.
|
|
|
|
|
2018-08-20 12:32:41 +00:00
|
|
|
Script {
|
2018-01-26 15:29:34 +00:00
|
|
|
property string tocTitle
|
2018-08-20 12:32:41 +00:00
|
|
|
property bool tocLinks
|
|
|
|
|
2018-01-26 15:29:34 +00:00
|
|
|
property variant settingsVariables: [
|
|
|
|
{
|
|
|
|
"identifier": "tocTitle",
|
|
|
|
"name": "Title of the table of contents",
|
|
|
|
"description": "",
|
|
|
|
"type": "string",
|
|
|
|
"default": "Table of Contents",
|
|
|
|
},
|
2018-08-20 12:32:41 +00:00
|
|
|
{
|
|
|
|
"identifier": "tocLinks",
|
|
|
|
"name": "Generate links to sections",
|
|
|
|
"description": "",
|
|
|
|
"type": "boolean",
|
|
|
|
"default": false,
|
|
|
|
},
|
2018-01-26 15:29:34 +00:00
|
|
|
]
|
2018-08-20 12:32:41 +00:00
|
|
|
|
2018-01-26 15:29:34 +00:00
|
|
|
function init() {
|
|
|
|
script.registerCustomAction("insertToc", "Insert TOC", "TOC", "", true)
|
|
|
|
}
|
2018-08-20 12:32:41 +00:00
|
|
|
|
2018-01-26 15:29:34 +00:00
|
|
|
function extractTOC(lines) {
|
|
|
|
var toc = [];
|
|
|
|
for (var n = 0; n < lines.length; n++) {
|
|
|
|
var match = lines[n].match(/^(#+)\s+(.*)$/)
|
|
|
|
if (match) {
|
|
|
|
toc.push({
|
|
|
|
"depth": match[1].length,
|
2018-08-20 12:32:41 +00:00
|
|
|
"title": match[2].trim(),
|
|
|
|
"link": extractLink(match[2].trim())
|
2018-01-26 15:29:34 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return toc;
|
|
|
|
}
|
2018-08-20 12:32:41 +00:00
|
|
|
|
|
|
|
function extractLink(title) {
|
|
|
|
var lowercase = title.toLowerCase()
|
|
|
|
var spaceReplaced = lowercase.replace(/ /g, "-")
|
|
|
|
var invalidCharsRemoved = spaceReplaced.replace(/[^A-Za-zÀ-ÿ-_]/g, "")
|
|
|
|
return invalidCharsRemoved;
|
|
|
|
}
|
|
|
|
|
2018-01-26 15:29:34 +00:00
|
|
|
function normalizeDepths(toc) {
|
|
|
|
var min = -1;
|
|
|
|
for (var n = 0; n < toc.length; n++) {
|
|
|
|
var depth = toc[n].depth;
|
|
|
|
if (min < 0 || depth < min) {
|
|
|
|
min = depth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (var n = 0; n < toc.length; n++) {
|
|
|
|
toc[n].depth -= min;
|
|
|
|
}
|
2018-08-20 12:32:41 +00:00
|
|
|
|
2018-01-26 15:29:34 +00:00
|
|
|
return toc;
|
|
|
|
}
|
2018-08-20 12:32:41 +00:00
|
|
|
|
2018-01-26 15:29:34 +00:00
|
|
|
function indent(depth) {
|
|
|
|
var s = "";
|
|
|
|
for (var i = 0; i < depth; i++) {
|
|
|
|
s += " ";
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
2018-08-20 12:32:41 +00:00
|
|
|
|
2018-01-26 15:29:34 +00:00
|
|
|
function customActionInvoked(action) {
|
|
|
|
if (action == "insertToc") {
|
|
|
|
var lines = script.currentNote().noteText.split("\n");
|
|
|
|
var toc = extractTOC(lines);
|
|
|
|
toc = normalizeDepths(toc);
|
2018-08-20 12:32:41 +00:00
|
|
|
|
2018-01-26 15:29:34 +00:00
|
|
|
script.noteTextEditWrite("\n" + tocTitle + "\n\n");
|
|
|
|
var indexByDepth = {};
|
|
|
|
for (var n = 0; n < toc.length; n++) {
|
|
|
|
indexByDepth[toc[n].depth] = 0;
|
|
|
|
}
|
|
|
|
var lastDepth = 0;
|
|
|
|
for (var n = 0; n < toc.length; n++) {
|
|
|
|
var depth = toc[n].depth;
|
|
|
|
var title = toc[n].title;
|
2018-08-20 12:32:41 +00:00
|
|
|
var link = toc[n].link;
|
2018-01-26 15:29:34 +00:00
|
|
|
if (depth > lastDepth) {
|
|
|
|
indexByDepth[depth] = 1;
|
|
|
|
} else {
|
|
|
|
indexByDepth[depth] += 1;
|
|
|
|
}
|
|
|
|
lastDepth = depth;
|
2018-08-20 12:32:41 +00:00
|
|
|
|
|
|
|
if (tocLinks) {
|
|
|
|
script.noteTextEditWrite(indent(depth) + indexByDepth[depth] + ". [" + title + "](#" + link + ")\n");
|
|
|
|
} else {
|
|
|
|
script.noteTextEditWrite(indent(depth) + indexByDepth[depth] + ". " + title + "\n");
|
|
|
|
}
|
2018-01-26 15:29:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|