GitHub - macaba/Markdig.Extensions.ScriptCs: An extension for Markdig that allows C# scripting to generate markdown or HTML content. (original) (raw)

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:

#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:

[](#output-2)

Hello World!

##### Markdown:

[](#markdown-3)
MarkdownDocument.Instance.InsertHtml("<p>Hello World!</p>");

##### Output:

[](#output-3)

Hello World!

## OxyPlot

[](#oxyplot)

##### Markdown:

[](#markdown-4)
#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:

[](#output-4)

[![./Images/oxyplot.svg](https://github.com/macaba/Markdig.Extensions.ScriptCs/raw/master/Images/oxyplot.svg)](/macaba/Markdig.Extensions.ScriptCs/blob/master/Images/oxyplot.svg)

## Separation of code from content

[](#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:

[](#markdown-5)
#load OxyPlot.csx
RenderChart();

##### OxyPlot.csx:

[](#oxyplotcsx)

#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

[](#exception-handling)

##### Markdown:

[](#markdown-6)
var fakeModel = new FakeModel { Title = "Test" };

##### Output:

[](#output-5)

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