emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/toc-org 7291a3b08d 100/128: Enable link following in markd


From: ELPA Syncer
Subject: [nongnu] elpa/toc-org 7291a3b08d 100/128: Enable link following in markdown mode
Date: Sun, 2 Jan 2022 09:59:13 -0500 (EST)

branch: elpa/toc-org
commit 7291a3b08d84d71879f16b6ce618503f3a2f50ec
Author: Sergei Nosov <sergei.nosov@gmail.com>
Commit: Sergei Nosov <sergei.nosov@gmail.com>

    Enable link following in markdown mode
---
 README.org | 40 +++++++++++++++++++++++++++++++++++++++-
 toc-org.el | 30 ++++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+), 1 deletion(-)

diff --git a/README.org b/README.org
index fb302c305d..a175ae989c 100644
--- a/README.org
+++ b/README.org
@@ -5,7 +5,8 @@
 toc-org helps you to have an up-to-date table of contents in org files without
 exporting (useful primarily for readme files on GitHub).
 
-It is similar to the 
[[https://github.com/ardumont/markdown-toc][markdown-toc]] package, but works 
for org files.
+It is similar to the 
[[https://github.com/ardumont/markdown-toc][markdown-toc]] package, but works 
for org files. Since
+recently, it, actually, works in [[Markdown support][markdown]], too!
 
 *NOTE:* Previous name of the package is =org-toc=. It was changed because of a
 name conflict with one of the org contrib modules.
@@ -21,6 +22,7 @@ name conflict with one of the org contrib modules.
   - [[#exclude-headings][Exclude headings]]
   - [[#quote-table-of-contents][Quote table of contents]]
   - [[#shortcut-for-toc-tag][Shortcut for TOC tag]]
+- [[#markdown-support][Markdown support]]
 - [[#different-href-styles][Different href styles]]
 - [[#example][Example]]
 #+END_QUOTE
@@ -37,6 +39,10 @@ repository and then put the following snippet in your 
~/.emacs file
 #+BEGIN_SRC elisp
   (if (require 'toc-org nil t)
       (add-hook 'org-mode-hook 'toc-org-mode)
+
+      ;; enable in markdown, too
+      (add-hook 'markdown-mode-hook 'toc-org-mode)
+      (define-key markdown-mode-map (kbd "\C-c\C-o") 
'toc-org-markdown-follow-thing-at-point))
     (warn "toc-org not found"))
 #+END_SRC
 
@@ -52,6 +58,10 @@ repository and then put the following snippet in your 
~/.emacs file
     (add-to-list 'load-path "~/.emacs.d/toc-org")
     (if (require 'toc-org nil t)
         (add-hook 'org-mode-hook 'toc-org-mode)
+
+        ;; enable in markdown, too
+        (add-hook 'markdown-mode-hook 'toc-org-mode)
+        (define-key markdown-mode-map (kbd "\C-c\C-o") 
'toc-org-markdown-follow-thing-at-point))
       (warn "toc-org not found"))
   #+END_SRC
 
@@ -112,6 +122,34 @@ In your emacs' setup, you can bind a tag =:TOC:= to a 
binding =T=:
 
 Now =C-c C-q T RET= and you are done putting the =:TOC:= entry.
 
+* Markdown support
+
+You can also enable the mode in Markdown files and get pretty much the same
+functionality. The package will
+1. Look for '#'s instead of '*'s as heading markers (so, markdown syntax with
+   trailing pounds, like, =## Heading 2 ##= is not supported).
+2. Expect the =:TOC:= tag to appear as comment, like, =<-- :TOC: -->=
+3. Format the links and the quote block according to Markdown syntax
+
+Example:
+
+#+BEGIN_SRC markdown
+  # About
+  # Table of Contents                                    <-- :TOC: -->
+  - [About](#about)
+  - [Installation](#installation)
+    - [via package.el](#via-packageel)
+    - [Manual](#manual)
+  - [Use](#use)
+  - [Example](#example)
+
+  # Installation
+  ## via package.el
+  ## Manual
+  # Use
+  # Example
+#+END_SRC
+
 * Different href styles
 
 Currently, only 2 href styles are supported: =gh= and =org=. You can easily
diff --git a/toc-org.el b/toc-org.el
index 03d69e209b..ef84811875 100644
--- a/toc-org.el
+++ b/toc-org.el
@@ -80,6 +80,17 @@ files on GitHub)"
 equal to `org-drawer-regexp'. However, some older versions of
 org (notably, 8.2.10) restrict the values that can be placed
 between the colons. So, the value here is set explicitly.")
+(defconst toc-org-markdown-link-regexp ;; copy-paste from markdown-mode
+  
"\\(!\\)?\\(\\[\\)\\([^]^][^]]*\\|\\)\\(\\]\\)\\((\\)\\([^)]*?\\)\\(?:\\s-+\\(\"[^\"]*\"\\)\\)?\\()\\)"
+  "Regular expression for a [text](file) or an image link ![text](file).
+Group 1 matches the leading exclamation point (optional).
+Group 2 matches the opening square bracket.
+Group 3 matches the text inside the square brackets.
+Group 4 matches the closing square bracket.
+Group 5 matches the opening parenthesis.
+Group 6 matches the URL.
+Group 7 matches the title (optional).
+Group 8 matches the closing parenthesis.")
 
 (defcustom toc-org-max-depth 2
   "Maximum depth of the headings to use in the table of
@@ -428,6 +439,25 @@ not :noexport_#:."
                       (insert new-toc)))))
             (message (concat "Hrefify function " hrefify-string " is not 
found"))))))))
 
+(defun toc-org-follow-markdown-link ()
+  "Follow the markdown link (mimics `org-open-at-point')"
+  (interactive)
+  (when (thing-at-point-looking-at toc-org-markdown-link-regexp)
+    (let ((pos (point)))
+      (goto-char (point-min))
+      (if (re-search-forward (concat "^#+\s+" (match-string-no-properties 3)) 
(point-max) t)
+          (beginning-of-line)
+        (goto-char pos)))))
+
+(defun toc-org-markdown-follow-thing-at-point (arg)
+  "Try to follow the link with `toc-org-follow-markdown-link',
+fallback to `markdown-follow-thing-at-point' on failure"
+  (interactive "P")
+  (let ((pos (point)))
+    (toc-org-follow-markdown-link)
+    (when (equal pos (point))
+      (markdown-follow-thing-at-point arg))))
+
 ;;;###autoload
 (defun toc-org-enable ()
   "Enable toc-org in this buffer."



reply via email to

[Prev in Thread] Current Thread [Next in Thread]