summaryrefslogtreecommitdiff
path: root/resources/test/parsers
diff options
context:
space:
mode:
authorAsko Nomm <asko@bien.ee>2022-04-09 14:01:31 +0200
committerAsko Nomm <asko@bien.ee>2022-04-09 14:01:31 +0200
commitaac78c214ea953e4d1fcd84943c867395bfc22ec (patch)
treec97f855509700665e83af2ce914ffdddc7159ac2 /resources/test/parsers
parent9f8f4fc36adef06458320d7bfdef01dbe41f52a1 (diff)
Add full test coverage for code blocks (re #4)
Diffstat (limited to 'resources/test/parsers')
-rw-r--r--resources/test/parsers/code_block_no_language.md41
-rw-r--r--resources/test/parsers/code_block_no_language_result.html39
2 files changed, 80 insertions, 0 deletions
diff --git a/resources/test/parsers/code_block_no_language.md b/resources/test/parsers/code_block_no_language.md
new file mode 100644
index 0000000..dbba1f1
--- /dev/null
+++ b/resources/test/parsers/code_block_no_language.md
@@ -0,0 +1,41 @@
+```
+// 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_no_language_result.html b/resources/test/parsers/code_block_no_language_result.html
new file mode 100644
index 0000000..0512d3e
--- /dev/null
+++ b/resources/test/parsers/code_block_no_language_result.html
@@ -0,0 +1,39 @@
+<pre><code>// 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