Skip to content

Commit 2de46fb

Browse files
authored
Refine TOC validation logic and error messaging. (#827)
Improve handling of TOC nesting rules by adding checks for valid file paths and better accommodating development and narrative docs. Update error messages for clarity and adjust depth validation to reflect maximum allowed levels. (cherry picked from commit 3e61bec)
1 parent b355058 commit 2de46fb

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/Elastic.Markdown/IO/Configuration/TableOfContentsConfiguration.cs

+21-3
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,26 @@ private IReadOnlyCollection<ITocItem> ReadChildren()
8282
case "toc":
8383
var children = ReadChildren(reader, entry.Entry);
8484
var tocEntries = TableOfContents.OfType<TocReference>().ToArray();
85-
if (!_configuration.DevelopmentDocs && !_configuration.IsNarrativeDocs && tocEntries.Length > 0 && TableOfContents.Count != tocEntries.Length)
86-
reader.EmitError("toc links to other toc sections it may only contain other toc references", entry.Key);
85+
86+
// if no nested toc sections simply return
87+
if (tocEntries.Length == 0)
88+
return children;
89+
90+
// dev docs may mix and match as they please because they publish in isolation
91+
if (_configuration.DevelopmentDocs)
92+
return children;
93+
94+
// narrative docs may put files at the root as they please.
95+
if (_configuration.IsNarrativeDocs && _depth == 0)
96+
return children;
97+
98+
var filePaths = children.OfType<FileReference>().ToArray();
99+
if (filePaths.Length == 0 && _depth == 0)
100+
return children;
101+
if (filePaths.Length is > 1 or 0)
102+
reader.EmitError("toc with nested toc sections must only link a single file: index.md", entry.Key);
103+
else if (!filePaths[0].Path.EndsWith("index.md"))
104+
reader.EmitError($"toc with nested toc sections must only link a single file: 'index.md' actually linked {filePaths[0].Path}", entry.Key);
87105
return children;
88106
}
89107
}
@@ -97,7 +115,7 @@ private IReadOnlyCollection<ITocItem> ReadChildren(YamlStreamReader reader, KeyV
97115
parentPath ??= _parentPath;
98116
if (_depth > _maxTocDepth)
99117
{
100-
reader.EmitError($"toc.yml files may only be linked from docset.yml", entry.Key);
118+
reader.EmitError($"toc.yml files may not be linked deeper than {_maxTocDepth} current depth {_depth}", entry.Key);
101119
return [];
102120
}
103121

0 commit comments

Comments
 (0)