Home

Awesome

Markdig.Extensions.ScriptCs

This is an extension for Markdig that allows C# scripting to generate markdown or HTML content.

Prerequisites

Instantiate Markdig.Extensions.ScriptCs into the scripting runtime:

```ScriptCs
#r Markdig.Extensions.ScriptCs.dll
using Markdig.Extensions.ScriptCs;
```

This only needs to be done once in your document. This gives access to MarkdownDocument.Instance.

Inline

Markdown:
This is some `ScriptCs MarkdownDocument.Instance.InsertMarkdown("_italic_");` text.
Output:

This is some italic text.

Markdown:
This is some `ScriptCs MarkdownDocument.Instance.InsertHtml("<em>italic</em>");` text.
Output:

This is some italic text.

Code Block

Markdown:
```ScriptCs
MarkdownDocument.Instance.InsertMarkdown("Hello World!");
```
Output:

Hello World!

Markdown:
```ScriptCs
MarkdownDocument.Instance.InsertHtml("<p>Hello World!</p>");
```
Output:

Hello World!

OxyPlot

Markdown:
```ScriptCs
#r OxyPlot.dll
#r System.Runtime.dll
using OxyPlot;
using OxyPlot.Series;
var myModel = new PlotModel { Title = "Example 1" };
myModel.Series.Add(new FunctionSeries(Math.Cos, 0, 10, 0.1, "cos(x)"));
var exporter = new SvgExporter { Width = 800, Height = 400 };
MarkdownDocument.Instance.InsertHtml(exporter.ExportToString(myModel));
```
Output:

./Images/oxyplot.svg

Separation of code from content

It is possible to separate large amounts of code into csx files which can be referenced in the markdown. The csx file must be located in the bin directory.

Markdown:
```ScriptCs
#load OxyPlot.csx
RenderChart();
```
OxyPlot.csx:
#r OxyPlot.dll
#r System.Runtime.dll
using OxyPlot;
using OxyPlot.Series;

public void RenderChart()
{
    var myModel = new PlotModel { Title = "Example 1" };
    myModel.Series.Add(new FunctionSeries(Math.Cos, 0, 10, 0.1, "cos(x)"));
    var exporter = new SvgExporter { Width = 800, Height = 400 };
    MarkdownDocument.Instance.InsertHtml(exporter.ExportToString(myModel));
}

Exception handling

Markdown:
```ScriptCs
var fakeModel = new FakeModel { Title = "Test" };
```
Output:
ScriptCs exception:
(1,21): error CS0246: The type or namespace name 'FakeModel' could not be found (are you missing a using directive or an assembly reference?)

More examples

This PDF was generated by the Chrome print dialog from the HTML generated by Markdig using input.md as the source. It coincidentally shows the use of CSS to get page breaks in the PDF output. https://github.com/macaba/Markdig.Extensions.ScriptCs/blob/master/Markdig.Extensions.ScriptCs.ConsoleApp/Example%20output/output.pdf