Search code examples
c#.netmarkdown

Convert Html or RTF to Markdown or Wiki Compatible syntax?


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?


Solution

  • 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;
    }