vdoc: improve line merging for readme contents (#21224)

This commit is contained in:
Turiiya 2024-04-08 17:45:51 +02:00 committed by GitHub
parent 4e8d00d368
commit 427b6dbcbb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 3 deletions

View file

@ -501,8 +501,25 @@ fn doc_node_html(dn doc.DocNode, link string, head bool, include_examples bool,
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) {
// Strip markdown [TOC] directives, since we generate our own.
dn.comments[0].text
readme_lines := dn.comments[0].text.split_into_lines()
mut merged_lines := []string{}
mut is_codeblock := false
for i := 0; i < readme_lines.len - 1; i++ {
l := readme_lines[i]
nl := readme_lines[i + 1]
is_codeblock_divider := l.trim_left('\x01').trim_space().starts_with('```')
if is_codeblock_divider {
is_codeblock = !is_codeblock
}
if !is_codeblock && !is_codeblock_divider && l != '' && nl != ''
&& !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()
}