Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alrikolson/d2sharp
D2Sharp wraps the D2 diagramming library for .NET, allowing you to render D2 diagrams with C# in your .NET applications.
https://github.com/alrikolson/d2sharp
csharp d2 d2lang diagrams dotnet nuget
Last synced: 3 months ago
JSON representation
D2Sharp wraps the D2 diagramming library for .NET, allowing you to render D2 diagrams with C# in your .NET applications.
- Host: GitHub
- URL: https://github.com/alrikolson/d2sharp
- Owner: AlrikOlson
- License: mit
- Created: 2024-04-06T00:16:57.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-09-03T22:54:48.000Z (4 months ago)
- Last Synced: 2024-10-14T03:02:53.446Z (3 months ago)
- Topics: csharp, d2, d2lang, diagrams, dotnet, nuget
- Language: PowerShell
- Homepage:
- Size: 101 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# D2Sharp
[![NuGet](https://img.shields.io/nuget/v/D2Sharp.svg)](https://www.nuget.org/packages/D2Sharp/)
D2Sharp wraps the D2 diagramming library for .NET, allowing you to render D2 diagrams with C# in your .NET applications.
## Features
- Render diagrams as SVG
- Integrate with ASP.NET Core for web applications
- Includes a web demo for quick testing## Prerequisites for Building
- .NET 8.0 SDK or newer
- Go 1.22.2 or newer
- GCC (for compiling the Go wrapper)You can check your setup using the provided scripts:
Windows:
```powershell
.\depcheck.ps1
```Unix-based systems:
```bash
./depcheck.sh
```## Project Structure
- `src/D2Sharp`: Main library project
- `examples/D2Sharp.Web`: Web demo application
- `src/D2Sharp/d2wrapper`: Go wrapper code## Setup
1. Clone the repository
2. Build the project: `dotnet build`## Usage
Basic usage:
```csharp
// Create an instance of D2Wrapper
// You can pass a logger instance if you want to enable logging
var wrapper = new D2Wrapper(logger);// Define your D2 script as a string
var script = @"direction: right
A -> B -> C";// Render the diagram
var svg = wrapper.RenderDiagram(script);// The 'svg' variable now contains the SVG representation of your diagram
// You can save this to a file, display it in a web page, or process it further as needed
```## Error Handling
The `RenderDiagram` method now returns a `RenderResult` object, which includes both the rendered SVG (if successful) and detailed error information (if rendering failed). Here's how you can use it:
```csharp
var wrapper = new D2Wrapper();
string script = @"
A -> B
B -> // This line has an error
C -> D
";var result = wrapper.RenderDiagram(script);
if (result.IsSuccess)
{
Console.WriteLine("Diagram rendered successfully:");
Console.WriteLine(result.Svg);
}
else
{
Console.WriteLine("Error rendering diagram:");
Console.WriteLine($"Message: {result.Error.Message}");
if (result.Error.LineNumber.HasValue)
{
Console.WriteLine($"Line {result.Error.LineNumber}: {result.Error.LineContent}");
}
}
```Running the web demo:
```
cd examples/D2Sharp.Web
dotnet run
```## Acknowledgements
This project would not be possible without the following open-source projects:
- [D2](https://github.com/terrastruct/d2): The underlying diagramming engine
- [.NET](https://github.com/dotnet/runtime): The runtime and framework
- [Go](https://github.com/golang/go): Used for the native wrapper