From 9f8f4fc36adef06458320d7bfdef01dbe41f52a1 Mon Sep 17 00:00:00 2001 From: Asko Nomm Date: Sat, 9 Apr 2022 13:51:16 +0200 Subject: Support settext headings (closes #6) --- resources/test/core.md | 8 +++- resources/test/core_result.html | 20 ++++++++- src/clarktown/parser.clj | 2 +- src/clarktown/parsers/heading_block.clj | 59 +++++++++++++++++++++++---- test/clarktown/parsers/heading_block_test.clj | 44 ++++++++++++++++++++ 5 files changed, 122 insertions(+), 11 deletions(-) create mode 100644 test/clarktown/parsers/heading_block_test.clj diff --git a/resources/test/core.md b/resources/test/core.md index fe284ad..d699887 100644 --- a/resources/test/core.md +++ b/resources/test/core.md @@ -68,4 +68,10 @@ This is ___bold italic text___ and ***this is also***. *What about italic text t * Another sub list item 1. Sub sub list item 2. Continuing sub list item -* Continuing list item \ No newline at end of file +* Continuing list item + +This is a H1 heading with settext +================================= + +And this is a H2 heading with settext +------------------------------------- \ No newline at end of file diff --git a/resources/test/core_result.html b/resources/test/core_result.html index fb64b35..5e9513b 100644 --- a/resources/test/core_result.html +++ b/resources/test/core_result.html @@ -1,4 +1,8 @@ -

Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.

  1. List item
  2. Another list item
    1. Sub list item
    2. Another sub list item
      1. Sub sub list item
    3. Continuing sub list item
  3. Continuing list item
// Detect horizontal line block
+

Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.

+ +
  1. List item
  2. Another list item
    1. Sub list item
    2. Another sub list item
      1. Sub sub list item
    3. Continuing sub list item
  3. Continuing list item
+ +
// Detect horizontal line block
 function isHorizontalLineBlock(block) {
   return block === "***";
 }
@@ -36,4 +40,16 @@ function markdownToHTML(markdown) {
 
   // And at last, join the blocks together for one big block.
   return parsedBlocks.join("");
-}

This is bold italic text and this is also. What about italic text that has bold text?

Hi there, world!

  • List item
  • Another list item
    • Sub list item
    • Another sub list item
      • Sub sub list item
      • Continuing sub list item
  • Continuing list item
  • List item
  • Another list item
    • Sub list item
    • Another sub list item
      1. Sub sub list item
      2. Continuing sub list item
  • Continuing list item
\ No newline at end of file +}
+ +

This is bold italic text and this is also. What about italic text that has bold text?

+ +

Hi there, world!

+ + + + + +

This is a H1 heading with settext

+ +

And this is a H2 heading with settext

\ No newline at end of file diff --git a/src/clarktown/parser.clj b/src/clarktown/parser.clj index 95776d7..0b6911d 100644 --- a/src/clarktown/parser.clj +++ b/src/clarktown/parser.clj @@ -94,4 +94,4 @@ (let [blocks (-> (string/split markdown #"\n\n") stitch-code-blocks) parsed-blocks (parse-blocks blocks parsers)] - (string/join "" parsed-blocks))) + (string/join "\n\n" parsed-blocks))) diff --git a/src/clarktown/parsers/heading_block.clj b/src/clarktown/parsers/heading_block.clj index 84a5fdb..def2394 100644 --- a/src/clarktown/parsers/heading_block.clj +++ b/src/clarktown/parsers/heading_block.clj @@ -3,17 +3,35 @@ [clojure.string :as string])) +(defn is-hashbang-heading? + "Determines whether the given block is a hashbang heading." + [block] + (-> (string/replace block #"\n" "") + string/trim + (string/starts-with? "#"))) + + +(defn is-settext-heading? + "Determines whether the given block is a settext heading." + [block] + (let [lines (-> (string/split-lines block)) + chars (-> (last lines) + string/trim + (string/split #""))] + (and (> (count lines) 1) + (every? #{"-" "="} chars)))) + + (defn is? - "Determines whether the given block is a heading block or not." + "Determines whether the given block is a heading block." [block] - (= true (-> (string/replace block #"\n" "") - string/trim - (string/starts-with? "#")))) + (or (is-hashbang-heading? block) + (is-settext-heading? block))) -(defn render - "Renders the heading block." - [block _] +(defn render-hashbang-heading + "Renders the hashbang heading block." + [block] (let [single-line-block (-> (string/replace block #"\n" "") string/trim) size (-> (string/split single-line-block #" ") @@ -25,3 +43,30 @@ (string/join " ") string/trim)] (str "" value ""))) + + +(defn render-settext-heading + "Renders the settext heading block." + [block] + (let [lines (string/split-lines block) + value (->> (split-at (- (count lines) 1) lines) + first + (string/join "\n")) + h1? (= "=" (-> (last lines) + string/trim + (string/split #"") + first))] + (if h1? + (str "

" value "

") + (str "

" value "

")))) + + +(render-settext-heading "Hello world\nAnd you too\n===") + + +(defn render + "Renders the heading block." + [block _] + (if (is-hashbang-heading? block) + (render-hashbang-heading block) + (render-settext-heading block))) diff --git a/test/clarktown/parsers/heading_block_test.clj b/test/clarktown/parsers/heading_block_test.clj new file mode 100644 index 0000000..9bfff4f --- /dev/null +++ b/test/clarktown/parsers/heading_block_test.clj @@ -0,0 +1,44 @@ +(ns clarktown.parsers.heading-block-test + (:require + [clojure.test :refer [deftest testing is]] + [clarktown.parsers.heading-block :as heading-block])) + + +(deftest hashbang-heading-test + (testing "Hashbang heading block that's a H1" + (is (= "

This is a heading block.

" + (heading-block/render "# This is a heading block." nil)))) + + (testing "Hashbang heading block that's a H2" + (is (= "

This is a heading block.

" + (heading-block/render "## This is a heading block." nil)))) + + (testing "Hashbang heading block that's a H3" + (is (= "

This is a heading block.

" + (heading-block/render "### This is a heading block." nil)))) + + (testing "Hashbang heading block that's a H4" + (is (= "

This is a heading block.

" + (heading-block/render "#### This is a heading block." nil)))) + + (testing "Hashbang heading block that's a H5" + (is (= "
This is a heading block.
" + (heading-block/render "##### This is a heading block." nil))))) + + +(deftest settext-heading-text + (testing "Settext heading block that's a H1" + (is (= "

This is a heading block.

" + (heading-block/render "This is a heading block.\n=========" nil)))) + + (testing "Settext heading block that's a H1 spanning multiple lines" + (is (= "

This is a \nheading block spanning multiple lines.

" + (heading-block/render "This is a \nheading block spanning multiple lines.\n========" nil)))) + + (testing "Settext heading block that's a H2" + (is (= "

This is a heading block.

" + (heading-block/render "This is a heading block.\n---------" nil)))) + + (testing "Settext heading block that's a H2 spanning multiple lines" + (is (= "

This is a \nheading block spanning multiple lines.

" + (heading-block/render "This is a \nheading block spanning multiple lines.\n--------" nil))))) \ No newline at end of file -- cgit v1.2.3