summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--project.clj4
-rw-r--r--resources/test/parsers/code_block.md41
-rw-r--r--resources/test/parsers/code_block_result.html38
-rw-r--r--src/clarktown/parsers/code_block.clj4
-rw-r--r--test/clarktown/core_test.clj9
-rw-r--r--test/clarktown/parsers/code_block_test.clj9
6 files changed, 95 insertions, 10 deletions
diff --git a/project.clj b/project.clj
index 697f8e0..9724f57 100644
--- a/project.clj
+++ b/project.clj
@@ -1,8 +1,10 @@
-(defproject com.github.askonomm/clarktown "1.0"
+(defproject com.github.askonomm/clarktown "1.0.2"
:description "A zero-dependency, pure-clojure Markdown parser."
:url "https://github.com/askonomm/clarktown"
:license {:name "MIT"
:url "https://github.com/askonomm/clarktown/blob/master/LICENSE.txt"}
+ :deploy-repositories [["releases" {:sign-releases false :url "https://repo.clojars.org"}]
+ ["snapshots" {:sign-releases false :url "https://repo.clojars.org"}]]
:dependencies [[org.clojure/clojure "1.11.0"]]
:plugins [[lein-auto "0.1.3"]]
:repl-options {:init-ns clarktown.core})
diff --git a/resources/test/parsers/code_block.md b/resources/test/parsers/code_block.md
new file mode 100644
index 0000000..8e6f863
--- /dev/null
+++ b/resources/test/parsers/code_block.md
@@ -0,0 +1,41 @@
+```javascript
+// Detect horizontal line block
+function isHorizontalLineBlock(block) {
+ return block === "***";
+}
+
+// Render horizontal line block
+function horizontalLineBlock(block) {
+ return `<hr>`;
+}
+
+// Compose an array of parsers
+const parsers = [{
+ matcher: isHorizontalLineBlock,
+ renderers: [horizontalLineBlock]
+}];
+
+// And finally, our parser itself
+function markdownToHTML(markdown) {
+ // Create blocks
+ const blocks = content.split(/\n\n/);
+
+ // Parse blocks
+ const parsedBlocks = blocks.map((block) => {
+ // Let's find a parser that has a matcher that matches
+ const parser = parsers.find((parser) => parser.matcher(block));
+
+ // If match was found, let's run our renderers over `block`
+ if (parser) {
+ for (const renderer of match.renderers) {
+ block = renderer(block);
+ }
+ }
+
+ return block;
+ });
+
+ // And at last, join the blocks together for one big block.
+ return parsedBlocks.join("");
+}
+``` \ No newline at end of file
diff --git a/resources/test/parsers/code_block_result.html b/resources/test/parsers/code_block_result.html
new file mode 100644
index 0000000..e95e481
--- /dev/null
+++ b/resources/test/parsers/code_block_result.html
@@ -0,0 +1,38 @@
+<pre><code class="language-javascript">// Detect horizontal line block
+function isHorizontalLineBlock(block) {
+ return block === "***";
+}
+
+// Render horizontal line block
+function horizontalLineBlock(block) {
+ return `&lt;hr&gt;`;
+}
+
+// Compose an array of parsers
+const parsers = [{
+ matcher: isHorizontalLineBlock,
+ renderers: [horizontalLineBlock]
+}];
+
+// And finally, our parser itself
+function markdownToHTML(markdown) {
+ // Create blocks
+ const blocks = content.split(/\n\n/);
+
+ // Parse blocks
+ const parsedBlocks = blocks.map((block) =&gt; {
+ // Let's find a parser that has a matcher that matches
+ const parser = parsers.find((parser) =&gt; parser.matcher(block));
+
+ // If match was found, let's run our renderers over `block`
+ if (parser) {
+ for (const renderer of match.renderers) {
+ block = renderer(block);
+ }
+ }
+
+ return block;
+ });
+
+ // And at last, join the blocks together for one big block.
+ return parsedBlocks.join("");</code></pre> \ No newline at end of file
diff --git a/src/clarktown/parsers/code_block.clj b/src/clarktown/parsers/code_block.clj
index b1699d7..a5de41c 100644
--- a/src/clarktown/parsers/code_block.clj
+++ b/src/clarktown/parsers/code_block.clj
@@ -22,9 +22,7 @@
(string/replace n #"&" "&amp;")
(string/replace n #"<" "&lt;")
(string/replace n #">" "&gt;")
- (string/replace n #"\n" "<br>")
- (string/replace n #"\tab" "<tab>")
(string/trim n))]
(if language
- (str "<pre class=\"language-" language "\"><code>" code "</code></pre>")
+ (str "<pre><code class=\"language-" language "\">" code "</code></pre>")
(str "<pre><code>" code "</code></pre>"))))
diff --git a/test/clarktown/core_test.clj b/test/clarktown/core_test.clj
index 8425a76..da252c7 100644
--- a/test/clarktown/core_test.clj
+++ b/test/clarktown/core_test.clj
@@ -1,7 +1,4 @@
(ns clarktown.core-test
- (:require [clojure.test :refer :all]
- [clarktown.core :refer :all]))
-
-(deftest a-test
- (testing "FIXME, I fail."
- (is (= 0 1))))
+ (:require
+ [clojure.test :refer :all]
+ [clarktown.core :refer :all])) \ No newline at end of file
diff --git a/test/clarktown/parsers/code_block_test.clj b/test/clarktown/parsers/code_block_test.clj
new file mode 100644
index 0000000..1bd51b2
--- /dev/null
+++ b/test/clarktown/parsers/code_block_test.clj
@@ -0,0 +1,9 @@
+(ns clarktown.parsers.code-block-test
+ (:require [clojure.test :refer [deftest testing is]]
+ [clarktown.parsers.code-block :as code-block]))
+
+
+(deftest code-block-test
+ (testing "Code block"
+ (is (= (slurp "./resources/test/parsers/code_block_result.html")
+ (code-block/render (slurp "./resources/test/parsers/code_block.md") nil))))) \ No newline at end of file