-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmarkdown.lua
More file actions
40 lines (40 loc) · 1.06 KB
/
markdown.lua
File metadata and controls
40 lines (40 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
local renderer = nil
local set_backend
set_backend = function(name, ...)
local _exp_0 = name
if "discount" == _exp_0 then
local opts_list = {
...
}
local discount = require("discount")
renderer = function(md_source)
return assert(discount(md_source, unpack(opts_list)))
end
elseif "cmark" == _exp_0 then
local cmark = require("cmark")
local opts = opts or (cmark.OPT_VALIDATE_UTF8 + cmark.OPT_NORMALIZE + cmark.OPT_SMART + cmark.OPT_UNSAFE)
renderer = function(md_source)
local document = assert(cmark.parse_string(md_source, opts))
return assert(cmark.render_html(document, opts))
end
else
return error("unknown markdown backend: " .. tostring(name))
end
end
local render
render = function(md_source)
if not (renderer) then
set_backend("discount")
end
return renderer(md_source)
end
local set_renderer
set_renderer = function(fn)
assert(type(fn) == "function", "renderer must be a function")
renderer = fn
end
return {
render = render,
set_renderer = set_renderer,
set_backend = set_backend
}