Markdown to HTML

Markdown to HTML

2
NewbieNewbie
2

    Mar 25, 2023#1

    I installed the trial version of UltraEdit in early March, and have since purchased UltraEdit. My main use of UltraEdit is for Markdown files.

    While UltraEdit allows the saving of a Markdown file to an HTML file, it is time intensive to do so.

    Here is how I am presently saving a Markdown file as an HTML file.

    To save an .md file as an .html file:
    1. From UltraEdit, ensure that the .md file has been saved, then Coding -> Live Preview
    2. Go to the folder where the .md file has been saved. I have a file called TCCWinAPI.md.
    3. There is a file called 0TCCWinAPI.html in that same folder.
      The file 0TCCWinAPI.html exists when Live Preview is active.
      The file 0TCCWinAPI.html does not exist when Live Preview is not active.
    4. The file 0TCCWinAPI.html has

      Code: Select all

      <title>Markdown preview</title>
    5. I want to change that title. I use SED.EXE to make the change.

      Code: Select all

      sed.exe -i -e 's/Markdown preview/Take Command Console Win API Functions/g' 0TCCWinAPI.html
    6. I then copy 0TCCWinAPI.html to TCCWinAPI.html.
    I have started development of a script file to automate this.

    Code: Select all

    var text = UltraEdit.activeDocument.path;
    UltraEdit.messageBox(text, "Results:");
    JavaScript is not my friend, but I am learning.

    Before I proceed further, I was wondering if anyone else has already developed a solution to convert a Markdown file to an HTML file.

    I realize that there are external utilities to convert a .md file to an .html file, but they change the appearance of the HTML file after being converted from a Markdown file. I understand that the appearance involves CSS styles which I am learning about. However, the HTML generated by UltraEdit is what I want.

    A cursory search of the forums did not reveal a solution.

    Constructive suggestions would be appreciated.

    Joe
    UltraEdit
    2022.2.0.52 64-bit

    6,675585
    Grand MasterGrand Master
    6,675585

      Mar 25, 2023#2

      I suggest taking a look at Saving Markdown file as HTML? especially on the last post. I recommend not re-inventing the wheel and code your own Markdown to HTML converter with JavaScript. That does not make sense in my opinion. There are a lot of such tools already available.

      If you like the look in preview of UltraEdit more than the one created by a separate tool, configure in the conversion tool to use the same CSS file as UltraEdit uses to create the HTML file. The four CSS files embedded in UltraEdit can be easily extracted from the temporary created HTML file. Everything between the tags <style> and </style> must be copied into a CSS file configured in the conversion tool for usage. I have done this for all four CSS styles embedded in UltraEdit for Windows v2022.20.0.52 and attached a ZIP file with those four CSS files. It could be that the CSS file must be adapted a little bit depending on the class identifiers used by the conversion tool for the tags in HTML body element of created HTML file.
      markdown_css_files.zip (25.56 KiB)   0
      CSS files used for Markdown live preview embedded in UE for Windows v2022.20.0.52

      There can be used of course also the temporary created HTML file.

      How?

      Configure a user tool with following properties:

      On tab Command:

      Menu item name: Copy HTML (for example)
      Command line: copy /B /Y "0%n.html" "%n.html"
      Working directory: %p
      Toolbar bitmap/icon: (let it empty or browse to a BMP, ICO, PNG file with a suitable icon/image)
      Use embedded icon in executable (if available): unchecked

      On tab Options:

      Program Type: DOS Program
      Save active file: unchecked
      Save all files first: unchecked

      On tab Output:

      Command Output: Appending to existing
      Show DOS Box: unchecked
      Capture output: unchecked
      Clear output before run: unchecked
      Replace selected text with: No replace
      Handle output as: ANSI


      Now there can be written a Markdown file in UltraEdit and with live preview currently toggled on, click on the advanced user tool item Copy HTML or press the hotkey of the configured user tool and UltraEdit executes silently in background in current directory of active Markdown file with file name TCCWinAPI.md:

      Code: Select all

      cmd.exe /c copy /B /Y "0TCCWinAPI.html" "TCCWinAPI.html"
      That user tool can be run also from within an UltraEdit macro or an UltraEdit script which next (re)opens the copied HTML file and makes further changes like editing the value of the title element in head of the HTML file.

      Example for an UltraEdit script:

      Code: Select all

      if (UltraEdit.document.length > 0)           // Is any file opened?
      {
         if (UltraEdit.activeDocument.isExt("md")) // Has the active file the extension MD?
         {
            // Store file index of the active Markdown file in a variable.
            var nMarkDownFileIndex = UltraEdit.activeDocumentIdx;
            // Get the fully qualified file name of the active Markdown file.
            var sMarkDownFileName = UltraEdit.activeDocument.path;
            // Copy the HTML file created by UltraEdit for live preview.
            UltraEdit.runTool("Copy HTML");
            // Determine the fully qualified file name of the HTML file by
            // replacing the file extension md with the file extension html.
            var sHtmlFileName = sMarkDownFileName.substr(0,sMarkDownFileName.length-2) + "html";
            // Open the HTML file of active Markdown file.
            UltraEdit.open(sHtmlFileName);
            // Is the active file indeed the expected HTML file?
            if(UltraEdit.activeDocument.path == sHtmlFileName)
            {
               // Move the caret to top of the HTML file.
               UltraEdit.activeDocument.top();
               // Define the parameters for a case-sensitive UltraEdit regular expression replace.
               UltraEdit.ueReOn();
               UltraEdit.activeDocument.findReplace.mode=0;
               UltraEdit.activeDocument.findReplace.matchCase=true;
               UltraEdit.activeDocument.findReplace.matchWord=false;
               UltraEdit.activeDocument.findReplace.regExp=true;
               UltraEdit.activeDocument.findReplace.searchDown=true;
               UltraEdit.activeDocument.findReplace.searchInColumn=false;
               UltraEdit.activeDocument.findReplace.preserveCase=false;
               UltraEdit.activeDocument.findReplace.replaceAll=false;
               UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
               // Run the UltraEdit regular expression replace to change the title.
               UltraEdit.activeDocument.findReplace.replace("<title>*</title>","<title>Take Command Console Win API Functions</title>");
               // Save and close the HTML file.
               UltraEdit.closeFile(UltraEdit.activeDocument.path,1);
            }
            // Make the unmodified Markdown file again the active file.
            UltraEdit.document[nMarkDownFileIndex].setActive();
         }
      }
      
      The script checks if any file is opened at all and next if the active file has the file extension md to prevent running on no file opened or on a definitely wrong file. It is not possible to check if there is toggled on the live preview for the active Markdown file. That must be made sure by the user before running the script.

      The script next gets file index and fully qualified file name of active Markdown file and runs the configured user tool. Next it opens the HTML file copied by the user tool and verifies if that was really successful. Then it moves the caret to top of opened HTML file, defines the parameters for a once executed case-sensitive UltraEdit regular expression replace and runs the replace to change the title of the HTML file.

      Last the script saves and closes the HTML file and makes the Markdown file again the active file which is still not saved at all as not really necessary.

      The script can be extended to do whatever the user of the script likes to do on HTML file contents.
      Best regards from an UC/UE/UES for Windows user from Austria

      2
      NewbieNewbie
      2

        Mar 26, 2023#3

        Thankyou @Mofi for your detailed response.

        For now, I am going to use your solution for the temporary created HTML file, which works as described.

        I will also try your CSS files with a separate, external tool, and see how that compares.

        Regards,

        Joe Caverly