From e387fcbdcbc7d3b28f8b159aaec27d18d1e9b424 Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+ttytm@users.noreply.github.com> Date: Mon, 15 Apr 2024 14:00:36 +0200 Subject: [PATCH] vdoc: fix markdown lists in html (#21287) --- cmd/tools/vdoc/html.v | 16 ++++++++++++---- .../vdoc/tests/testdata/output_formats/README.md | 8 ++++++++ .../vdoc/tests/testdata/output_formats/main.ansi | 13 +++++++++++++ .../vdoc/tests/testdata/output_formats/main.html | 2 +- .../vdoc/tests/testdata/output_formats/main.text | 5 +++++ 5 files changed, 39 insertions(+), 5 deletions(-) diff --git a/cmd/tools/vdoc/html.v b/cmd/tools/vdoc/html.v index 5843a73fcd839d..96515c2e49e2ef 100644 --- a/cmd/tools/vdoc/html.v +++ b/cmd/tools/vdoc/html.v @@ -511,13 +511,21 @@ fn doc_node_html(dn doc.DocNode, link string, head bool, include_examples bool, 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++ { + for i := 0; i < readme_lines.len; i++ { l := readme_lines[i] - nl := readme_lines[i + 1] - if l.trim_left('\x01').trim_space().starts_with('```') { + 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 !is_codeblock && l != '' && nl != '' + // -> 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++ diff --git a/cmd/tools/vdoc/tests/testdata/output_formats/README.md b/cmd/tools/vdoc/tests/testdata/output_formats/README.md index ba5f6a861e8017..b709d705e3f160 100644 --- a/cmd/tools/vdoc/tests/testdata/output_formats/README.md +++ b/cmd/tools/vdoc/tests/testdata/output_formats/README.md @@ -130,3 +130,11 @@ const html = ' ' ``` + +- Regular markdown list point 1 +- List point 2 +- List point 3 + +1. Numbered markdown list point 1 +2. List point 2 +3. List point 3 diff --git a/cmd/tools/vdoc/tests/testdata/output_formats/main.ansi b/cmd/tools/vdoc/tests/testdata/output_formats/main.ansi index 66a1d61bbbfe39..cf17e0d2c590a5 100644 --- a/cmd/tools/vdoc/tests/testdata/output_formats/main.ansi +++ b/cmd/tools/vdoc/tests/testdata/output_formats/main.ansi @@ -110,6 +110,14 @@ const html = ' } ' + +Regular markdown list point 1 +List point 2 +List point 3 + +Numbered markdown list point 1 +List point 2 +List point 3 module main ## Description @@ -243,6 +251,11 @@ const html = ' ' ``` + - Regular markdown list point 1 + - List point 2 + - 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 diff --git a/cmd/tools/vdoc/tests/testdata/output_formats/main.html b/cmd/tools/vdoc/tests/testdata/output_formats/main.html index 38813c787936db..8074ee3e92aa66 100644 --- a/cmd/tools/vdoc/tests/testdata/output_formats/main.html +++ b/cmd/tools/vdoc/tests/testdata/output_formats/main.html @@ -90,7 +90,7 @@

Description

This is an example of a an .md file, used for adding more console.log(res); } </script> -</html>' +</html>'

  1. Numbered markdown list point 1
  2. List point 2
  3. List point 3
diff --git a/cmd/tools/vdoc/tests/testdata/output_formats/main.text b/cmd/tools/vdoc/tests/testdata/output_formats/main.text index 092472890685cf..01ef2829c311df 100644 --- a/cmd/tools/vdoc/tests/testdata/output_formats/main.text +++ b/cmd/tools/vdoc/tests/testdata/output_formats/main.text @@ -131,6 +131,11 @@ module main ' ``` + - Regular markdown list point 1 + - List point 2 + - 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