Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Elastic.Markdown/DocumentationGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Elastic.Documentation.Site.Navigation;
using Elastic.Documentation.State;
using Elastic.Markdown.Exporters;
using Elastic.Markdown.Extensions.DetectionRules;
using Elastic.Markdown.IO;
using Elastic.Markdown.Page;
using Markdig.Syntax;
Expand Down Expand Up @@ -429,6 +430,8 @@ Cancel ctx
await _documentationFileExporter.ProcessFile(context, ctx);
if (file is MarkdownFile markdown)
{
// Rules live outside the docs folder; export them where their HTML page is written
var markdownOutputFile = markdown is DetectionRuleFile ? DetectionRuleFile.OutputPath(outputFile, Context) : outputFile;
foreach (var exporter in _markdownExporters)
{
var document = context.MarkdownDocument ??= await markdown.ParseFullAsync(
Expand All @@ -443,7 +446,7 @@ Cancel ctx
Resolvers = DocumentationSet.MarkdownParser.Resolvers,
Document = document,
SourceFile = markdown,
DefaultOutputFile = outputFile,
DefaultOutputFile = markdownOutputFile,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Severity: P3 (Low)

A rule named rules/index.toml can now overwrite the rule overview’s rules.md export. The remapped path makes the LLM exporter treat the rule as a directory index and write <output>/rules.md, even though the HTML writer emits <output>/rules/index/index.html. If the docset also has a docs/rules.md overview, both exporters write the same Markdown file in parallel. Make the HTML and Markdown writers agree on what counts as a directory index.

Generated by Libra. React with 👍 or 👎 to give feedback on this comment.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True for a rule file named index.toml, since LlmMarkdownExporter treats any index page as a folder index. Before this change that copy went to <output>/../rules.md, so it was misplaced either way. elastic/detection-rules has no index.toml today (code search finds none), so I left it out of this PR. I can make the exporter skip the folder-index mapping for DetectionRuleFile here if you'd like it covered.

DocumentationSet = DocumentationSet,
PositionaNavigation = PositionalNavigation,
NavigationItem = navigationItem,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using System.IO.Abstractions;
using AwesomeAssertions;
using Elastic.Documentation;
using Elastic.Documentation.Configuration;
using Elastic.Documentation.Diagnostics;
using Elastic.Documentation.FileSystems;
using Elastic.Markdown.Exporters;
using Elastic.Markdown.IO;

namespace Elastic.Markdown.Tests.DetectionRules;

public class DetectionRuleExportTests
{
// DetectionRule reads the rule from disk, so the docset is written to a temporary folder. The output
// folder is outside the checkout, so a write next to it is refused.
[Test]
public async Task GenerateAll_RuleOutsideDocsFolder_WritesMarkdownNextToHtml()
{
var ctx = TestContext.Current!.Execution.CancellationToken;
var root = Directory.CreateTempSubdirectory("docs-builder-rules-");
try
{
var repo = root.CreateSubdirectory("repo");
WriteFile(
repo,
"docs/docset.yml",
//language=yaml
"""
project: test
extensions:
- detection-rules
toc:
- file: index.md
- file: rules.md
detection_rules:
- ../rules
"""
);
WriteFile(repo, "docs/index.md", "# Home");
WriteFile(repo, "docs/rules.md", "# Rules");
WriteFile(
repo,
"rules/linux/kernel_ring_buffer.toml",
//language=toml
"""
[metadata]
creation_date = "2024/08/01"
maturity = "production"

[rule]
author = ["Elastic"]
description = "Test rule"
name = "Kernel Ring Buffer"
rule_id = "abc-123"
risk_score = 47
severity = "medium"
type = "query"
license = "Elastic License v2"
language = "kuery"
query = "process.name : evil.exe"
"""
);
_ = repo.CreateSubdirectory(".git");

var fs = new FileSystem();
var logger = new TestLoggerFactory();
await using var collector = new DiagnosticsCollector([]).StartAsync(ctx);
var documentationFileSystem = DocumentationFileSystem.Resolve(
fs.DirectoryInfo.New(Path.Join(repo.FullName, "docs")),
new DocumentationScopeOptions { Inner = fs, Output = Path.Join(root.FullName, "out", "html") }
);
var context = new BuildContext(collector, documentationFileSystem, TestHelpers.CreateConfigurationContext(fs))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Severity: P3 (Low)

The test catches the misplaced Markdown copy, but not the scoped-filesystem failure. With .git at root, both root/out/html and the old destination root/out/rules are inside the writable checkout. Add a case where the output is outside the checkout and the write scope denies the old destination.

Generated by Libra. React with 👍 or 👎 to give feedback on this comment.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 8da92bd the checkout is now root/repo and the output root/out/html, outside it. On my machine (Windows) the old write to root/out/rules still isn't refused in that layout, so the test checks where the copy lands rather than the exception. Without the fix it still fails, because the .md is at ../rules/linux/kernel_ring_buffer.md relative to the output folder.

{
SkipStaticAssetExtraction = true
};
var set = new DocumentationSet(context, logger, new TestCrossLinkResolver());
var generator = new DocumentationGenerator(set, logger, markdownExporters: [new LlmMarkdownExporter()]);

await generator.GenerateAll(ctx);
await collector.StopAsync(ctx);

var rulePages = Directory
.EnumerateFiles(root.FullName, "*", SearchOption.AllDirectories)
.Where(f => f.Contains("kernel_ring_buffer", StringComparison.Ordinal) && !f.EndsWith(".toml", StringComparison.Ordinal))
.Select(f => Path.GetRelativePath(set.OutputDirectory.FullName, f).Replace('\\', '/'));
rulePages.Should().BeEquivalentTo("rules/linux/kernel_ring_buffer/index.html", "rules/linux/kernel_ring_buffer.md");
}
finally
{
root.Delete(recursive: true);
}
}

private static void WriteFile(DirectoryInfo root, string relativePath, string content)
{
var path = Path.Join(root.FullName, relativePath);
_ = Directory.CreateDirectory(Path.GetDirectoryName(path)!);
File.WriteAllText(path, content);
}
}
Loading