LoginSignup
4
4

More than 5 years have passed since last update.

EmacsでMarkdownの目次(アウトライン)をプレヴュー

Last updated at Posted at 2018-02-10

概要

EmacsでMarkdownを書いている時、アウトラインを確認したいときがあります。
なので、Emacs内で今書いているMarkdownの目次を別ウインドウで見れるようにしてみました。

準備

Markdownから目次を生成してくれるスクリプトのgh-md-tocを使います。
依存するソフトはないのでPandocとか入れなくてもOK!

git clone https://github.com/ekalinin/github-markdown-toc.git
chmod a+x gh-md-toc

もともとMarkdownに目次をinsertするためのソフトなので今回の目的には不要なリンク出力を削除します。

- print sprintf("%*s", substr($0, length($0), 1)*3, " ") "* [" substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)"](" gh_url substr($0, match($0, "href=\"[^\"]+?\" ")+6, RLENGTH-8) ")"}' | sed 'y/+/ /; s/%/\\x/g')"

+ print sprintf("%*s", substr($0, length($0), 1)*3, " ") "* [" substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)"]" }' | sed 'y/+/ /; s/%/\\x/g')"

どこかパスの通ったところにおいて下さい。

Emacsの設定

initファイルに以下を記載します。

markdown.el
(defun markdown-toc ()
 (interactive )
 (let ((fName (buffer-file-name)))
   (get-buffer-create "*markdown-toc*")
   (set-buffer "*markdown-toc*")
   (erase-buffer)
   (display-buffer (current-buffer))
   (call-process-shell-command (concat "PATH/gh-md-toc " ; <- パスを指定する
                                    fName) nil t)))
(define-key markdown-mode-map (kbd "\C-c\C-t") 'markdown-toc) ; Keyはお好みで。

Markdownを編集している時にC-cC-tで目次が表示されます。
Screenshot from 2018-02-10 20-32-52.png

おまけ(pop-win.el)

目次は右側に出て欲しいので、popwin.elを使って指定してます。

(require 'popwin)
(popwin-mode 1)
(push '("*markdown-toc*" :width 0.2 :position right)
          popwin:special-display-config)
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4