.emacs.d / files / markdown.el

(defun -repeat (n x)
  "Return a list with X repeated N times.
Returns nil if N is less than 1."
  (let (ret)
    (--dotimes n (!cons x ret))
    ret))

(defun my-markdown-toc ()
  "Generate table of content from # to ####### headers."
  (interactive)
  (let ((n 'nil)
        (last-m nil)
        (toc ""))
    (save-excursion
      (while (re-search-forward "^\\(#+\\) \\(.*\\)" nil t)
        (if (equal last-m (match-string 1))
            (progn
              (setcar n (1+ (car n))))
          (if (< (length last-m) (length (match-string 1)))
              (!cons 1 n)
            (!cdr n)
            (setcar n (1+ (car n))))
          (setq last-m (match-string 1)))
        (setq toc (concat
                   toc
                   (apply #'concat (-repeat (* 4 (1- (length (match-string 1)))) " "))
                   (int-to-string (car n))
                   ". ["
                   (match-string 2)
                   "]"
                   "(#"
                   (replace-regexp-in-string " " "-" (downcase (match-string 2)))
                   ")
"
                   ))))
    (insert toc)))

(defun my-markdown-generate-anchors ()
  "Add anchors above each header.  If an anchor is present,
delete it and re-insert new one."
  (interactive)
  (let (m)
    (while (re-search-forward "^\\(#+\\) \\(.*\\)" nil t)
      (setq m (match-string 2))
      (beginning-of-line)
      (previous-line)
      (if (looking-at "<a")
          (delete-region (point) (line-end-position))
        (newline))
      (insert (concat
               "<a name=\""
               (replace-regexp-in-string " " "-" (downcase m))
               "\" />"))
      (next-line 2))))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.