Skip to content

Commit

Permalink
#248 fix: numbering sequence off after 4th heading
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
DutchPete authored Dec 20, 2024
1 parent 0fd5681 commit cb7c0e3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 39 deletions.
2 changes: 1 addition & 1 deletion outline-numbering/info.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"script": "outline-numbering.qml",
"authors": ["@jerksen"],
"platforms": ["linux", "macos", "windows"],
"version": "1.0.0",
"version": "1.0.1",
"minAppVersion": "20.5.6",
"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..."
}
81 changes: 43 additions & 38 deletions outline-numbering/outline-numbering.qml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import QtQml 2.0
import QOwnNotesTypes 1.0


/**
* This script inserts and updates the decimal outline numbering in a document
**/
Expand All @@ -16,86 +15,92 @@ Script {
}

/**
* this function finds all the headings and overwrites them with decimal outline numbering
* This function finds all the headings and overwrites them with decimal outline numbering
*
* @param lines an array of strings containing the lines of the document
* @return lines an array of strings with the heading lines updated
**/
function addOlNumbers(lines) {
// set the current nums to their start
var curNums = [0,0,0,0,0,0];
// Set the current numbers to their start
var curNums = [0, 0, 0, 0, 0, 0];
var depth = 0;
var last_depth = 0;

// go through all the lines
// Go through all the lines
for (var n = 0; n < lines.length; n++) {
// if we found a heading
// If we found a heading
var match = lines[n].match(/^(#+)\s*([0-9\.]*)\s+(.*)$/);
if (match) {

// get the depth - the heading number
// Get the depth - the heading number
depth = match[1].length - 1;

// if the current depth is at a higher level than the last, reset all the lower level vals
// If the current depth is at a higher level than the last, reset all the lower level values
if (depth < last_depth) {
for (var j = depth; n < curNums.length ; n++) {
curNums[j] == 0;
for (var j = depth + 1; j < curNums.length; j++) {
curNums[j] = 0; // Reset lower-level numbering
}
}
// up the val for the current depth and save this depth as the last one

// Increment the value for the current depth and save this depth as the last one
curNums[depth] += 1;
last_depth = depth;
// rewrite the currentt line with the number

// Rewrite the current line with the number
lines[n] = match[1] + " " + getOlNumber(curNums, depth) + " " + match[3];
};
}
}
return lines;
}

/**
* based on the current depth and the current digits, return the outline number string
* which is the first depth number of elements in the nums array joined by a "."
*
* @param nums a 6 element array containing the current oultline numbering values
* Based on the current depth and the current digits, return the outline number string
* which is the first depth number of elements in the nums array joined by "."
*
* @param nums a 6-element array containing the current outline numbering values
* @param depth the current depth that we want a number for
* @return string containing #depth numbers seperated by "."s
* @return string containing #depth numbers separated by "."s
*
* example: getOlNumber([1,2,3,4,5,6], 4) returns "1.2.3.4"
* Example: getOlNumber([1,2,3,4,5,6], 4) returns "1.2.3.4"
**/
function getOlNumber(nums, depth) {
var num = "";
for (var n = 0; n < depth + 1; n++) {
num += nums[n];

var num = "";
for (var n=0; n<depth+1; n++) {
num += nums[n];

// only add the delim if there are more numbers to get
if (n < depth) { num += ".";};
// Only add the delimiter if there are more numbers to get
if (n < depth) {
num += ".";
}
return num;
}

// Ensure that the number always ends with a full stop
num += ".";

return num;
}


/**
* this function is invoked when a custom action is triggered
* This function is invoked when a custom action is triggered
*
* @param action string identifier defined in registerCumstomAction
* @param action string identifier defined in registerCustomAction
**/
function customActionInvoked(action) {
if (action == "outlineNumbering")
// get the document and update the lines
if (action == "outlineNumbering") {
// Get the document and update the lines
var lines = script.currentNote().noteText.split("\n");
var updated_lines = addOlNumbers(lines);
// save the current cursor position

// Save the current cursor position
var cursorPositionStart = script.noteTextEditSelectionStart();
var cursorPositionEnd = script.noteTextEditSelectionEnd();

// select all and overwrite with the new text
// Select all and overwrite with the new text
script.noteTextEditSelectAll();
script.noteTextEditWrite(updated_lines.join("\n"));

// restore the cursor position
// Restore the cursor position
script.noteTextEditSetSelection(cursorPositionStart, cursorPositionEnd);
}
}
}

0 comments on commit cb7c0e3

Please sign in to comment.