ardumont / markdown-toc

Generate a TOC in markdown file
GNU General Public License v3.0
128 stars 100 forks source link

Generate TOC does not respect heading levels. #53

Open TheAifam5 opened 2 years ago

TheAifam5 commented 2 years ago

bug

Generated TOC does not respect heading levels and every TOC entry is on the same level.

Emacs Doom: master Emacs Version: 28.1 with native-compilation

markdown-toc - Please:
- Describe your problem with clarity and conciceness (cf. https://www.gnu.org/software/emacs/manual/html_node/emacs/Understanding-Bug-Reporting.html)
- Explicit your installation choice (melpa, marmalade, el-get, tarball, git clone...).
- Report the following message trace inside your issue.

System information:
- system-type: gnu/linux
- locale-coding-system: utf-8-unix
- emacs-version: GNU Emacs 28.1 (build 2, x86_64-pc-linux-gnu, X toolkit, cairo version 1.16.0)
 of 2022-05-19
- markdown-mode path: /home/theaifam5/.config/emacs/.local/straight/build-28.1/markdown-mode/markdown-mode.el
- markdown-toc version: 0.1.5
- markdown-toc path: /home/theaifam5/.config/emacs/.local/straight/build-28.1/markdown-toc/markdown-toc.el

Config

With and without it, the issue still occurs.

(custom-set-variables '(markdown-toc-user-toc-structure-manipulation-fn 'cdr))

Expected behavior

<!-- markdown-toc start - Don't edit this section. Run M-x markdown-toc-refresh-toc -->
**Table of Contents**

- [Example 1](#example-1)
  - [Content](#content)
- [Example 2](#example-2)
  - [Content](#content-1)

<!-- markdown-toc end -->

Actual behavior

<!-- markdown-toc start - Don't edit this section. Run M-x markdown-toc-refresh-toc -->
**Table of Contents**

- [Example 1](#example-1)
- [Content](#content)
- [Example 2](#example-2)
- [Content](#content-1)

<!-- markdown-toc end -->

Steps to reproduce the behavior

Title
=====

# Example 1
aa

## Content
xx

# Example 2
yy

## Content 
dd
dominicm00 commented 2 years ago

To make a nested TOC, the buffer-local imenu-create-index-function has to be set to markdown-imenu-create-nested-index instead of markdown-imenu-create-flat-index.

goetzc commented 2 years ago

Did this chance with a new recent Emacs version?

dominicm00 commented 2 years ago

Did this chance with a new recent Emacs version?

It did; maybe a default value was changed? Although this would be a change in markdown-mode rather than Emacs itself right?

andorsk commented 1 year ago

I tried setting it like this: (custom-set-variables '(imenu-create-index-function markdown-imenu-create-nested-index) but it didn't work. any advice here?

sochotnicky commented 1 year ago

I tried setting it like this: (custom-set-variables '(imenu-create-index-function markdown-imenu-create-nested-index) but it didn't work. any advice here?

It took a while digging around and this seems related to native compilation as far as I can tell. I have a workaround in my config here: https://codeberg.org/sochotnicky/dotfiles/src/branch/main/dot_doom.d/config.org#headline-64

Basically - disable native compilation for markdown-mode & add a hook to enable nested TOC generation. Might help you as well?

andorsk commented 1 year ago

Good idea, but didn't work unfortunately. This is a bummer, as this is a super useful feature for me.

sochotnicky commented 1 year ago

I tried setting it like this: (custom-set-variables '(imenu-create-index-function markdown-imenu-create-nested-index) but it didn't work. any advice here?

To be clear - you need to delete any already natively-compiled markdown-mode files or that first part won't be effective. Just make sure you do that...

andorsk commented 1 year ago

I had to restart my emacs, but it worked! Nice callout @sochotnicky

xiliuya commented 1 year ago

Hi, replace this funtion will fix it.

(defun markdown-toc--compute-toc-structure (imenu-index)
  "Given a IMENU-INDEX, compute the TOC structure."
  (--mapcat (markdown-toc--compute-toc-structure-from-level
             (if (eq markdown-toc-user-toc-structure-manipulation-fn 'cdr)
                 -1
               0)
             it)
            imenu-index))