18

Is there a .net api that can do this? I saw Pandoc has a standalone exe that I could wrap but I'd rather not if there is something already out there. Any suggestions?

Rob
  • 3,026
  • 4
  • 30
  • 32

2 Answers2

27

Here's the code I used to wrap pandoc. I haven't seen any other decent methods so far unfortunately.

public string Convert(string source)
{
    string processName = @"C:\Program Files\Pandoc\bin\pandoc.exe";
    string args = String.Format(@"-r html -t mediawiki");

    ProcessStartInfo psi = new ProcessStartInfo(processName, args);

    psi.RedirectStandardOutput = true;
    psi.RedirectStandardInput = true;

    Process p = new Process();
    p.StartInfo = psi;
    psi.UseShellExecute = false;
    p.Start();

    string outputString = "";
    byte[] inputBuffer = Encoding.UTF8.GetBytes(source);
    p.StandardInput.BaseStream.Write(inputBuffer, 0, inputBuffer.Length);
    p.StandardInput.Close();

    p.WaitForExit(2000);
    using (System.IO.StreamReader sr = new System.IO.StreamReader(
                                           p.StandardOutput.BaseStream))
    {

        outputString = sr.ReadToEnd();
    }

    return outputString;
}
Marek
  • 10,307
  • 8
  • 70
  • 106
Rob
  • 3,026
  • 4
  • 30
  • 32
  • 9
    Two small changes: "ASCIIEncoding.UTF8" should be "Encoding.UTF8", and you can replace "Thread.Sleep(2000)" with "p.WaitForExit(2000)", which will return sooner if the process exits sooner. – Richard Deeming Oct 03 '12 at 12:29
  • 1
    Why `string args = String.Format(@"-r html -t mediawiki");` and not just `string args = "-r html -t mediawiki";`? Is there any intended side effect of String.Format that I'm missing? – Marcus Mangelsdorf Apr 09 '21 at 14:36
14

I have created a library Html2Markdown. Usage is very simple.

var markdown = new Converter().Convert(html);

Where html is the string representation of the HTML you wish to convert. I actively support it and happily accept contributions.

baynezy
  • 6,493
  • 10
  • 48
  • 73