Skip to content

Commit

Permalink
adds onCodeBlock callback re #11
Browse files Browse the repository at this point in the history
  • Loading branch information
rsms committed Jul 1, 2021
1 parent da6fc41 commit 2938af6
Show file tree
Hide file tree
Showing 11 changed files with 262 additions and 93 deletions.
14 changes: 7 additions & 7 deletions example/example.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ <h2><a id="another" class="anchor" aria-hidden="true" href="#another"></a>Anothe
<p>line 1
line 2</p>
<h2><a id="code-poetry" class="anchor" aria-hidden="true" href="#code-poetry"></a>Code &amp; Poetry</h2>
<pre><code>You can also indent
blocks to display
code or poetry.
<pre><code>YOU CAN ALSO INDENT
BLOCKS TO DISPLAY
CODE OR POETRY.

Indented code/poetry blocks
can be hard-wrapped.
INDENTED CODE/POETRY BLOCKS
CAN BE HARD-WRAPPED.
</code></pre>
<p><b>Or, wrap your code in three backticks:</b></p>
<pre><code class="language-js">function codeBlocks() {
return &quot;Can be inserted&quot;
<pre><code class="language-js">FUNCTION CODEBLOCKS() {
RETURN &quot;CAN BE INSERTED&quot;
}
</code></pre>
<h3><a id="block-quotes" class="anchor" aria-hidden="true" href="#block-quotes"></a>Block Quotes</h3>
Expand Down
15 changes: 14 additions & 1 deletion example/example.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
const fs = require("fs")
const md = require("../dist/markdown.node.js")
// const md = require("../build/debug/markdown.node.js")

const source = fs.readFileSync(__dirname + "/example.md")
const outbuf = md.parse(source, { bytes: true })
const outbuf = md.parse(source, {
bytes: true,
onCodeBlock(lang, body) {
console.log(`onCodeBlock (${lang})`)
return html_escape(body.toString().toUpperCase())
},
})
const outfile = __dirname + "/example.html"
console.log("write", outfile)
fs.writeFileSync(outfile, outbuf)
Expand All @@ -24,3 +31,9 @@ if (process.argv.includes("-bench")) {
const timeSpent = Date.now() - timeStart
console.log(`benchmark end -- avg parse time: ${((timeSpent / ntotal) * 1000).toFixed(1)}us`)
}

function html_escape(str) {
return str.replace(/[&<>'"]/g, tag => ({
'&': '&amp;','<': '&lt;','>': '&gt;',"'": '&#39;','"': '&quot;'
}[tag]))
}
21 changes: 21 additions & 0 deletions markdown.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,31 @@ export interface ParseOptions {
*/
bytes? :boolean

/**
* onCodeBlock is an optional callback which if provided is called for each code block.
* langname holds the "language tag", if any, of the block.
*
* The returned value is inserted into the resulting HTML verbatim, without HTML escaping.
* Thus, you should take care of properly escaping any special HTML characters.
*
* If the function returns null or undefined, or an exception occurs, the body will be
* included as-is after going through HTML escaping.
*
* Note that use of this callback has an adverse impact on performance as it casues
* calls and data to be bridged between WASM and JS on every invocation.
*/
onCodeBlock? :(langname :string, body :UTF8Bytes) => Uint8Array|string|null|undefined

/** @depreceated use "bytes" instead (v1.1.1) */
asMemoryView? :boolean
}

/** UTF8Bytes is a Uint8Array representing UTF8 text */
export interface UTF8Bytes extends Uint8Array {
/** toString returns a UTF8 decoded string (lazily decoded and cached) */
toString() :string
}

/** Flags that customize Markdown parsing */
export enum ParseFlags {
/** In TEXT, collapse non-trivial whitespace into single ' ' */ COLLAPSE_WHITESPACE,
Expand Down
9 changes: 8 additions & 1 deletion src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ typedef int32_t i32;
#endif
#if DEBUG
#include <stdio.h>
#define dlog(...) printf(__VA_ARGS__)
#define dlog(fmt, ...) printf(fmt "\n", ##__VA_ARGS__)
#else
#define dlog(...)
#endif /* DEBUG > 0 */

#include "wbuf.h"

typedef int(*JSTextFilterFun)(
const char* metaptr, u32 metalen,
const char* inptr, u32 inlen,
const char** outptrp); // return outlen
Loading

0 comments on commit 2938af6

Please sign in to comment.