| 
									
										
										
										
											2017-11-15 03:01:00 +02:00
										 |  |  | const has = require('hast-util-has-property'); | 
					
						
							|  |  |  | const is = require('hast-util-is-element'); | 
					
						
							| 
									
										
										
										
											2017-04-27 14:04:51 +01:00
										 |  |  | const slug = require('rehype-slug'); | 
					
						
							| 
									
										
										
										
											2017-11-15 03:01:00 +02:00
										 |  |  | const visit = require('unist-util-visit'); | 
					
						
							| 
									
										
										
										
											2017-04-27 14:04:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2017-11-15 03:01:00 +02:00
										 |  |  |  * Get remark to add IDs to headings and inject anchors into them. | 
					
						
							|  |  |  |  * This is a stripped-down equivalent of [rehype-autolink-headings](https://github.com/wooorm/rehype-autolink-headings)
 | 
					
						
							|  |  |  |  * that supports ignoring headings with the `no-anchor` class. | 
					
						
							| 
									
										
										
										
											2017-04-27 14:04:51 +01:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-11-15 03:01:00 +02:00
										 |  |  | const HEADINGS = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']; | 
					
						
							|  |  |  | const NO_ANCHOR_CLASS = 'no-anchor'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const clone = obj => JSON.parse(JSON.stringify(obj)); | 
					
						
							|  |  |  | const hasClass = (node, cls) => { | 
					
						
							|  |  |  |   const className = node.properties.className; | 
					
						
							|  |  |  |   return className && className.includes(cls); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const link = options => | 
					
						
							|  |  |  |   tree => visit(tree, node => { | 
					
						
							|  |  |  |     if (is(node, HEADINGS) && has(node, 'id') && !hasClass(node, NO_ANCHOR_CLASS)) { | 
					
						
							|  |  |  |       node.children.unshift({ | 
					
						
							|  |  |  |         type: 'element', | 
					
						
							|  |  |  |         tagName: 'a', | 
					
						
							|  |  |  |         properties: Object.assign(clone(options.properties), {href: `#${node.properties.id}`}), | 
					
						
							|  |  |  |         children: clone(options.content) | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 14:04:51 +01:00
										 |  |  | module.exports = [ | 
					
						
							|  |  |  |   slug, | 
					
						
							|  |  |  |   [link, { | 
					
						
							|  |  |  |     properties: { | 
					
						
							|  |  |  |       title: 'Link to this heading', | 
					
						
							|  |  |  |       className: ['header-link'], | 
					
						
							|  |  |  |       'aria-hidden': 'true' | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2017-11-15 03:01:00 +02:00
										 |  |  |     content: [ | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         type: 'element', | 
					
						
							|  |  |  |         tagName: 'i', | 
					
						
							|  |  |  |         properties: {className: ['material-icons']}, | 
					
						
							|  |  |  |         children: [{ type: 'text', value: 'link' }] | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     ] | 
					
						
							| 
									
										
										
										
											2017-04-27 14:04:51 +01:00
										 |  |  |   }] | 
					
						
							|  |  |  | ]; |