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' } head_tag := if head { 'h1' } else { 'h2' }
// Allow README.md to go through unescaped except for script tags // Allow README.md to go through unescaped except for script tags
escaped_html := if head && is_module_readme(dn) { escaped_html := if head && is_module_readme(dn) {
// Strip markdown [TOC] directives, since we generate our own. readme_lines := dn.comments[0].text.split_into_lines()
dn.comments[0].text 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 { } else {
dn.merge_comments() dn.merge_comments()
} }

View file

@ -1,6 +1,6 @@
<section id="readme_main" class="doc-node"> <section id="readme_main" class="doc-node">
<div class="title"><h1> main <a href="#readme_main">#</a></h1></div> <div class="title"><h1> main <a href="#readme_main">#</a></h1></div>
<h2>Description</h2><p>This is an example of a an .md file, used for adding more rich textdocumentation in a project or module.</p><p>This is a <a href="https://vlang.io/">link</a> to the main V site.</p><p>This is a <b>bold text</b>.</p><p>This is a script <code>&lt;script&gt;console.log('hi from README.md');&lt;/script&gt;</code> .</p><h2>Examples</h2><h3>Processing command line args</h3><pre><code class="language-v"><span class="token keyword">import</span> os <h2>Description</h2><p>This is an example of a an .md file, used for adding more rich text documentation in a project or module.</p><p>This is a <a href="https://vlang.io/">link</a> to the main V site.</p><p>This is a <b>bold text</b>.</p><p>This is a script <code>&lt;script&gt;console.log('hi from README.md');&lt;/script&gt;</code> .</p><h2>Examples</h2><h3>Processing command line args</h3><pre><code class="language-v"><span class="token keyword">import</span> os
<span class="token keyword">fn</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">fn</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">dump</span><span class="token punctuation">(</span>os<span class="token punctuation">.</span>args<span class="token punctuation">)</span> <span class="token keyword">dump</span><span class="token punctuation">(</span>os<span class="token punctuation">.</span>args<span class="token punctuation">)</span>