From 427b6dbcbb21302cc55935fbcf84d9ac03bd9fd2 Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+ttytm@users.noreply.github.com> Date: Mon, 8 Apr 2024 17:45:51 +0200 Subject: [PATCH] vdoc: improve line merging for readme contents (#21224) --- cmd/tools/vdoc/html.v | 21 +++++++++++++++++-- .../tests/testdata/output_formats/main.html | 2 +- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/cmd/tools/vdoc/html.v b/cmd/tools/vdoc/html.v index e2320d7056..bfbea197b4 100644 --- a/cmd/tools/vdoc/html.v +++ b/cmd/tools/vdoc/html.v @@ -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() } diff --git a/cmd/tools/vdoc/tests/testdata/output_formats/main.html b/cmd/tools/vdoc/tests/testdata/output_formats/main.html index 748c7180a2..5161433786 100644 --- a/cmd/tools/vdoc/tests/testdata/output_formats/main.html +++ b/cmd/tools/vdoc/tests/testdata/output_formats/main.html @@ -1,6 +1,6 @@

main #

-

Description

This is an example of a an .md file, used for adding more rich textdocumentation in a project or module.

This is a link to the main V site.

This is a bold text.

This is a script <script>console.log('hi from README.md');</script> .

Examples

Processing command line args

import os
+

Description

This is an example of a an .md file, used for adding more rich text documentation in a project or module.

This is a link to the main V site.

This is a bold text.

This is a script <script>console.log('hi from README.md');</script> .

Examples

Processing command line args

import os
 
 fn main() {
     dump(os.args)