Skip to content
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

Question: implementing link configuration #24

Open
captainhusaynpenguin opened this issue Apr 18, 2017 · 1 comment
Open

Question: implementing link configuration #24

captainhusaynpenguin opened this issue Apr 18, 2017 · 1 comment
Labels

Comments

@captainhusaynpenguin
Copy link

Thanks for the library, it's really helpful, one question though, and sorry that it's a beginner thing in JS, but how do you implement the link property correctly. Here is how my test looks like:

var    contentsSM,
    newHeading,
    contentsMD;

contentsSM = window.Contents({
	
		articles: document.querySelectorAll('article h2, article h2'),
		link : function (guide, article) {
	        var guideLink,
	            articleName,
	            articleId;
	
	        guide = $(guide);
	        article = $(article);
	
	        guideLink = $('<a>');
	        articleName = article.text();
	        articleId = 'etc';
	
	        guideLink
	            .text(articleName)
	            .attr('href', '#' + 'etc')
	            .prependTo(guide);
	
	        article
	            .attr('id', 'etc');
			}
	    
    });

document.querySelector('#table-of-content').appendChild(contentsSM.list());

Actually what I'm trying to achieve is to have <a href="#1"> and <a href="#2"> and etc, but however I tried to manipulate the articleId, it doesn't work, even something like this:

contentsSM = window.Contents({
		articles: document.querySelectorAll('article h2, article h2'),
    });

contentsSM.articleId = function(articleName, element) {
    return 'how are you';
};

generates:

<h2 id="But can why-questions ever get answered?"><a href="#But can why-questions ever get answered?">But can <em>why-questions </em>ever get answered?</a></h2>

Thanks in advance ...

PS. I setup the contents by npm and require it with window.contents = require('contents)'.

@gajus gajus added the question label Apr 18, 2017
@captainhusaynpenguin
Copy link
Author

Here is what I end up doing:

var articleNames = window.jQuery('article.journal-text > h2').map(function() {
                 return $(this).text();
              }).get();
contentsSM = window.Contents({
        articles: document.querySelectorAll('article h2'),
		link: function (guide, article) {
			let articleLink,
		        guideLink,
		        id;
		    guideLink = document.createElement('a');
		    articleLink = document.createElement('a');
			id = articleNames.indexOf(article.name);
		    articleLink.href = `#${id}`;
			articleLink.setAttribute("name", id);
		    article.element.appendChild(articleLink);
		    guideLink.appendChild(document.createTextNode(article.name));
		    guideLink.href = `#${id}`;
		
		    guide.insertBefore(guideLink, guide.firstChild);
		}
    });
document.querySelector('#table-of-content').appendChild(contentsSM.list());

I personally preferred adding name to a instead of letting them being handled by JS.

PS. I still use the default performance too, so I generates to table of contents.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants