-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheleventy-ids.js
41 lines (29 loc) · 1.01 KB
/
eleventy-ids.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module.exports = function( eleventyConfig, config = {} ) {
const { JSDOM } = require('jsdom');
const selectors = config.selectors || ['h1','h2','h3','h4','h5','h6'];
const prefix = config.prefix || '';
const formatter = config.formatter || function(element,existing_ids){
var id = prefix + eleventyConfig.javascriptFunctions.slug(element.textContent).replace(/[&,+()$~%.'":*?!<>{}]/g, "");
if( existing_ids.indexOf(id) >= 0 ){}
return id;
};
// add meta tag to each html page
eleventyConfig.addTransform("ids", async (rawContent, outputPath) => {
if( !outputPath || !outputPath.endsWith(".html") ){
return rawContent;
}
const dom = new JSDOM(rawContent);
const ids = [];
selectors.forEach((selector)=>{
dom.window.document.querySelectorAll(selector).forEach( (element) => {
var id = element.getAttribute('id')
if( id != undefined ){
ids.push(id);
return;
}
element.setAttribute('id',formatter(element,ids));
});
});
return dom.serialize();
});
}