diff --git a/cmd/tools/vdoc.v b/cmd/tools/vdoc.v index ead4b57b6d..204e22e60c 100644 --- a/cmd/tools/vdoc.v +++ b/cmd/tools/vdoc.v @@ -525,40 +525,56 @@ fn (cfg DocConfig) gen_plaintext(idx int) string { if dcs.head.comment.trim_space().len > 0 && !cfg.pub_only { pw.writeln(dcs.head.comment.split_into_lines().map(' ' + it).join('\n')) } - for _, cn in dcs.contents { - pw.writeln(cn.content) - if cn.comment.len > 0 && !cfg.pub_only { - pw.writeln(cn.comment.trim_space().split_into_lines().map(' ' + it).join('\n')) - } - if cfg.show_loc { - pw.writeln('Location: $cn.file_path:$cn.pos.line\n') - } - } + cfg.write_plaintext_content(dcs.contents.arr(), mut pw) return pw.str() } +fn (cfg DocConfig) write_plaintext_content(contents []doc.DocNode, mut pw strings.Builder) { + for cn in contents { + if cn.content.len > 0 { + pw.writeln(cn.content) + if cn.comment.len > 0 && !cfg.pub_only { + pw.writeln(cn.comment.trim_space().split_into_lines().map(' ' + it).join('\n')) + } + if cfg.show_loc { + pw.writeln('Location: $cn.file_path:$cn.pos.line\n') + } + } + cfg.write_plaintext_content(cn.children, mut pw) + } +} + fn (cfg DocConfig) gen_markdown(idx int, with_toc bool) string { dcs := cfg.docs[idx] mut hw := strings.new_builder(200) mut cw := strings.new_builder(200) - hw.writeln('# $dcs.head.content\n$dcs.head.comment\n') + hw.writeln('# $dcs.head.content\n') + if dcs.head.comment.len > 0 { + hw.writeln('$dcs.head.comment\n') + } if with_toc { hw.writeln('## Contents') } - for _, cn in dcs.contents { - name := cn.name.all_after(dcs.head.name + '.') - if with_toc { - hw.writeln('- [#$name](${slug(name)})') - } - cw.writeln('## $name') - cw.writeln('```v\n$cn.content\n```$cn.comment\n') - cw.writeln('[\[Return to contents\]](#Contents)\n') - } + cfg.write_markdown_content(dcs.contents.arr(), mut cw, mut hw, 0, with_toc) footer_text := cfg.gen_footer_text(idx) cw.writeln('#### $footer_text') return hw.str() + '\n' + cw.str() } +fn (cfg DocConfig) write_markdown_content(contents []doc.DocNode, mut cw strings.Builder, mut hw strings.Builder, indent int, with_toc bool) { + for cn in contents { + if with_toc && cn.name.len > 0 { + hw.writeln(' '.repeat(2 * indent) + '- [#$cn.name](${slug(cn.name)})') + cw.writeln('## $cn.name') + } + if cn.content.len > 0 { + cw.writeln('```v\n$cn.content\n```$cn.comment\n') + cw.writeln('[\[Return to contents\]](#Contents)\n') + } + cfg.write_markdown_content(cn.children, mut cw, mut hw, indent + 1, with_toc) + } +} + fn (cfg DocConfig) gen_footer_text(idx int) string { current_doc := cfg.docs[idx] footer_text := 'Powered by vdoc.' diff --git a/vlib/v/doc/doc.v b/vlib/v/doc/doc.v index 3def4e0ceb..331cee4ad0 100644 --- a/vlib/v/doc/doc.v +++ b/vlib/v/doc/doc.v @@ -296,6 +296,9 @@ pub fn (mut d Doc) file_ast(file_ast ast.File) map[string]DocNode { } } d.fmt.mod2alias = map[string]string{} + if contents[''].kind != .const_group { + contents.delete('') + } return contents } diff --git a/vlib/v/doc/doc_test.v b/vlib/v/doc/doc_test.v index 16a5ad85fa..a9c9ab7d28 100644 --- a/vlib/v/doc/doc_test.v +++ b/vlib/v/doc/doc_test.v @@ -13,6 +13,6 @@ fn test_generate_from_mod() { } assert nested_mod_doc.head.name == nested_mod_name assert nested_mod_doc.head.content == 'module $nested_mod_name' - assert nested_mod_doc.contents.len == 4 + assert nested_mod_doc.contents.len == 3 assert nested_mod_doc.contents['ChunkScanner'].children.len == 3 }