diff --git a/lib/helpers/line-meta.coffee b/lib/helpers/line-meta.coffee index 83a52e1c..3ec28289 100644 --- a/lib/helpers/line-meta.coffee +++ b/lib/helpers/line-meta.coffee @@ -1,12 +1,12 @@ utils = require "../utils" -LIST_UL_TASK_REGEX = /// ^ (\s*) ([*+-\.]) \s+ \[[xX\ ]\] \s* (.*) $ /// -LIST_UL_REGEX = /// ^ (\s*) ([*+-\.]) \s+ (.*) $ /// -LIST_OL_TASK_REGEX = /// ^ (\s*) (\d+)([\.\)]) \s+ \[[xX\ ]\] \s* (.*) $ /// -LIST_OL_REGEX = /// ^ (\s*) (\d+)([\.\)]) \s+ (.*) $ /// -LIST_AL_TASK_REGEX = /// ^ (\s*) ([a-zA-Z]+)([\.\)]) \s+ \[[xX\ ]\] \s* (.*) $ /// -LIST_AL_REGEX = /// ^ (\s*) ([a-zA-Z]+)([\.\)]) \s+ (.*) $ /// -BLOCKQUOTE_REGEX = /// ^ (\s*) (>) \s* (.*) $ /// +LIST_UL_TASK_REGEX = /// ^ (\s*) ([*+-\.]) \s+ \[[xX\ ]\] (?:\s+ (.*))? $ /// +LIST_UL_REGEX = /// ^ (\s*) ([*+-\.]) (?:\s+ (.*))? $ /// +LIST_OL_TASK_REGEX = /// ^ (\s*) (\d+)([\.\)]) \s+ \[[xX\ ]\] (?:\s+ (.*))? $ /// +LIST_OL_REGEX = /// ^ (\s*) (\d+)([\.\)]) (?:\s+ (.*))? $ /// +LIST_AL_TASK_REGEX = /// ^ (\s*) ([a-zA-Z]+)([\.\)]) \s+ \[[xX\ ]\] (?:\s+ (.*))? $ /// +LIST_AL_REGEX = /// ^ (\s*) ([a-zA-Z]+)([\.\)]) (?:\s+ (.*))? $ /// +BLOCKQUOTE_REGEX = /// ^ (\s*) (>) (?:\s+ (.*))? $ /// incStr = (str) -> num = parseInt(str, 10) @@ -87,7 +87,7 @@ class LineMeta @head = matches[2] @defaultHead = type.defaultHead(matches[2]) @suffix = if matches.length >= 4 then matches[3] else "" - @body = matches[matches.length-1] + @body = matches[matches.length-1] || "" @nextLine = (type.nextLine || type.lineHead).call(null, @indent, @head, @suffix) break diff --git a/spec/helpers/line-meta-spec.coffee b/spec/helpers/line-meta-spec.coffee index 5e65846f..a5930961 100644 --- a/spec/helpers/line-meta-spec.coffee +++ b/spec/helpers/line-meta-spec.coffee @@ -22,71 +22,132 @@ describe "LineMeta", -> it "is not continuous", -> expect(new LineMeta("normal line").isContinuous()).toBe(false) - describe "unordered task list line", -> - lineMeta = new LineMeta("- [X] line") - - it "is list", -> expect(lineMeta.isList()).toBe(true) - it "is ul list", -> expect(lineMeta.isList("ul")).toBe(true) - it "is not ol list", -> expect(lineMeta.isList("ol")).toBe(false) - it "is task list", -> expect(lineMeta.isTaskList()).toBe(true) - it "is continuous", -> expect(lineMeta.isContinuous()).toBe(true) - it "is not empty body", -> expect(lineMeta.isEmptyBody()).toBe(false) - it "is not indented", -> expect(lineMeta.isIndented()).toBe(false) - it "has body", -> expect(lineMeta.body).toBe("line") - it "has head", -> expect(lineMeta.head).toBe("-") - it "had default head", -> expect(lineMeta.defaultHead).toBe("-") - it "has indent", -> expect(lineMeta.indent).toBe("") - it "has nextLine", -> expect(lineMeta.nextLine).toBe("- [ ] ") - it "create lineHead", -> expect(lineMeta.lineHead("*")).toBe("* [ ] ") + describe "unordered task list lines", -> + for line in ["- [ ]", "- [x]", "- [ ] ", "- [X] "] + describe line, -> + lineMeta = new LineMeta(line) + it "is list", -> expect(lineMeta.isList()).toBe(true) + it "is ul list", -> expect(lineMeta.isList("ul")).toBe(true) + it "is not ol list", -> expect(lineMeta.isList("ol")).toBe(false) + it "is task list", -> expect(lineMeta.isTaskList()).toBe(true) + it "is continuous", -> expect(lineMeta.isContinuous()).toBe(true) + it "is empty body", -> expect(lineMeta.isEmptyBody()).toBe(true) + it "is not indented", -> expect(lineMeta.isIndented()).toBe(false) + it "has body", -> expect(lineMeta.body).toBe("") + it "has head", -> expect(lineMeta.head).toBe("-") + it "had default head", -> expect(lineMeta.defaultHead).toBe("-") + it "has indent", -> expect(lineMeta.indent).toBe("") + it "has nextLine", -> expect(lineMeta.nextLine).toBe("- [ ] ") + it "create lineHead", -> expect(lineMeta.lineHead("*")).toBe("* [ ] ") + + describe "- [X] line", -> + lineMeta = new LineMeta("- [X] line") + it "is list", -> expect(lineMeta.isList()).toBe(true) + it "is ul list", -> expect(lineMeta.isList("ul")).toBe(true) + it "is not ol list", -> expect(lineMeta.isList("ol")).toBe(false) + it "is task list", -> expect(lineMeta.isTaskList()).toBe(true) + it "is continuous", -> expect(lineMeta.isContinuous()).toBe(true) + it "is not empty body", -> expect(lineMeta.isEmptyBody()).toBe(false) + it "is not indented", -> expect(lineMeta.isIndented()).toBe(false) + it "has body", -> expect(lineMeta.body).toBe("line") + it "has head", -> expect(lineMeta.head).toBe("-") + it "had default head", -> expect(lineMeta.defaultHead).toBe("-") + it "has indent", -> expect(lineMeta.indent).toBe("") + it "has nextLine", -> expect(lineMeta.nextLine).toBe("- [ ] ") + it "create lineHead", -> expect(lineMeta.lineHead("*")).toBe("* [ ] ") describe "unordered list line", -> - lineMeta = new LineMeta(" - line") - - it "is list", -> expect(lineMeta.isList()).toBe(true) - it "is continuous", -> expect(lineMeta.isContinuous()).toBe(true) - it "is not empty body", -> expect(lineMeta.isEmptyBody()).toBe(false) - it "is indented", -> expect(lineMeta.isIndented()).toBe(true) - it "has body", -> expect(lineMeta.body).toBe("line") - it "has head", -> expect(lineMeta.head).toBe("-") - it "had default head", -> expect(lineMeta.defaultHead).toBe("-") - it "has indent", -> expect(lineMeta.indent).toBe(" ") - it "has nextLine", -> expect(lineMeta.nextLine).toBe(" - ") - it "create lineHead", -> expect(lineMeta.lineHead("*")).toBe(" * ") + for line in ["-", "- ", "- "] + describe line, -> + lineMeta = new LineMeta(line) + it "is list", -> expect(lineMeta.isList()).toBe(true) + it "is continuous", -> expect(lineMeta.isContinuous()).toBe(true) + it "is empty body", -> expect(lineMeta.isEmptyBody()).toBe(true) + it "is not indented", -> expect(lineMeta.isIndented()).toBe(false) + it "has body", -> expect(lineMeta.body).toBe("") + it "has head", -> expect(lineMeta.head).toBe("-") + it "had default head", -> expect(lineMeta.defaultHead).toBe("-") + it "has indent", -> expect(lineMeta.indent).toBe("") + it "has nextLine", -> expect(lineMeta.nextLine).toBe("- ") + it "create lineHead", -> expect(lineMeta.lineHead("*")).toBe("* ") + + describe " - line", -> + lineMeta = new LineMeta(" - line") + it "is list", -> expect(lineMeta.isList()).toBe(true) + it "is continuous", -> expect(lineMeta.isContinuous()).toBe(true) + it "is not empty body", -> expect(lineMeta.isEmptyBody()).toBe(false) + it "is indented", -> expect(lineMeta.isIndented()).toBe(true) + it "has body", -> expect(lineMeta.body).toBe("line") + it "has head", -> expect(lineMeta.head).toBe("-") + it "had default head", -> expect(lineMeta.defaultHead).toBe("-") + it "has indent", -> expect(lineMeta.indent).toBe(" ") + it "has nextLine", -> expect(lineMeta.nextLine).toBe(" - ") + it "create lineHead", -> expect(lineMeta.lineHead("*")).toBe(" * ") describe "ordered task list line", -> - lineMeta = new LineMeta(" 99. [X] line") - - it "is list", -> expect(lineMeta.isList()).toBe(true) - it "is ol list", -> expect(lineMeta.isList("ol")).toBe(true) - it "is not ul list", -> expect(lineMeta.isList("ul")).toBe(false) - it "is task list", -> expect(lineMeta.isTaskList()).toBe(true) - it "is continuous", -> expect(lineMeta.isContinuous()).toBe(true) - it "is not empty body", -> expect(lineMeta.isEmptyBody()).toBe(false) - it "is indented", -> expect(lineMeta.isIndented()).toBe(true) - it "has body", -> expect(lineMeta.body).toBe("line") - it "has head", -> expect(lineMeta.head).toBe("99") - it "had default head", -> expect(lineMeta.defaultHead).toBe("1") - it "has indent", -> expect(lineMeta.indent).toBe(" ") - it "has nextLine", -> expect(lineMeta.nextLine).toBe(" 100. [ ] ") - it "create lineHead", -> expect(lineMeta.lineHead("1")).toBe(" 1. [ ] ") + for line in ["1. [ ]", "1. [x]", "1. [ ] ", "1. [X] "] + describe line, -> + lineMeta = new LineMeta(line) + it "is list", -> expect(lineMeta.isList()).toBe(true) + it "is ol list", -> expect(lineMeta.isList("ol")).toBe(true) + it "is not ul list", -> expect(lineMeta.isList("ul")).toBe(false) + it "is task list", -> expect(lineMeta.isTaskList()).toBe(true) + it "is continuous", -> expect(lineMeta.isContinuous()).toBe(true) + it "is empty body", -> expect(lineMeta.isEmptyBody()).toBe(true) + it "is not indented", -> expect(lineMeta.isIndented()).toBe(false) + it "has body", -> expect(lineMeta.body).toBe("") + it "has head", -> expect(lineMeta.head).toBe("1") + it "had default head", -> expect(lineMeta.defaultHead).toBe("1") + it "has indent", -> expect(lineMeta.indent).toBe("") + it "has nextLine", -> expect(lineMeta.nextLine).toBe("2. [ ] ") + it "create lineHead", -> expect(lineMeta.lineHead("1")).toBe("1. [ ] ") + + describe " 99. [X] line", -> + lineMeta = new LineMeta(" 99. [X] line") + it "is list", -> expect(lineMeta.isList()).toBe(true) + it "is ol list", -> expect(lineMeta.isList("ol")).toBe(true) + it "is not ul list", -> expect(lineMeta.isList("ul")).toBe(false) + it "is task list", -> expect(lineMeta.isTaskList()).toBe(true) + it "is continuous", -> expect(lineMeta.isContinuous()).toBe(true) + it "is not empty body", -> expect(lineMeta.isEmptyBody()).toBe(false) + it "is indented", -> expect(lineMeta.isIndented()).toBe(true) + it "has body", -> expect(lineMeta.body).toBe("line") + it "has head", -> expect(lineMeta.head).toBe("99") + it "had default head", -> expect(lineMeta.defaultHead).toBe("1") + it "has indent", -> expect(lineMeta.indent).toBe(" ") + it "has nextLine", -> expect(lineMeta.nextLine).toBe(" 100. [ ] ") + it "create lineHead", -> expect(lineMeta.lineHead("1")).toBe(" 1. [ ] ") describe "ordered list line", -> - lineMeta = new LineMeta("3. line") - - it "is list", -> expect(lineMeta.isList()).toBe(true) - it "is continuous", -> expect(lineMeta.isContinuous()).toBe(true) - it "is not empty body", -> expect(lineMeta.isEmptyBody()).toBe(false) - it "is not indented", -> expect(lineMeta.isIndented()).toBe(false) - it "has body", -> expect(lineMeta.body).toBe("line") - it "has head", -> expect(lineMeta.head).toBe("3") - it "had default head", -> expect(lineMeta.defaultHead).toBe("1") - it "has indent", -> expect(lineMeta.indent).toBe("") - it "has nextLine", -> expect(lineMeta.nextLine).toBe("4. ") - it "create lineHead", -> expect(lineMeta.lineHead("1")).toBe("1. ") - - describe "ordered list line (bracket)", -> + for line in ["3.", "3. ", "3. "] + describe line, -> + lineMeta = new LineMeta(line) + it "is list", -> expect(lineMeta.isList()).toBe(true) + it "is continuous", -> expect(lineMeta.isContinuous()).toBe(true) + it "is empty body", -> expect(lineMeta.isEmptyBody()).toBe(true) + it "is not indented", -> expect(lineMeta.isIndented()).toBe(false) + it "has body", -> expect(lineMeta.body).toBe("") + it "has head", -> expect(lineMeta.head).toBe("3") + it "had default head", -> expect(lineMeta.defaultHead).toBe("1") + it "has indent", -> expect(lineMeta.indent).toBe("") + it "has nextLine", -> expect(lineMeta.nextLine).toBe("4. ") + it "create lineHead", -> expect(lineMeta.lineHead("1")).toBe("1. ") + + describe "3. line", -> + lineMeta = new LineMeta("3. line") + it "is list", -> expect(lineMeta.isList()).toBe(true) + it "is continuous", -> expect(lineMeta.isContinuous()).toBe(true) + it "is not empty body", -> expect(lineMeta.isEmptyBody()).toBe(false) + it "is not indented", -> expect(lineMeta.isIndented()).toBe(false) + it "has body", -> expect(lineMeta.body).toBe("line") + it "has head", -> expect(lineMeta.head).toBe("3") + it "had default head", -> expect(lineMeta.defaultHead).toBe("1") + it "has indent", -> expect(lineMeta.indent).toBe("") + it "has nextLine", -> expect(lineMeta.nextLine).toBe("4. ") + it "create lineHead", -> expect(lineMeta.lineHead("1")).toBe("1. ") + + describe "3) line", -> lineMeta = new LineMeta("3) line") - it "is list", -> expect(lineMeta.isList()).toBe(true) it "is continuous", -> expect(lineMeta.isContinuous()).toBe(true) it "is not empty body", -> expect(lineMeta.isEmptyBody()).toBe(false) @@ -98,51 +159,35 @@ describe "LineMeta", -> it "has nextLine", -> expect(lineMeta.nextLine).toBe("4) ") it "create lineHead", -> expect(lineMeta.lineHead("1")).toBe("1) ") - describe "ordered list line (empty)", -> - lineMeta = new LineMeta("3. ") - - it "is list", -> expect(lineMeta.isList()).toBe(true) - it "is continuous", -> expect(lineMeta.isContinuous()).toBe(true) - it "is not empty body", -> expect(lineMeta.isEmptyBody()).toBe(true) - it "is not indented", -> expect(lineMeta.isIndented()).toBe(false) - it "has body", -> expect(lineMeta.body).toBe("") - it "has head", -> expect(lineMeta.head).toBe("3") - it "had default head", -> expect(lineMeta.defaultHead).toBe("1") - it "has indent", -> expect(lineMeta.indent).toBe("") - it "has nextLine", -> expect(lineMeta.nextLine).toBe("4. ") - it "create lineHead", -> expect(lineMeta.lineHead("1")).toBe("1. ") - describe "ordered alpha list line", -> - lineMeta = new LineMeta("a. line") - - it "is list", -> expect(lineMeta.isList()).toBe(true) - it "is continuous", -> expect(lineMeta.isContinuous()).toBe(true) - it "is not empty body", -> expect(lineMeta.isEmptyBody()).toBe(false) - it "is not indented", -> expect(lineMeta.isIndented()).toBe(false) - it "has body", -> expect(lineMeta.body).toBe("line") - it "has head", -> expect(lineMeta.head).toBe("a") - it "had default head", -> expect(lineMeta.defaultHead).toBe("a") - it "has indent", -> expect(lineMeta.indent).toBe("") - it "has nextLine", -> expect(lineMeta.nextLine).toBe("b. ") - it "create lineHead", -> expect(lineMeta.lineHead("a")).toBe("a. ") - - describe "ordered alpha list line (bracket)", -> - lineMeta = new LineMeta("E) line") - - it "is list", -> expect(lineMeta.isList()).toBe(true) - it "is continuous", -> expect(lineMeta.isContinuous()).toBe(true) - it "is not empty body", -> expect(lineMeta.isEmptyBody()).toBe(false) - it "is not indented", -> expect(lineMeta.isIndented()).toBe(false) - it "has body", -> expect(lineMeta.body).toBe("line") - it "has head", -> expect(lineMeta.head).toBe("E") - it "had default head", -> expect(lineMeta.defaultHead).toBe("A") - it "has indent", -> expect(lineMeta.indent).toBe("") - it "has nextLine", -> expect(lineMeta.nextLine).toBe("F) ") - it "create lineHead", -> expect(lineMeta.lineHead("A")).toBe("A) ") + describe "a. line", -> + lineMeta = new LineMeta("a. line") + it "is list", -> expect(lineMeta.isList()).toBe(true) + it "is continuous", -> expect(lineMeta.isContinuous()).toBe(true) + it "is not empty body", -> expect(lineMeta.isEmptyBody()).toBe(false) + it "is not indented", -> expect(lineMeta.isIndented()).toBe(false) + it "has body", -> expect(lineMeta.body).toBe("line") + it "has head", -> expect(lineMeta.head).toBe("a") + it "had default head", -> expect(lineMeta.defaultHead).toBe("a") + it "has indent", -> expect(lineMeta.indent).toBe("") + it "has nextLine", -> expect(lineMeta.nextLine).toBe("b. ") + it "create lineHead", -> expect(lineMeta.lineHead("a")).toBe("a. ") + + describe "E) line", -> + lineMeta = new LineMeta("E) line") + it "is list", -> expect(lineMeta.isList()).toBe(true) + it "is continuous", -> expect(lineMeta.isContinuous()).toBe(true) + it "is not empty body", -> expect(lineMeta.isEmptyBody()).toBe(false) + it "is not indented", -> expect(lineMeta.isIndented()).toBe(false) + it "has body", -> expect(lineMeta.body).toBe("line") + it "has head", -> expect(lineMeta.head).toBe("E") + it "had default head", -> expect(lineMeta.defaultHead).toBe("A") + it "has indent", -> expect(lineMeta.indent).toBe("") + it "has nextLine", -> expect(lineMeta.nextLine).toBe("F) ") + it "create lineHead", -> expect(lineMeta.lineHead("A")).toBe("A) ") describe "blockquote", -> lineMeta = new LineMeta(" > blockquote") - it "is list", -> expect(lineMeta.isList()).toBe(false) it "is continuous", -> expect(lineMeta.isContinuous()).toBe(true) it "is not empty body", -> expect(lineMeta.isEmptyBody()).toBe(false)