babashka/test-resources/lib_tests/clarktown/parsers/code_block_result.html
Bob 1113037b51
add Clarktown lib tests (#1247)
* add lib tests for clarktown

* add clarktown to library list

* make code block test agnostic to line separator
2022-04-17 20:11:33 +02:00

39 lines
No EOL
1,000 B
HTML

<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>