Skip to content

Commit 40c24f4

Browse files
committed
Refactor and other stuff
1 parent 24fe347 commit 40c24f4

File tree

12 files changed

+150
-205
lines changed

12 files changed

+150
-205
lines changed

docs/_docset.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ toc:
9696
- file: code.md
9797
- file: comments.md
9898
- file: conditionals.md
99-
- file: csv-file.md
99+
- file: csv-include.md
100100
- hidden: diagrams.md
101101
- file: dropdowns.md
102102
- file: definition-lists.md
Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# CSV files
22

3-
The `{csv-file}` directive allows you to include and render CSV files as formatted tables in your documentation. The directive automatically parses CSV content and renders it using the standard table styles defined in `table.css`.
3+
The `{csv-include}` directive allows you to include and render CSV files as formatted tables in your documentation. The directive automatically parses CSV content and renders it using the standard table styles defined in `table.css`.
44

55
## Usage
66

77
:::::{tab-set}
88

99
::::{tab-item} Output
1010

11-
:::{csv-file} ../_snippets/sample-data.csv
11+
:::{csv-include} ../_snippets/sample-data.csv
1212
:caption: Sample user data from the database
1313
:::
1414

@@ -17,7 +17,7 @@ The `{csv-file}` directive allows you to include and render CSV files as formatt
1717
::::{tab-item} Markdown
1818

1919
```markdown
20-
:::{csv-file} _snippets/sample-data.csv
20+
:::{csv-include} _snippets/sample-data.csv
2121
:::
2222
```
2323

@@ -34,7 +34,7 @@ The CSV file directive supports several options to customize the table rendering
3434
Add a descriptive caption above the table:
3535

3636
```markdown
37-
:::{csv-file} _snippets/sample-data.csv
37+
:::{csv-include} _snippets/sample-data.csv
3838
:caption: Sample user data from the database
3939
:::
4040
```
@@ -44,33 +44,25 @@ Add a descriptive caption above the table:
4444
Specify a custom field separator (default is comma):
4545

4646
```markdown
47-
:::{csv-file} _snippets/sample-data.csv
47+
:::{csv-include} _snippets/sample-data.csv
4848
:separator: ;
4949
:::
5050
```
5151

52-
### Size and performance limits
52+
### Performance limits
5353

54-
Control how much data is loaded and displayed:
54+
The directive includes built-in performance limits to handle large files efficiently:
5555

56-
```markdown
57-
:::{csv-file} _snippets/large-dataset.csv
58-
:max-rows: 5000
59-
:max-columns: 50
60-
:max-size: 20MB
61-
:::
62-
```
63-
64-
- **max-rows**: Maximum number of rows to display (default: 10,000)
65-
- **max-columns**: Maximum number of columns to display (default: 100)
66-
- **max-size**: Maximum file size to process (default: 10MB). Supports KB, MB, GB units.
56+
- **Row limit**: Maximum of 10,000 rows will be displayed
57+
- **Column limit**: Maximum of 100 columns will be displayed
58+
- **File size limit**: Maximum file size of 10MB
6759

6860
### Preview mode
6961

7062
For very large files, enable preview mode to show only the first 100 rows:
7163

7264
```markdown
73-
:::{csv-file} _snippets/huge-dataset.csv
65+
:::{csv-include} _snippets/huge-dataset.csv
7466
:preview-only: true
7567
:::
7668
```
@@ -80,11 +72,10 @@ For very large files, enable preview mode to show only the first 100 rows:
8072
The CSV directive is optimized for large files:
8173

8274
- Files are processed using streaming to avoid loading everything into memory
83-
- Size validation prevents processing of files that exceed the specified limits
75+
- Built-in size validation prevents processing of files that exceed 10MB
8476
- Row and column limits protect against accidentally rendering massive tables
8577
- Warning messages are displayed when limits are exceeded
8678

8779
For optimal performance with large CSV files, consider:
88-
- Setting appropriate `max-rows` and `max-columns` limits
8980
- Using `preview-only: true` for exploratory data viewing
90-
- Increasing `max-size` only when necessary
81+
- Breaking very large files into smaller, more manageable chunks

docs/syntax/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ The following directives are available in Elastic Docs V3:
1818

1919
* [Admonitions](admonitions.md) - Callouts and warnings
2020
* [Code blocks](code.md) - Syntax-highlighted code
21-
* [CSV file](csv-file.md) - Render CSV files as tables
21+
* [CSV include](csv-include.md) - Render CSV files as tables
2222
* [Diagrams](diagrams.md) - Visual diagrams and charts
2323
* [Dropdowns](dropdowns.md) - Collapsible content
2424
* [Images](images.md) - Enhanced image handling

src/Elastic.Markdown/Myst/Directives/CsvFile/CsvFileView.cshtml

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/Elastic.Markdown/Myst/Directives/CsvFile/CsvFileViewModel.cs

Lines changed: 0 additions & 64 deletions
This file was deleted.

src/Elastic.Markdown/Myst/Directives/CsvFile/CsvFileBlock.cs renamed to src/Elastic.Markdown/Myst/Directives/CsvInclude/CsvIncludeBlock.cs

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
using System.IO.Abstractions;
77
using Elastic.Markdown.Diagnostics;
88

