Skip to content

Commit e674236

Browse files
authored
Add script "Insert Table of Contents II (Map of Contents)" (#273)
* Create insert-moc.json * Update insert-moc.json * Create insert-moc.qml * Update insert-moc.qml * Update and rename insert-moc.json to info.json - changed identifier to "insert-moc" - changed script to insert-moc.qml - updated the minAppVersion to 25.12.7 - extended the description. * Update info.json - Removed `"` and replaced it with `'`to give the .json a correct form. - I did not delete the `"Insert"`, as in the very similar "Table of Contents" script "name" descriptor is called: - "Insert Table of Contents (TOC)" - Mine: "Insert Table of Contents II (M.O.C. Map of contents)" If you really want me to I will will remove the name. As a user I just think it is convenient that those 2 scripts are just beneth each other in the script repository menu in QN.
1 parent c2960a8 commit e674236

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

insert-moc/info.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "Insert Table of Contents II (M.O.C. Map of Contents)",
3+
"identifier": "insert-moc",
4+
"script": "insert-moc.qml",
5+
"authors": ["@netlimpopo"],
6+
"platforms": ["linux", "macos", "windows"],
7+
"version": "0.0.1",
8+
"minAppVersion": "25.12.7",
9+
"description": "This script generates a list of clickable links which map to every single <code>.md</code> note within the active note folder (= 'Map of contents (M.O.C.)'). It displays the links in a table format along with the <em>relative filepaths of the files</em>. Furthermore it sorts the links in alphabetical order. \n The difference to the 'Table of Contents' script lies in the fact, that the T.O.C script creates links to headings of the same document whereas this M.O.C. script creates references to every single notename in the active note folder.\n <strong>Usage</strong> \n. <ol> <li>Select the highest folder hierachy <strong>(!)</strong>.</li> <li>Create a new note with an arbitrary name in the highest note hierachy (note level).</li><li>Click the M.O.C button in the script menu. Then the script will overwrite all of the current note content and will insert the linkslist.</li> </ol> \n\n <strong>Warning</strong> \n Note that <strong>all of the content</strong> of the selcted note <strong>will be replaced</strong> by the table of contents link list. Therefore it is essential to create a new note and exclusively use this note for the M.O.C list.\n <strong>automatic updates</strong>\n In case you rename a note, the markdown links in the M.O.C will be automatically updated too, as QOwnNotes watches all markdownlinks and checks links for necessary updates. \n However, if <strong>one creates a new note anywhere, the script must be excecuted again</strong>, as it will otherwise not watch for file additions nor will it add a new link to the list.\n Also if you want to move the M.O.C file to a different location, all the links will be updated accordingly thanks to the QOwnNotes logic. \n An M.O.C. can act as a navitational site (especially when all files are exported as static .html pages) and provide an overview of all available notes. The idea is strongly influenced by the Obsidian plugin 'Waypoint' see <a href='https://github.com/IdreesInc/Waypoint'>Waypoint Obsidian</a>. "
10+
}

insert-moc/insert-moc.qml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import QtQml 2.2
2+
import QOwnNotesTypes 1.0
3+
4+
Script {
5+
function init() {
6+
// Register custom action for MOC index generation
7+
script.registerCustomAction("MOC", "Generate MOC", "MOC");
8+
}
9+
10+
function customActionInvoked(action) {
11+
if (action !== "indexpage") {
12+
return;
13+
}
14+
15+
var currentNote = script.currentNote;
16+
var noteIds = script.fetchNoteIdsByNoteTextPart(""); // get all note IDs
17+
18+
// Build each link line and collect into array
19+
var listedLinks = [];
20+
for (var i = 0; i < noteIds.length; i++) {
21+
var noteId = noteIds[i];
22+
var noteObj = script.fetchNoteById(noteId);
23+
var name = noteObj.name;
24+
var fullPath = noteObj.fullNoteFilePath;
25+
var parts = fullPath.split('.');
26+
var ext = parts[parts.length - 1];
27+
var relDir = noteObj.relativeNoteFileDirPath;
28+
29+
// Construct filePath and encoded path
30+
var filePath = relDir && relDir.length > 0
31+
? relDir + "/" + name + "." + ext
32+
: name + "." + ext;
33+
var rawLink = (relDir && relDir.length > 0
34+
? relDir + "/" + name
35+
: name);
36+
var encoded = encodeURIComponent(rawLink)
37+
.replace(/%2F/g, "/")
38+
.replace(/%2E/g, ".") + "." + ext;
39+
40+
// Format line
41+
var line = "|" + filePath + " | ---> [" + name + "." + ext + "](" + encoded + ")| ";
42+
listedLinks.push(line);
43+
}
44+
45+
// Sort lines alphabetically
46+
listedLinks.sort();
47+
48+
// Assemble new note text
49+
var noteTextstring = "# Map of Content \n\n|path |filelink | \n|---|---| \n" + listedLinks.join("\n") + "\n";
50+
51+
// Replace entire note content
52+
script.noteTextEditSelectAll();
53+
script.noteTextEditWrite(noteTextstring);
54+
script.log("MOC index generated and sorted successfully.");
55+
}
56+
}

0 commit comments

Comments
 (0)