50

Is it possible to present Markdown rendered in an Emacs buffer using Emacs' own buffer text formatting capabilities? Emacs in graphical environments has rich text presentation capabilities (font styles, colors, links and even images) so it should be quite possible. Are there any existing implementations?

Note that the idea is to have the rendered Markdown be native Emacs formatted text that can be navigated and operated on as any other text in Emacs. Therefore solutions that render to an image that is embedded in an Emacs buffer are not desirable here.

Also note that this is not about a mode for editing Markdown, but for presenting rendered Markdown in an Emacs buffer. It should preferably be a pure Emacs Lisp solution for portability.

Markus Miller
  • 3,695
  • 2
  • 29
  • 33

8 Answers8

22

Personally, I use the following workflow:

  • run on a C-c C-c m to run Mark­down on the cur­rent buffer and pre­view the out­put in an­other buffer.
  • move to html-mode on this other buffer (M-x html-mode)
  • hide the html tags to display something close to the output (M-x sgml-tags-invisible)

Then every time you want to refresh the rendering, simply run again C-c C-c m on the markdown buffer.

Yet I confess until now for Markdown editing/previewing, nothing beats for me Textmate and its markdown preview panel. Actually, from a personal perspective, the only case where I prefer to run Textmate rather than Emacs is when I want to edit markdown files. Yet the path to have the same quality of preview on emacs is not so difficult and probably I should investigate it. As I see it, it's simply:

  • get the internal css used by Textmate for rendering the preview
  • use w3 or w3m to preview the markdown output using this css
serv-inc
  • 35,772
  • 9
  • 166
  • 188
Sebastien Varrette
  • 3,876
  • 1
  • 24
  • 21
  • Thanks, love how you combined all these functionalities :) – Ev Dolzhenko Jun 12 '13 at 10:35
  • 4
    Great setup, but for some reason I'm unable to refresh. When I run `C-c C-c m` on the rendered buffer I get `C-c C-c m is undefined`, and when I run it on the source buffer I get `Text is read-only`. How can I get around this? – Matthew Piziak May 18 '14 at 19:10
  • Are you in `markdown-mode`? Perhaps you can make sure that your `markdown-mode` is installed and up to date. You can also run `C-h k C-c C-c m` to check whether `C-c C-c m` is bound to the function `markdown-other-window`. You can also just run the `markdown-other-window` function directly with `M-x markdown-other-window`. – modulitos Oct 05 '15 at 00:14
  • I got Text is read-only due to there was already a read only buffer (occupied by another markdonw output in my case) . The complain disapperay after deleting the buffer. – user3113626 Dec 30 '18 at 05:00
7

I have this in my .emacs file:

(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(markdown-header-delimiter-face ((t (:inherit font-lock-function-name-face :underline t :weight bold))) t)
 '(markdown-header-face-1 ((t (:inherit markdown-header-face :height 1.5))) t)
 '(markdown-header-face-2 ((t (:inherit markdown-header-face :height 1.3))) t)
 '(markdown-header-face-3 ((t (:inherit markdown-header-face :underline t :height 1.2))) t)
 '(markdown-header-face-4 ((t (:inherit markdown-header-face :underline t :height 1.1))) t)
 '(markdown-header-face-5 ((t (:inherit markdown-header-face :underline t))) t)
 '(markdown-header-face-6 ((t (:inherit markdown-header-face :underline t))) t))
(put 'set-goal-column 'disabled nil)

which enlarges all the headlines. Markdown-mode itself will italicize starred text and boldface double-starred text, so this will get you there. However, it will not make the control characters invisible.

If you want that, you should probably look into pretty-lambda for examples (as Bozhidar Batsov suggested)

Community
  • 1
  • 1
bastibe
  • 16,551
  • 28
  • 95
  • 126
7

I guess you can use the source code of latex-preview for inspiration or pretty-lambda(a much simpler piece of software).

Alternatively you can convert the markdown to html in a background process and preview the html.

Everything is possible in Emacs, but not everything is easily achieved :-)

Bozhidar Batsov
  • 55,802
  • 13
  • 100
  • 117
  • 1
    Thanks for mentioning latex-preview. I would however not like to render the Markdown as an image embedded in the Emacs buffer, but as native Emacs formatted text that can be navigated and operated on as any other text. Maybe something could be learned from pretty-lambda though. – Markus Miller Aug 11 '10 at 10:53
5

Depending on the context where you'd like to see the rendered text, you might be able to get pretty close to what you need by just tweaking markdown-mode's font-lock rules.

Stefan
  • 27,908
  • 4
  • 53
  • 82
  • Sounds like a pragmatic approach with small effort (just updating font-lock rules) and it doesn't depend on any external tool or process which is good. – Markus Miller Mar 29 '12 at 11:20
4

Apart from pretty-lambda (which has already been suggested multiple times), you could look into org-mode and its syntax highliting rules. In particular, the org-hide-emphasis-markers variable does more or less what you want (see e.g. how org-do-emphasis-faces hide markup characters).

François Févotte
  • 19,520
  • 4
  • 51
  • 74
3

As I don't know about any emacs-based Markdown parser, I'd have to say that you have to code one from scratch. Maybe this SO question can throw some pointers at you.

If you opt to go through the Emacs-only road, then Semantic is an excellent API for that job (it offers you a lexer, parser-generator, and parser; it's been around for more than a decade, and it has documentation!). After having the language parser, you'll have to make some rendering functions for each token type. And this way you can customize everything.

Although this would be an enlightening trip, for sure, I'd still opt for using an existing Markdown->html converter on a separate background process and then use w3(m) for emacs preview (as Bozhidar suggested). It gets the job done and it's much simpler to do. No major performance issues, neither - you should run this tool rather scarcely, so you can easily spare some extra milliseconds).

A mixed solution would be to make the Markdown parser generate HTML directly, and preview it on a w3(m) buffer (it takes away the rendering weight from your shoulders, you only need to transliterate the markdown into html, and that seems pretty straight forward with Semantic).

Community
  • 1
  • 1
Edgar Gonçalves
  • 378
  • 1
  • 10
1

If it is only about the rendering, go with Bozhidar's suggestion and do a Markdown to HTML conversion, then display the HTML in a W3 buffer. markdown-mode has code to call the external Markdown command with a few goodies.

But if you really want to do everything within Emacs Lisp, you'll have to write a Markdown parser first.

Thomas Kappler
  • 3,795
  • 1
  • 22
  • 21
  • 1
    Thank you, but I really want to have it all in Emacs Lisp for portability. Therefore I'm hoping to find an existing Emacs Lisp Markdown parser. – Markus Miller Aug 05 '10 at 16:49
0

Me too, I've been looking for something like this for a very long time. The best I could find, though, is not am Emacs solution, it is an independent great piece of software called ReText.

Bruno Reis
  • 37,201
  • 11
  • 119
  • 156