0

I'm working with Emacs in terminal mode (emacs -nw) from inside Windows cmd and have exported an Org file to HTML. Now I want to view the resulting HTML file. Here are my questions:

  1. How can I open the exported HTML file using a command like cmd start myfile.html from within Emacs?

  2. Does Emacs have a built-in HTML viewer similar to Visual Studio Code in its GUI mode? (I'm currently using CLI/TUI but am open to switching to GUI if it offers this feature)

  3. Are there any options for instant preview of the HTML file, either inside Emacs or in an external browser like Microsoft Edge?

Current workflow:

  1. Open Emacs in terminal: emacs -nw
  2. Create a new file: C-x C-f (name it with .org extension)
  3. Save the file: C-x C-s
  4. Export to HTML: C-c C-e then h h

I'm looking for the next steps to efficiently view and potentially live-preview the exported HTML.

5
  • 1
    Try C-c C-e h o to export to html and open the file, by default browser. Commented Sep 30, 2024 at 13:21
  • 1
    See HTML Export Commands in the manual. You can read the manual in Emacs with C-h i g(org) - also learn about the i command which searches the index of the manual: type i html export e.g. to see that section. That's the best way to learn about Emacs. Commented Sep 30, 2024 at 13:43
  • @NickD I kinda solved the problem. I have created this repo. Would be great if you could be kind to take a look and let me know if I am doing it the right way or I am off! Commented Sep 30, 2024 at 13:57
  • 1
    There is no "right" way: if it works for you, it's "right". FWIW, a quick scan did not trigger any alarms in my mind, but I didn't try it out to see whether it "works". Commented Sep 30, 2024 at 14:05
  • @NickD thanks a again. would love to get more feedback, if you ever get to try it. Commented Sep 30, 2024 at 14:10

1 Answer 1

1

Two functions come to mind for viewing HTML:

  1. eww - The Emacs Web Wowser, a text-based browser within Emacs
  2. browse-url (and friends such as browse-url-firefox) - to open an external web browser

Typically actions you want to do after a library call are handled by hooks. However, these need to be programmed by the library. As far as I know, there isn't a hook for after an Org export. So, you'll need to wrap the call.

Here's an example of that I had laying around:

(defun my-preview (&optional arg option)
  "Render current Org buffer and preview result.

Options are: (h)tml, (t)ext, and (m)arkdown.

Calling with a prefix (e.g. `C-u M-x my-preview') disables
preview."
  (interactive "p")
  (require 'ox-md)
  (let* ((option (or option (car (read-multiple-choice "Output format:"
                                            '((?h "html")
                                              (?t "text")
                                              (?m "markdown"))))))
         (current-file (buffer-file-name))
         output-file)
    (cond ((= option ?h)
           (setq output-file (concat (file-name-base current-file) ".html"))
           (with-temp-buffer
             (insert-file-contents current-file)
             (org-export-to-file 'html (concat temporary-file-directory output-file))))
          ((= option ?t)
           (setq output-file (concat (file-name-base current-file) ".txt"))
           (with-temp-buffer
             (insert-file-contents current-file)
             (org-export-to-file 'ascii (concat temporary-file-directory output-file))))
          ((= option ?m)
           (setq output-file (concat (file-name-base current-file) ".md"))
           (with-temp-buffer
             (insert-file-contents current-file)

             ;; ox-md outputs to the current directory
             (cd temporary-file-directory)
             (org-export-to-file 'md output-file))))
    (if (= arg 1)
        ;; TODO use appropriate `browse-url' function or `eww'
        (browse-url-default-windows-browser (concat temporary-file-directory output-file)))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.