Skip to content

Commit cb7c0e3

Browse files
authored
#248 fix: numbering sequence off after 4th heading
* Update outline-numbering.qml Fixed numbering that was incorrect for deeper level headings. * Update info.json Changed version number from 1.0.0 to 1.0.1
1 parent 0fd5681 commit cb7c0e3

File tree

2 files changed

+44
-39
lines changed

2 files changed

+44
-39
lines changed

outline-numbering/info.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"script": "outline-numbering.qml",
55
"authors": ["@jerksen"],
66
"platforms": ["linux", "macos", "windows"],
7-
"version": "1.0.0",
7+
"version": "1.0.1",
88
"minAppVersion": "20.5.6",
99
"description" : "This script will insert/update decimal outline numbers for all headings in the current note\n\nDecimal Outline numbering is assigning tiered numbers to the headings based on their level and will result in something like this:\n\n# 1 Introduction\n## 1.1 Background\n### 1.1.1 In the beginning\n##1.2 Purpose\n..."
1010
}
+43-38
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import QtQml 2.0
22
import QOwnNotesTypes 1.0
33

4-
54
/**
65
* This script inserts and updates the decimal outline numbering in a document
76
**/
@@ -16,86 +15,92 @@ Script {
1615
}
1716

1817
/**
19-
* this function finds all the headings and overwrites them with decimal outline numbering
18+
* This function finds all the headings and overwrites them with decimal outline numbering
2019
*
2120
* @param lines an array of strings containing the lines of the document
2221
* @return lines an array of strings with the heading lines updated
2322
**/
2423
function addOlNumbers(lines) {
25-
// set the current nums to their start
26-
var curNums = [0,0,0,0,0,0];
24+
// Set the current numbers to their start
25+
var curNums = [0, 0, 0, 0, 0, 0];
2726
var depth = 0;
2827
var last_depth = 0;
2928

30-
// go through all the lines
29+
// Go through all the lines
3130
for (var n = 0; n < lines.length; n++) {
32-
// if we found a heading
31+
// If we found a heading
3332
var match = lines[n].match(/^(#+)\s*([0-9\.]*)\s+(.*)$/);
3433
if (match) {
35-
36-
// get the depth - the heading number
34+
// Get the depth - the heading number
3735
depth = match[1].length - 1;
3836

39-
// if the current depth is at a higher level than the last, reset all the lower level vals
37+
// If the current depth is at a higher level than the last, reset all the lower level values
4038
if (depth < last_depth) {
41-
for (var j = depth; n < curNums.length ; n++) {
42-
curNums[j] == 0;
39+
for (var j = depth + 1; j < curNums.length; j++) {
40+
curNums[j] = 0; // Reset lower-level numbering
4341
}
4442
}
45-
46-
// up the val for the current depth and save this depth as the last one
43+
44+
// Increment the value for the current depth and save this depth as the last one
4745
curNums[depth] += 1;
4846
last_depth = depth;
49-
50-
// rewrite the currentt line with the number
47+
48+
// Rewrite the current line with the number
5149
lines[n] = match[1] + " " + getOlNumber(curNums, depth) + " " + match[3];
52-
};
50+
}
5351
}
5452
return lines;
5553
}
56-
54+
5755
/**
58-
* based on the current depth and the current digits, return the outline number string
59-
* which is the first depth number of elements in the nums array joined by a "."
60-
*
61-
* @param nums a 6 element array containing the current oultline numbering values
56+
* Based on the current depth and the current digits, return the outline number string
57+
* which is the first depth number of elements in the nums array joined by "."
58+
*
59+
* @param nums a 6-element array containing the current outline numbering values
6260
* @param depth the current depth that we want a number for
63-
* @return string containing #depth numbers seperated by "."s
61+
* @return string containing #depth numbers separated by "."s
6462
*
65-
* example: getOlNumber([1,2,3,4,5,6], 4) returns "1.2.3.4"
63+
* Example: getOlNumber([1,2,3,4,5,6], 4) returns "1.2.3.4"
6664
**/
6765
function getOlNumber(nums, depth) {
66+
var num = "";
67+
for (var n = 0; n < depth + 1; n++) {
68+
num += nums[n];
6869

69-
var num = "";
70-
for (var n=0; n<depth+1; n++) {
71-
num += nums[n];
72-
73-
// only add the delim if there are more numbers to get
74-
if (n < depth) { num += ".";};
70+
// Only add the delimiter if there are more numbers to get
71+
if (n < depth) {
72+
num += ".";
7573
}
76-
return num;
7774
}
7875

76+
// Ensure that the number always ends with a full stop
77+
num += ".";
78+
79+
return num;
80+
}
81+
82+
7983
/**
80-
* this function is invoked when a custom action is triggered
84+
* This function is invoked when a custom action is triggered
8185
*
82-
* @param action string identifier defined in registerCumstomAction
86+
* @param action string identifier defined in registerCustomAction
8387
**/
8488
function customActionInvoked(action) {
85-
if (action == "outlineNumbering")
86-
// get the document and update the lines
89+
if (action == "outlineNumbering") {
90+
// Get the document and update the lines
8791
var lines = script.currentNote().noteText.split("\n");
8892
var updated_lines = addOlNumbers(lines);
89-
90-
// save the current cursor position
93+
94+
// Save the current cursor position
9195
var cursorPositionStart = script.noteTextEditSelectionStart();
9296
var cursorPositionEnd = script.noteTextEditSelectionEnd();
9397

94-
// select all and overwrite with the new text
98+
// Select all and overwrite with the new text
9599
script.noteTextEditSelectAll();
96100
script.noteTextEditWrite(updated_lines.join("\n"));
97101

98-
// restore the cursor position
102+
// Restore the cursor position
99103
script.noteTextEditSetSelection(cursorPositionStart, cursorPositionEnd);
100104
}
101105
}
106+
}

0 commit comments

Comments
 (0)