-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add attributes to the TOC list #58
Comments
@nicofranzp Could you clarify which attributes you'd want to set to a list besides the |
I thought it might be useful to have access to style and id |
🤔 hmm not sure about that. Lists are created recursively and I think |
What I ended up using is the following: function tocItemToHtml(tocItem, options, md, counter = 0) {
let extraAttributes = ''
if (counter == 0){
if (options.listAttrs !== '') { extraAttributes = ' ' + options.listAttrs }
}
return '<' + options.listType + extraAttributes + '>' + tocItem.children.map(childItem => {
let li = '<li>';
let anchor = childItem.anchor;
if (options && options.transformLink) {
anchor = options.transformLink(anchor);
}
let text = childItem.text ? options.format(childItem.text, md, anchor) : null;
li += anchor ? `<a href="#${anchor}">${text}</a>` : (text || '');
return li + (childItem.children.length > 0 ? tocItemToHtml(childItem, options, md, counter + 1) : '') + '</li>';
}).join('') + '</' + options.listType + '>';
} Which avoids adding the same attributes into sub-lists (and the unnecessary white space). Maybe this could be generalized to sublists, but there I understand the concern that the I had to get rid of the container since the people which I send the final HTML set that constraint. I don't know if that's to specific to be honest, haha |
That'd work, but before we talk about the actual implementation, please share more details about the scenario you wanna achieve. What is the exact HTML output you desire? That would be your test case to test against. Also, do you use |
I use it in a static context (I use gulp to combine everything). The built HTML has to have a table of contents without container (which I ditch using <ul id="TOC" class="some clases" style="some styles">
<li><a href="#S1">Section 1</a></li>
<li><a href="#S2">Section 2</a></li>
...
</ul> |
Hi I was wondering if it is possible to add an option to add attributes to the list in the TOC
i.e. add a default
listAttrs: ''
--- around line 28 ---, and replace current line 181 forreturn '<' + options.listType + ' ' + options.listAttrs +'>' + tocItem.children.map(childItem => {
or something similarThe text was updated successfully, but these errors were encountered: