Emacs mode for Stack Overflow's markdown

EmacsMarkdownOrg Mode

Emacs Problem Overview


I am using Org-mode in Emacs to handle all my technical documentation. I would like to use Emacs to prepare questions that I have for Stack Overflow. Is there an Emacs mode for that, or even better, an extension for Org-mode to handle Stack Overflow formatting? Ideally it should include all formatting options supported by the markdown syntax used in the Stack Overflow question/comment buffers.

Emacs Solutions


Solution 1 - Emacs

Integrating Emacs with Stack Overflow

As mentioned you can use markdown-mode. To integrate markdown-mode with Stack Overflow you can use the Firefox plugin It's All Text which lets you edit textareas with an external editor. Here is how to set it up:

  1. Install markdown-mode. If you use Debian or Ubuntu you can install it by issuing

     sudo apt-get install emacs-goodies-el
    

or if you're on emacs 24 (or have package.el on emacs 23) and Marmalade or Melpa you can install it with

    M-x package-install RET markdown-mode
  1. Install It's All Text.

  2. Set It's All Text's preferences to use Emacs. Either you can set it to the executable (e.g. /usr/bin/emacs) or the emacsclient.

  3. Add the following to your .emacs to enable markdown-mode for Stack Overflow and Stack Exchange textareas:

     ;; Integrate Emacs with Stack Exchange https://stackoverflow.com/a/10386560/789593
     (add-to-list 'auto-mode-alist '("stack\\(exchange\\|overflow\\)\\.com\\.[a-z0-9]+\\.txt" . markdown-mode))
    

Alternatively, if as-external-alist is defined—if M-x describe-variable RET as-external-alist doesn't fail—it will probably override your auto-mode-alist. It has a slightly different format (it's a list of pairs instead of a list of cons cells) so this will work:

  <!-- language: lang-lisp -->

    (add-to-list 'as-external-alist '("stack\\(exchange\\|overflow\\)\\.com\\.[a-z0-9]+\\.txt" markdown-mode))

5. Press the blue edit button at the bottom right side of a textarea to edit it via emacs. The blue edit button is shown in the following screenshot:

Screenshot of the blue edit button at the bottom right side of this textarea

In the following screenshot is an Emacs buffer in markdown-mode editing this post:

Screenshot of an Emacs buffer in markdown-mode editing this post

  1. When you are done editing in Emacs save the buffer to send it to Firefox.

If you want this functionality for other domains you need to change the regexp above. The following recognizes Stack Exchange, Stack Overflow, Ask Ubuntu and Super User:

;; Integrate Emacs with Stack Exchange https://stackoverflow.com/a/10386560/789593
(add-to-list 'auto-mode-alist '("\\(stack\\(exchange\\|overflow\\)\\|superuser\\|askubuntu\\)\\.com\\.[a-z0-9]+\\.txt" . markdown-mode))

Using markdown-mode with Org-mode

To use markdow-mode with Org-mode you can use its feature for working with source code. With it you can include blocks of markdown inside your Org-mode buffers which you can edit via markdown-mode. You can use it as follows:

  1. When in an Org-mode buffer enter <s on a newline and press Tab. This will result in

     #+begin_src 
    
     #+end_src
    
  2. Enter markdown after #+begin_src so that you have

     #+begin_src markdown
    
     #+end_src
    
  3. When inside the source block (between #+begin_src markdown and #+end_src) press C-c ' to edit the source block with markdown-mode.

  4. Edit the source block in markdown-mode.

  5. Press C-c ' to return to the Org-mode buffer and insert the edit. This can look like what the following screenshot shows:

An Org-mode buffer editing a block of markdown code for this post

Solution 2 - Emacs

There is markdown-mode: http://jblevins.org/projects/markdown-mode/

See http://emacswiki.org/emacs/MarkdownMode for some customization.

And there is a markdown backend for the new export engine here: http://orgmode.org/w/?p=org-mode.git;a=blob_plain;f=contrib/lisp/org-md.el;hb=HEAD

Make sure you add the contrib/lisp/ directory to your load-path.

Then (require 'org-export) and (require 'org-md).

M-x org-md-export-to-markdown RET will export to markdown.

Solution 3 - Emacs

@N.N's answer is applicable to Chrome also, with these changes in details.

Chrome has Edit with Emacs extension, which is similar to It's all text. After installing it, you'll find further instructions from the extension options page.

There is an emacs customization group edit-server with options, most importantly the major mode change per site is configured differently than in @N.N's answer:

(add-to-list 'edit-server-url-major-mode-alist
             '("^stackoverflow" . markdown-mode))

Also, for the edit-server to work in terminal emacs, I had to disable the edit-server-new-frame option.

Solution 4 - Emacs

Pandoc has support for reading a subset of org-mode and can output markdown.

In other words, you can keep writing in org-mode, including writing italics /like this/, and then export to markdown. From emacs you can convert to markdown by selecting the region, hitting C-u M-S-\ and typing pandoc -r org -t markdown, getting output like this:

In other words, you can keep writing in org-mode, including writing italics *like this*, and then export to markdown.

Or, you can save the file and convert it at the command line.

Solution 5 - Emacs

It Is Easy With Chrome Too

This is less an answer than a test of my solution using the Edit with Emacs chrome plugin and the Emacs Edit Server configured to use markdown-mode for Stackoverflow (and other Stackexchange sites I use). All I had to do after installing the extension and MELPA packages was set

(setq edit-server-url-major-mode-alist
             '(("mail.google.com" . org-mode)
               ("stackoverflow\\.com" . markdown-mode)
               (".*\\.stackexchange\\.com" . markdown-mode)
               ("github\\.com" . markdown-mode)))
               
Gmail Message Support

Note in the above setting, I'm using org-mode for gmail messages. Still need to do a little work here, but it is possible, with a little effort, to draft your gmail message using org-mode and then use org-mime to generate html from your org structured text. You could just use markdown and a markdown to html conversion.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionblueFastView Question on Stackoverflow
Solution 1 - EmacsN.N.View Answer on Stackoverflow
Solution 2 - EmacsbzgView Answer on Stackoverflow
Solution 3 - EmacsristohietalView Answer on Stackoverflow
Solution 4 - EmacsSandraView Answer on Stackoverflow
Solution 5 - EmacsTim XView Answer on Stackoverflow