markdown #
markdown
Markdown module for V. For now, it uses the md4c library for parsing markdown files to HTML and soon it will be rewritten from scratch to pure V.
Usage
module main
import markdown
fn main() {
text := '# Markdown Rocks!'
output := markdown.to_html(text)
println(output) // Markdown Rocks!
}
Installation
Install and use markdown module via VPM:
v install markdown
Or via vpkg:
vpkg get https://github.com/vlang/markdown --global
Or through Git:
git clone https://github.com/vlang/markdown.git ~/.vmodules/markdown
Contributors
- Ned Palacios (@nedpals)
Constants #
const default_html_transformer = &DefaultHtmlTransformer{}
fn new #
fn new(parser_flags u32, enter_block_cb BlockFn, leave_block_cb BlockFn, enter_span_cb SpanFn, leave_span_cb SpanFn, text_cb TextFn, debug_cb DebugFn) C.MD_PARSER
fn render #
fn render(src string, mut renderer Renderer) !string
render parses and renders a given markdown string based on the renderer.
fn to_html #
fn to_html(input string) string
fn to_html_experimental #
fn to_html_experimental(input string) string
fn to_plain #
fn to_plain(input string) string
interface HtmlTransformer #
interface HtmlTransformer {
transform_attribute(parent ParentType, name string, value string) string
transform_content(parent ParentType, text string) string
mut:
config_set(key string, value string)
}
HtmlTransformer transforms converted HTML elements (limited to attribute and content for now) from markdown before incorporated into the final output.
When implementing an HtmlTransformer, escaping the content is up to you. However, it is best to wrap the values you don't need with markdown.default_html_transformer.transform_{attribute|content}
interface Renderer #
interface Renderer {
mut:
str() string
enter_block(typ MD_BLOCKTYPE, detail voidptr) ?
leave_block(typ MD_BLOCKTYPE, detail voidptr) ?
enter_span(typ MD_SPANTYPE, detail voidptr) ?
leave_span(typ MD_SPANTYPE, detail voidptr) ?
text(typ MD_TEXTTYPE, content string) ?
debug_log(msg string)
}
Renderer represents an entity that accepts incoming data and renders the content.
type AttrTransformerFn #
type AttrTransformerFn = fn (ParentType, string, string) string
type ContentTransformerFn #
type ContentTransformerFn = fn (ParentType, string) string
fn (ContentTransformerFn) transform_attribute #
fn (t ContentTransformerFn) transform_attribute(parent ParentType, name string, value string) string
fn (ContentTransformerFn) transform_content #
fn (t ContentTransformerFn) transform_content(parent ParentType, text string) string
fn (ContentTransformerFn) config_set #
fn (mut t ContentTransformerFn) config_set(key string, val string)
type ParentType #
type ParentType = MD_BLOCKTYPE | MD_SPANTYPE
enum MD_ALIGN #
@[typedef]
enum MD_ALIGN {
md_align_default = 0
md_align_left
md_align_center
md_align_right
}
enum MD_BLOCKTYPE #
@[typedef]
enum MD_BLOCKTYPE {
md_block_doc = 0
md_block_quote
md_block_ul
md_block_ol
md_block_li
md_block_hr
md_block_h
md_block_code
md_block_html
md_block_p
md_block_table
md_block_thead
md_block_tbody
md_block_tr
md_block_th
md_block_td
}
enum MD_SPANTYPE #
@[typedef]
enum MD_SPANTYPE {
md_span_em
md_span_strong
md_span_a
md_span_img
md_span_code
md_span_del
md_span_latexmath
md_span_latexmath_display
md_span_wikilink
md_span_u
}
enum MD_TEXTTYPE #
@[typedef]
enum MD_TEXTTYPE {
md_text_normal = 0
md_text_null_char
md_text_br
md_text_softbr
md_text_entity
md_text_code
md_text_html
md_text_latexmath
}
struct AttrTransformer #
struct AttrTransformer {
pub mut:
callback AttrTransformerFn = fn (_ ParentType, _ string, _ string) string {
return ''
}
}
fn (AttrTransformer) transform_attribute #
fn (t AttrTransformer) transform_attribute(parent ParentType, name string, value string) string
fn (AttrTransformer) transform_content #
fn (t AttrTransformer) transform_content(parent ParentType, text string) string
fn (AttrTransformer) config_set #
fn (mut t AttrTransformer) config_set(key string, val string)
struct C.MD_ATTRIBUTE #
@[typedef]
struct C.MD_ATTRIBUTE {
pub:
text &char
size u32
substr_types &MD_TEXTTYPE
substr_offsets &u32
}
struct C.MD_BLOCK_CODE_DETAIL #
@[typedef]
struct C.MD_BLOCK_CODE_DETAIL {
pub:
info C.MD_ATTRIBUTE
lang C.MD_ATTRIBUTE
fence_char u8
}
struct C.MD_BLOCK_H_DETAIL #
@[typedef]
struct C.MD_BLOCK_H_DETAIL {
pub:
level u32
}
struct C.MD_BLOCK_LI_DETAIL #
@[typedef]
struct C.MD_BLOCK_LI_DETAIL {
pub:
is_task bool
task_mark u8
task_mark_offset u32
}
struct C.MD_BLOCK_OL_DETAIL #
@[typedef]
struct C.MD_BLOCK_OL_DETAIL {
pub:
start u32
is_tight int
mark_delimiter u8
}
struct C.MD_BLOCK_TD_DETAIL #
@[typedef]
struct C.MD_BLOCK_TD_DETAIL {
pub:
align MD_ALIGN
}
struct C.MD_BLOCK_UL_DETAIL #
@[typedef]
struct C.MD_BLOCK_UL_DETAIL {
pub:
is_tight int
mark u8
}
struct C.MD_PARSER #
@[typedef]
struct C.MD_PARSER {
pub:
abi_version u32
flags u32
enter_block BlockFn
leave_block BlockFn
enter_span SpanFn
leave_span SpanFn
text TextFn
debug_log DebugFn
}
struct C.MD_SPAN_A_DETAIL #
@[typedef]
struct C.MD_SPAN_A_DETAIL {
pub:
href C.MD_ATTRIBUTE
title C.MD_ATTRIBUTE
}
struct C.MD_SPAN_IMG_DETAIL #
@[typedef]
struct C.MD_SPAN_IMG_DETAIL {
pub:
src C.MD_ATTRIBUTE
title C.MD_ATTRIBUTE
}
struct C.MD_SPAN_WIKILINK_DETAIL #
@[typedef]
struct C.MD_SPAN_WIKILINK_DETAIL {
pub:
target C.MD_ATTRIBUTE
}
struct DefaultHtmlTransformer #
struct DefaultHtmlTransformer {}
fn (DefaultHtmlTransformer) transform_attribute #
fn (t &DefaultHtmlTransformer) transform_attribute(parent ParentType, name string, value string) string
fn (DefaultHtmlTransformer) transform_content #
fn (t &DefaultHtmlTransformer) transform_content(parent ParentType, text string) string
fn (DefaultHtmlTransformer) config_set #
fn (mut t DefaultHtmlTransformer) config_set(key string, val string)
struct HtmlRenderer #
struct HtmlRenderer {
pub mut:
transformer HtmlTransformer = default_html_transformer
mut:
parent_stack Stack[ParentType]
content_writer strings.Builder = strings.new_builder(200)
writer strings.Builder = strings.new_builder(200)
image_nesting_level int
last_htag_open_pos int
}
struct PlaintextRenderer #
struct PlaintextRenderer {
mut:
writer strings.Builder = strings.new_builder(200)
}
fn (PlaintextRenderer) str #
fn (mut pt PlaintextRenderer) str() string
- README
- Constants
- fn new
- fn render
- fn to_html
- fn to_html_experimental
- fn to_plain
- interface HtmlTransformer
- interface Renderer
- type AttrTransformerFn
- type ContentTransformerFn
- type ParentType
- enum MD_ALIGN
- enum MD_BLOCKTYPE
- enum MD_SPANTYPE
- enum MD_TEXTTYPE
- struct AttrTransformer
- struct C.MD_ATTRIBUTE
- struct C.MD_BLOCK_CODE_DETAIL
- struct C.MD_BLOCK_H_DETAIL
- struct C.MD_BLOCK_LI_DETAIL
- struct C.MD_BLOCK_OL_DETAIL
- struct C.MD_BLOCK_TD_DETAIL
- struct C.MD_BLOCK_UL_DETAIL
- struct C.MD_PARSER
- struct C.MD_SPAN_A_DETAIL
- struct C.MD_SPAN_IMG_DETAIL
- struct C.MD_SPAN_WIKILINK_DETAIL
- struct DefaultHtmlTransformer
- struct HtmlRenderer
- struct PlaintextRenderer