vdoc: update merge_doc_comments so recent fixes extend to all output formats (#21289)

This commit is contained in:
Turiiya 2024-04-15 17:16:27 +02:00 committed by GitHub
parent 107c43a34d
commit 0b83ea77c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 131 additions and 151 deletions

View File

@ -506,43 +506,12 @@ fn html_highlight(code string, tb &ast.Table) string {
fn doc_node_html(dn doc.DocNode, link string, head bool, include_examples bool, tb &ast.Table) string {
mut dnw := strings.new_builder(200)
head_tag := if head { 'h1' } else { 'h2' }
// Allow README.md to go through unescaped except for script tags
escaped_html := if head && is_module_readme(dn) {
readme_lines := dn.comments[0].text.split_into_lines()
mut merged_lines := []string{}
mut is_codeblock := false
for i := 0; i < readme_lines.len; i++ {
l := readme_lines[i]
nl := readme_lines[i + 1] or {
merged_lines << l
break
}
l_trimmed := l.trim_left('\x01').trim_space()
if l_trimmed.starts_with('```') {
is_codeblock = !is_codeblock
}
// -> if l_trimmed.len > 1 && (is_ul || is_ol)
is_list := l_trimmed.len > 1 && ((l_trimmed[1] == ` ` && l_trimmed[0] in [`*`, `-`])
|| (l_trimmed.len > 2 && l_trimmed[2] == ` ` && l_trimmed[1] == `.`
&& l_trimmed[0].is_digit()))
if !is_codeblock && l != '' && nl != '' && !is_list
&& !nl.trim_left('\x01').trim_space().starts_with('```') {
merged_lines << '${l} ${nl}'
i++
continue
}
merged_lines << l
}
merged_lines.join_lines()
} else {
dn.merge_comments_without_examples()
}
mut renderer := markdown.HtmlRenderer{
transformer: &MdHtmlCodeHighlighter{
table: tb
}
}
md_content := markdown.render(escaped_html, mut renderer) or { '' }
md_content := markdown.render(dn.merge_comments_without_examples(), 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)

View File

@ -255,7 +255,10 @@ List point 3
- List point 2
- List point 3
1. Numbered markdown list point 1 2. List point 2 3. List point 3
1. Numbered markdown list point 1
2. List point 2
3. List point 3
const omega = 3 // should be first
const alpha = 5 // should be in the middle

View File

@ -135,7 +135,10 @@ module main
- List point 2
- List point 3
1. Numbered markdown list point 1 2. List point 2 3. List point 3
1. Numbered markdown list point 1
2. List point 2
3. List point 3
const omega = 3 // should be first
const alpha = 5 // should be in the middle

View File

@ -63,9 +63,11 @@ pub fn merge_doc_comments(comments []DocComment) string {
mut comment := ''
mut next_on_newline := true
mut is_codeblock := false
mut trimmed_indent := ''
for cmt in doc_comments.reverse() {
line_loop: for line in cmt.split_into_lines() {
l := line.trim_left('\x01').trim_space()
l_normalized := line.trim_left('\x01')
l := l_normalized.trim_space()
last_ends_with_lb := comment.ends_with('\n')
if l == '' {
comment += if last_ends_with_lb { '\n' } else { '\n\n' }
@ -74,7 +76,7 @@ pub fn merge_doc_comments(comments []DocComment) string {
}
has_codeblock_quote := l.starts_with('```')
if is_codeblock {
comment += l + '\n'
comment += l_normalized.trim_string_left(trimmed_indent) + '\n'
if has_codeblock_quote {
is_codeblock = !is_codeblock
}
@ -86,11 +88,14 @@ pub fn merge_doc_comments(comments []DocComment) string {
}
comment += l + '\n'
is_codeblock = !is_codeblock
trimmed_indent = l_normalized.all_before(l)
next_on_newline = true
continue
}
is_list := l.len > 1 && ((l[1] == ` ` && l[0] in [`-`, `*`, `+`])
|| (l.len > 2 && l[2] == ` ` && l[1] == `.` && l[0].is_digit()))
line_before_spaces := l.before(' ')
if (l.starts_with('|') && l.ends_with('|')) || l.starts_with('- ')
if is_list || (l.starts_with('|') && l.ends_with('|'))
|| (l.starts_with('#') && line_before_spaces.count('#') == line_before_spaces.len) {
comment += l + '\n'
next_on_newline = true