Skip to content

Commit

Permalink
feat(vdoc): add an html code highligher
Browse files Browse the repository at this point in the history
  • Loading branch information
nedpals committed Nov 1, 2023
1 parent 6609223 commit bdfd2b3
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion cmd/tools/vdoc/html.v
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,12 @@ fn doc_node_html(dn doc.DocNode, link string, head bool, include_examples bool,
} else {
html_tag_escape(comments)
}
md_content := markdown.to_html(escaped_html)
mut renderer := markdown.HtmlRenderer{
transformer: &MdHtmlCodeHighlighter{
table: tb
}
}
md_content := markdown.render(escaped_html, mut renderer) or { '' }
highlighted_code := html_highlight(dn.content, tb)
node_class := if dn.kind == .const_group { ' const' } else { '' }
sym_name := get_sym_name(dn)
Expand Down Expand Up @@ -514,3 +519,28 @@ fn write_toc(dn doc.DocNode, mut toc strings.Builder) {
fn no_quotes(s string) string {
return s.replace_each(no_quotes_replacement)
}

struct MdHtmlCodeHighlighter {
mut:
language string
table &ast.Table
}

fn (f &MdHtmlCodeHighlighter) transform_attribute(p markdown.ParentType, name string, value string) string {
return markdown.default_html_transformer.transform_attribute(p, name, value)
}

fn (f &MdHtmlCodeHighlighter) transform_content(parent markdown.ParentType, text string) string {
if parent is markdown.MD_BLOCKTYPE && parent == .md_block_code {
if f.language == 'v' || f.language == 'vlang' {
return html_highlight(text, f.table)
}
}
return markdown.default_html_transformer.transform_content(parent, text)
}

fn (mut f MdHtmlCodeHighlighter) config_set(key string, val string) {
if key == 'code_language' {
f.language = val
}
}

0 comments on commit bdfd2b3

Please sign in to comment.