From aff93d24dd0cde9eaae55db2f5770d71c51a0515 Mon Sep 17 00:00:00 2001 From: John McLear Date: Sat, 6 Jun 2020 14:30:13 +0100 Subject: [PATCH] export: txt import include correct OL prefix (#4086) Includes test coverage Final part of solving https://github.com/ether/etherpad-lite/issues/3447 --- src/node/utils/ExportTxt.js | 47 ++++++++++++++++++++++--- tests/backend/specs/api/importexport.js | 13 ++++++- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/node/utils/ExportTxt.js b/src/node/utils/ExportTxt.js index 304f77b8a..4a9c0ba40 100644 --- a/src/node/utils/ExportTxt.js +++ b/src/node/utils/ExportTxt.js @@ -190,7 +190,12 @@ function getTXTFromAtext(pad, atext, authorColors) // so we want to do something reasonable there. We also // want to deal gracefully with blank lines. // => keeps track of the parents level of indentation + + var listNumbers = {}; + var prevListLevel; + for (var i = 0; i < textLines.length; i++) { + var line = _analyzeLine(textLines[i], attribLines[i], apool); var lineContent = getLineTXT(line.text, line.aline); @@ -198,15 +203,49 @@ function getTXTFromAtext(pad, atext, authorColors) lineContent = "* " + lineContent; // add a bullet } + if (line.listTypeName !== "number") { + // We're no longer in an OL so we can reset counting + for (var key in listNumbers) { + delete listNumbers[key]; + } + } + if (line.listLevel > 0) { for (var j = line.listLevel - 1; j >= 0; j--) { - pieces.push('\t'); + pieces.push('\t'); // tab indent list numbers.. + if(!listNumbers[line.listLevel]){ + listNumbers[line.listLevel] = 0; + } } if (line.listTypeName == "number") { - pieces.push(line.listLevel + ". "); - // This is bad because it doesn't truly reflect what the user - // sees because browsers do magic on nested
  1. s + /* + * listLevel == amount of indentation + * listNumber(s) == item number + * + * Example: + * 1. foo + * 1.1 bah + * 2. latte + * 2.1 latte + * + * To handle going back to 2.1 when prevListLevel is lower number + * than current line.listLevel then reset the object value + */ + if(line.listLevel < prevListLevel){ + delete listNumbers[prevListLevel]; + } + + listNumbers[line.listLevel]++; + if(line.listLevel > 1){ + var x = 1; + while(x <= line.listLevel-1){ + pieces.push(listNumbers[x]+".") + x++; + } + } + pieces.push(listNumbers[line.listLevel]+". ") + prevListLevel = line.listLevel; } pieces.push(lineContent, '\n'); diff --git a/tests/backend/specs/api/importexport.js b/tests/backend/specs/api/importexport.js index 4635c88b1..685bb7241 100644 --- a/tests/backend/specs/api/importexport.js +++ b/tests/backend/specs/api/importexport.js @@ -36,8 +36,19 @@ var testImports = { expectedHTML: '
    • FOO

    ', expectedText: '\t* FOO\n\n' }, + "prefixcorrectlinenumber":{ + input: '
    1. should be 1
    2. should be 2
    ', + expectedHTML: '
    1. should be 1
    2. should be 2

    ', + expectedText: '\t1. should be 1\n\t2. should be 2\n\n' + }, + "prefixcorrectlinenumbernested":{ + input: '
    1. should be 1
      1. foo
    2. should be 2
    ', + expectedHTML: '
    1. should be 1
      1. foo
    2. should be 2

    ', + expectedText: '\t1. should be 1\n\t\t1.1. foo\n\t2. should be 2\n\n' + }, + /* - "prefixcorrectlinenumber #3450":{ + "prefixcorrectlinenumber when introduced none list item - currently not supported see #3450":{ input: '
    1. should be 1
    2. test
    3. should be 2
    ', expectedHTML: '
    1. should be 1
    2. test
    3. should be 2

    ', expectedText: '\t1. should be 1\n\ttest\n\t2. should be 2\n\n'