9-
namespace Elastic.Markdown.Myst.Directives.CsvFile;
9+
namespace Elastic.Markdown.Myst.Directives.CsvInclude;
1010

11-
public class CsvFileBlock(DirectiveBlockParser parser, ParserContext context) : DirectiveBlock(parser, context)
11+
public class CsvIncludeBlock(DirectiveBlockParser parser, ParserContext context) : DirectiveBlock(parser, context)
1212
{
13-
public override string Directive => "csv-file";
13+
public override string Directive => "csv-include";
1414

1515
public string? CsvFilePath { get; private set; }
1616
public string? CsvFilePathRelativeToSource { get; private set; }
@@ -30,15 +30,6 @@ public override void FinalizeAndValidate(ParserContext context)
3030
if (!string.IsNullOrEmpty(separator))
3131
Separator = separator;
3232

33-
if (int.TryParse(Prop("max-rows"), out var maxRows) && maxRows > 0)
34-
MaxRows = maxRows;
35-
36-
if (ParseFileSize(Prop("max-size"), out var maxSize) && maxSize > 0)
37-
MaxFileSizeBytes = maxSize;
38-
39-
if (int.TryParse(Prop("max-columns"), out var maxColumns) && maxColumns > 0)
40-
MaxColumns = maxColumns;
41-
4233
PreviewOnly = bool.TryParse(Prop("preview-only"), out var preview) && preview;
4334

4435
ExtractCsvPath(context);
@@ -49,7 +40,7 @@ private void ExtractCsvPath(ParserContext context)
4940
var csvPath = Arguments;
5041
if (string.IsNullOrWhiteSpace(csvPath))
5142
{
52-
this.EmitError("csv-file requires an argument specifying the path to the CSV file.");
43+
this.EmitError("csv-include requires an argument specifying the path to the CSV file.");
5344
return;
5445
}
5546

@@ -81,7 +72,7 @@ private void ValidateFileSize()
8172
{
8273
var sizeMB = fileInfo.Length / (1024.0 * 1024.0);
8374
var maxSizeMB = MaxFileSizeBytes / (1024.0 * 1024.0);
84-
this.EmitError($"CSV file `{CsvFilePath}` is {sizeMB:F1}MB, which exceeds the maximum allowed size of {maxSizeMB:F1}MB. Use :max-size to increase the limit.");
75+
this.EmitError($"CSV file `{CsvFilePath}` is {sizeMB:F1}MB, which exceeds the maximum allowed size of {maxSizeMB:F1}MB.");
8576
Found = false;
8677
}
8778
}
@@ -91,39 +82,4 @@ private void ValidateFileSize()
9182
Found = false;
9283
}
9384
}
94-
95-
private static bool ParseFileSize(string? sizeString, out long bytes)
96-
{
97-
bytes = 0;
98-
if (string.IsNullOrEmpty(sizeString))
99-
return false;
100-
101-
var multiplier = 1L;
102-
var value = sizeString.ToUpperInvariant();
103-
104-
if (value.EndsWith("KB"))
105-
{
106-
multiplier = 1024;
107-
value = value[..^2];
108-
}
109-
else if (value.EndsWith("MB"))
110-
{
111-
multiplier = 1024 * 1024;
112-
value = value[..^2];
113-
}
114-
else if (value.EndsWith("GB"))
115-
{
116-
multiplier = 1024 * 1024 * 1024;
117-
value = value[..^2];
118-
}
119-
120-
if (double.TryParse(value, out var numericValue))
121-
{
122-
bytes = (long)(numericValue * multiplier);
123-
return true;
124-
}
125-
126-
return false;
127-
}
128-
12985
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
@using Elastic.Markdown.Myst.Directives.CsvInclude
2+
@inherits RazorSlice<CsvIncludeViewModel>
3+
4+
@{
5+
var csvBlock = (CsvIncludeBlock)Model.DirectiveBlock;
6+
if (!csvBlock.Found)
7+
{
8+
return;
9+
}
10+
11+
var csvRows = Model.GetCsvRows().ToList();
12+
if (!csvRows.Any())
13+
{
14+
return;
15+
}
16+
}
17+
18+
<div class="table-wrapper">
19+
@if (!string.IsNullOrEmpty(csvBlock.Caption))
20+
{
21+
<caption>@csvBlock.Caption</caption>
22+
}
23+
24+
<table>
25+
<thead>
26+
<tr>
27+
@for (var i = 0; i < csvRows[0].Length; i++)
28+
{
29+
<th>@csvRows[0][i]</th>
30+
}
31+
</tr>
32+
</thead>
33+
34+
<tbody>
35+
@for (var rowIndex = 1; rowIndex < csvRows.Count; rowIndex++)
36+
{
37+
<tr>
38+
@for (var i = 0; i < csvRows[rowIndex].Length; i++)
39+
{
40+
<td>@csvRows[rowIndex][i]</td>
41+
}
42+
</tr>
43+
}
44+
</tbody>
45+
</table>
46+
</div>

0 commit comments

Comments
 (0)