mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
vdoc: support for highlighting code in README's (#19725)
This commit is contained in:
parent
be487b3c36
commit
6c83d9a205
1 changed files with 31 additions and 1 deletions
|
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue