snosov1 / toc-org

toc-org is an Emacs utility to have an up-to-date table of contents in the org files without exporting (useful primarily for readme files on GitHub)
GNU General Public License v3.0
287 stars 29 forks source link

Custom toc-org-hrefify-STYLE based on ID #63

Closed csantosb closed 5 years ago

csantosb commented 5 years ago

Hi,

I try to develop a custom toc-org-hrefify-STYLE function, where I need to get the ID property of the heading. Is there a way to achieve that ? I mean, I only have access to the heading itself, not no any of its properties. Maybe extending str in

(str &optional hash)

to include the whole heading might be great. Otherwise, a third argument

(str &optional hash props)

with props obtained from org-entry-properties is also an option.

Alternatively, any way to access to the current org file name ? When using

(defun toc-org-hrefify-custom (str &optional hash)
"Given a heading, transform it into a href using the org-mode rules."
(format "%s" (buffer-file-name)))

I get nil. If I had access to it, I would parse its contents to get the heading ID.

Thanks for your help,

C.

snosov1 commented 5 years ago

Hey!

You, actually, got me on this one. I think the desire is legit, but there's no clean way to do this now.

As a workaround, it seems like you can get the buffer of the org file via (message (format "%s" (other-buffer (current-buffer) 1))).

For now, I'm not sure how a better solution should look like. I'll try to think about it =)

csantosb commented 5 years ago

You're right, not obvious. I've been looking at the code, I see what you mean.

Maybe replacing toc-org-raw-toc by an alist of headings and props, and then getting current behaviour from there, but still making the props accessible to the hrefify function ?

snosov1 commented 5 years ago

alist of headings and props

Well, my main concern is that I'm not sure properties are enough. Some other guy would decide that it's a good idea to have different hrefs based on the text inside the heading, which means another change to the API =)

snosov1 commented 5 years ago

I'm thinking that if I just provide access to the whole buffer - then the user can do whatever he wants. As one thing - use org API to get the tree and properties and cookies and whatnot.

csantosb commented 5 years ago

After a little thinking, I guess that giving the heading and the buffer name (not the whole buffer, think about very large files) should be enough for all purposes, as you say.

Then, what about duplicated headings ? One workaround would be providing the point, the hash and the buffer name. This works as the TOC gets updated only when you modify a heading, and not the text. From the point, one may get the heading, the properties and do any other fancy stuff I may think of.

snosov1 commented 5 years ago

I guess that giving the heading and the buffer name

That's more or less what you can do even now (with the other-buffer call I provided)

Then, what about duplicated headings ?

You can figure out what heading is meant exactly having the hash table (e.g. if you have a heading 'About' and there's already #about and #about-1 in the hash, then this 'About` is the third one). It's not very pretty, but can be done.

csantosb commented 5 years ago

Ok, thanks, I'll proceed this way then.