diff --git a/outline-numbering/info.json b/outline-numbering/info.json index c9173cc..98f4b7e 100644 --- a/outline-numbering/info.json +++ b/outline-numbering/info.json @@ -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..." } diff --git a/outline-numbering/outline-numbering.qml b/outline-numbering/outline-numbering.qml index 5160450..51d3532 100644 --- a/outline-numbering/outline-numbering.qml +++ b/outline-numbering/outline-numbering.qml @@ -1,7 +1,6 @@ import QtQml 2.0 import QOwnNotesTypes 1.0 - /** * This script inserts and updates the decimal outline numbering in a document **/ @@ -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