QOwnNotes icon indicating copy to clipboard operation
QOwnNotes copied to clipboard

Generate HTML identifier from section title

Open toindev opened this issue 7 years ago • 10 comments

Expected behaviour

I can link to a specific section in a exported HTML document using an identifier generated from its title.

eg. <h4 id="my-section">My section</h4> in generated html, in order to be able to use my_note.html#my-section to access "My section"

Actual behaviour

The identifier for sections exist, but are numerically incremented, and do not reflect the title of the section.

example: <h4 id="toc_6">My section</h4>

Possible implementation

This is how it's done in the insert-toc script, in order to work well with GitHub flavored Markdown:

    function extractLink(title) {
        var lowercase = title.toLowerCase()
        var spaceReplaced = lowercase.replace(/ /g, "-")
        var invalidCharsRemoved = spaceReplaced.replace(/[^A-Za-zÀ-ÿ-_]/g, "")
        return invalidCharsRemoved;
    }

toindev avatar Aug 20 '18 15:08 toindev

You can change the generated html with http://docs.qownnotes.org/en/develop/scripting/README.html#notetomarkdownhtmlhook. To set anchors as you like... Does this answer your question?

pbek avatar Aug 20 '18 17:08 pbek

It could, but it would not be very practical, since I would have to run regexp after regexp. I think it could be better suited for a patch to the HTML generator. Could you direct me to the code, unless it is from an external library ?

toindev avatar Aug 20 '18 22:08 toindev

It's a 3rd party library: https://github.com/pbek/QOwnNotes/tree/develop/src/libraries/hoedown But you can also play with markdown-it (see: the script repository).

pbek avatar Aug 21 '18 04:08 pbek

Ha, nice! markdown-it even has a plugin for header anchors already. I'll have a look at integrating it and close this issue, thank you.

toindev avatar Aug 21 '18 11:08 toindev

great!

pbek avatar Aug 21 '18 14:08 pbek

@toindev - Did you figure out how to use this for TOC-style links within a given note? I tried using markdown-it, but still didn't get quite what I was looking for.

thomascwells avatar Jun 27 '22 15:06 thomascwells

Oh, I see now (#1567) that the markdown engine(?) has been changed to https://github.com/mity/md4c .

Maybe this issue is now moot.

thomascwells avatar Jun 27 '22 15:06 thomascwells

Maybe this issue is now moot.

So can you link to sections? 🤔

pbek avatar Jun 28 '22 06:06 pbek

I actually couldn't quite figure out how to link to sections directly. I was expecting something like this to work:

# My Title

[Skip to conclusion](#conclusion)
[Skip to footnotes](#footnotes)

## Section 1

## Conclusion

### Footnotes

But I could only get it to work with the link formatted something like: [Skip to conclusion](my_file_name.md#conclusion)

I thought switching to MD4C would fix it, but alas, I couldn't make it work. I tired with the markdown-it script as well.

For now, I'm going to use VSCodium, so this isn't a pressing issue for me. (I'm often coding, so I will usually have VSCode open already anyway, and at least on my system (Fedora) the RAM footprint was about the same).

But I'm happy to continue engaging/working on this Issue if there's anything I can do. I appreciate all the work you've put into QOwnNotes and love to see tools like this continue to be successful - especially ones that integrate so well into the self-hosted ecosystem!

thomascwells avatar Jun 28 '22 16:06 thomascwells

[Skip to conclusion](my_file_name.md#conclusion)

yes, this is the way 😁

pbek avatar Jun 29 '22 07:06 pbek