Skip to content

Commit 183c199

Browse files
authored
vdoc: improve creation of trimmed node descriptions, extend tests (#21281)
1 parent 7142463 commit 183c199

File tree

5 files changed

+46
-9
lines changed

5 files changed

+46
-9
lines changed

.github/workflows/module_docs_ci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ jobs:
1313
- uses: actions/checkout@v4
1414
- name: Build V
1515
run: make
16+
- name: Test v doc
17+
run: |
18+
# While the integration tests (executing the v doc command) should install
19+
# markdown automatically, unit tests won't. Run integration tests first.
20+
./v test cmd/tools/vdoc/tests/
21+
./v test cmd/tools/vdoc/*.v
1622
- name: Build module documentation
1723
run: ./v doc -m -f html vlib/
1824
- name: Deploy docs to vercel

cmd/tools/vdoc/html.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ fn (mut vd VDoc) collect_search_index(out Output) {
132132
doc.head.merge_comments_without_examples()
133133
}
134134
vd.search_module_data << SearchModuleResult{
135-
description: trim_doc_node_description(comments)
135+
description: trim_doc_node_description(mod, comments)
136136
link: vd.get_file_name(mod, out)
137137
}
138138
for _, dn in doc.contents {
@@ -151,7 +151,7 @@ fn (mut vd VDoc) create_search_results(mod string, dn doc.DocNode, out Output) {
151151
} else {
152152
dn.merge_comments_without_examples()
153153
}
154-
dn_description := trim_doc_node_description(comments)
154+
dn_description := trim_doc_node_description(dn.name, comments)
155155
vd.search_index << dn.name
156156
vd.search_data << SearchResult{
157157
prefix: if dn.parent_name != '' { '${dn.kind} (${dn.parent_name})' } else { '${dn.kind} ' }

cmd/tools/vdoc/utils.v

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,25 @@ fn is_module_readme(dn doc.DocNode) bool {
3333
return dn.comments.len > 0 && (dn.content == 'module ${dn.name}' || dn.name == 'README')
3434
}
3535

36-
fn trim_doc_node_description(desc string) string {
36+
// trim_doc_node_description returns the nodes trimmed description.
37+
// An example use are the descriptions of the search results in the sidebar.
38+
fn trim_doc_node_description(mod_name string, desc string) string {
3739
mut dn_desc := desc.replace_each(['\r\n', '\n', '"', '\\"'])
38-
// Handle module READMEs.
39-
if dn_desc.starts_with('## Description\n\n') {
40-
dn_desc = dn_desc['## Description\n\n'.len..].all_before('\n')
41-
if dn_desc.starts_with('`') && dn_desc.count('`') > 1 {
42-
dn_desc = dn_desc.all_after('`').all_after('`').trim_left(' ')
40+
// Get the first "descriptive" line.
41+
if dn_desc.starts_with('#') {
42+
// Handle module READMEs.
43+
for l in dn_desc.split_into_lines()[1..] {
44+
if l != '' && !l.starts_with('#') {
45+
quoted_mod_name := '`${mod_name}`'
46+
if l.starts_with(quoted_mod_name) {
47+
// Omit the module name in the description as it is redundant since the name is displayed as well.
48+
// "`arrays` is a module that..." -> "is a module that..."
49+
dn_desc = l.all_after(quoted_mod_name).trim_left(' ')
50+
} else {
51+
dn_desc = l
52+
}
53+
break
54+
}
4355
}
4456
} else {
4557
dn_desc = dn_desc.all_before('\n')
@@ -48,7 +60,7 @@ fn trim_doc_node_description(desc string) string {
4860
if dn_desc.len > 80 {
4961
dn_desc = dn_desc[..80]
5062
}
51-
// If `\` is last character, it ends with `\"` which leads to a JS error.
63+
// If `\` is the last character, it ends with `\"` which leads to a JS error.
5264
return dn_desc.trim_string_right('\\')
5365
}
5466

cmd/tools/vdoc/utils_test.v

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module main
2+
3+
fn test_trim_doc_node_description() {
4+
mod := 'foo'
5+
mut readme := '## Description
6+
7+
`foo` is a module that provides tools and utility functions to assist in working with bar.
8+
It also assists with composing and testing baz.'
9+
expected := 'is a module that provides tools and utility functions to assist in working with'
10+
res := trim_doc_node_description(mod, readme).trim_space()
11+
assert res == expected
12+
13+
readme = '# Foo
14+
`foo` is a module that provides tools and utility functions to assist in working with bar.
15+
It also assists with composing and testing baz.'
16+
res2 := trim_doc_node_description(mod, readme).trim_space()
17+
assert res2 == res
18+
}

cmd/tools/vtest-self.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ const essential_list = [
8484
]
8585
const skip_test_files = [
8686
'do_not_remove',
87+
'cmd/tools/vdoc/utils_test.v', // markdown not installed
8788
'vlib/context/deadline_test.v', // sometimes blocks
8889
'vlib/context/onecontext/onecontext_test.v', // backtrace_symbols is missing
8990
'vlib/db/mysql/mysql_orm_test.v', // mysql not installed

0 commit comments

Comments
 (0)