diff --git a/source/documents/scripts/services/reader/markdown.js.coffee b/source/documents/scripts/services/reader/markdown.js.coffee index f3717b6..9ff29af 100644 --- a/source/documents/scripts/services/reader/markdown.js.coffee +++ b/source/documents/scripts/services/reader/markdown.js.coffee @@ -24,26 +24,31 @@ angular.module("sideBySide").factory("markdownReader", () -> # # @param lexed [Array] Lexed blocks # @return [Array] Section - readSection = (lexed) -> + readSection = (lexed, separators) -> items = [] while lexed.length > 0 - break if lexed[0]["type"] == "heading" + break if lexed[0]["type"] in separators items.push(lexed.shift()) return items # Read content from lexed blocks # # @param lexed [Array] Lexed blocks + # @param separators [Array] Section separators # @return [Array] Content - readContent = (lexed) -> + readContent = (lexed, separators) -> content = [] - while lexed.length > 0 - heading = if lexed[0]["type"] == "heading" \ - then lexed.shift().text \ - else heading = '' + # if first block is separator, add dummy foreword + if lexed[0]["type"] in separators + lexed.unshift({type: "paragraph", text: ""}) - text = readSection(lexed) + while lexed.length > 0 + heading = if lexed[0]["type"] in separators \ + then lexed.shift().text or "" \ + else "" + + text = readSection(lexed, separators) text.links = lexed.links content.push({ section: heading @@ -54,9 +59,11 @@ angular.module("sideBySide").factory("markdownReader", () -> lexed = marked.lexer(source) meta = lexed.shift() meta = readMeta(meta.text, new marked.InlineLexer(lexed.links)) + separators = if meta.Separator \ + then [meta.Separator] else ['heading', 'hr'] return { meta: meta - content: readContent(lexed) + content: readContent(lexed, separators) } ) diff --git a/source/documents/tests/services/reader/markdown.js.coffee b/source/documents/tests/services/reader/markdown.js.coffee index 9657cf2..58df561 100644 --- a/source/documents/tests/services/reader/markdown.js.coffee +++ b/source/documents/tests/services/reader/markdown.js.coffee @@ -35,6 +35,7 @@ test("markdownReader", () -> Source: 'http://en.wikisource.org/wiki/Universal_Declaration_of_Human_Rights' } content: [ + { section: "", text: "
\n" } { section: "1", text: "All human beings are born free and equal in dignity and rights...
\n" } { section: "2", text: """Everyone is entitled to all the rights and freedoms...
@@ -52,21 +53,50 @@ test("markdownReader", () -> } }, { tested: """ - Title: Test leading text + Title: Test leading text and mixed separators **This** is leading text. # I First section. + + --- + Second section. + """ + expected: { + meta: { + Title: "Test leading text and mixed separators" + } + content: [ + { section: "", text: "This is leading text.
\n" } + { section: "I", text: "First section.
\n" } + { section: "", text: "Second section.
\n" } + ] + } + }, { + tested: """ + Title: Separator test + Separator: heading + + # I + First section. + + --- + Second part. + # II Second section. """ expected: { meta: { - Title: "Test leading text" + Title: "Separator test" + Separator: "heading" } content: [ - { section: "", text: "This is leading text.
\n" } - { section: "I", text: "First section.
\n" } + { section: "", text: "\n" } + { section: "I", text: """ +First section.
+Second part.
\n""" } { section: "II", text: "Second section.
\n" } ] }