From 06bdf0f3abeab98d7713d389ae3a2c933317d701 Mon Sep 17 00:00:00 2001 From: Asko Nomm Date: Wed, 6 Apr 2022 20:05:50 +0200 Subject: Test code block --- resources/test/parsers/code_block.md | 41 +++++++++++++++++++++++++++ resources/test/parsers/code_block_result.html | 38 +++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 resources/test/parsers/code_block.md create mode 100644 resources/test/parsers/code_block_result.html (limited to 'resources/test/parsers') 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 `
`; +} + +// 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 @@ +
// 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 -- cgit v1.2.3