2018-07-20 00:36:48 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-07-20 17:57:25 +02:00
|
|
|
// V A R I A B L E S
|
|
|
|
|
|
|
|
const headerRegex = /###.+/g;
|
|
|
|
const numberRegex = /^[0-9]/g;
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-07-20 00:36:48 +02:00
|
|
|
// E X P O R T
|
|
|
|
|
|
|
|
module.exports = exports = (state, emit, markdown) => {
|
|
|
|
const tocElements = markdown.match(headerRegex);
|
|
|
|
const collectionOfTocElements = [];
|
|
|
|
|
|
|
|
for (const item of tocElements) collectionOfTocElements.push(`<li><a href="${slugify(item)}" title="">${item.replace(/### /g, "")}</a></li>`);
|
|
|
|
|
|
|
|
return `
|
|
|
|
<ul class="component--glossary-toc">
|
|
|
|
${collectionOfTocElements.join("")}
|
|
|
|
</ul>
|
2018-07-27 17:18:29 +02:00
|
|
|
<button class="component--glossary-toc-toggle" data-action="toggle glossary sidebar" type="button">Toggle</button>
|
2018-07-20 00:36:48 +02:00
|
|
|
`;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// H E L P E R
|
|
|
|
|
|
|
|
function slugify(stringToSlugify) {
|
2018-07-20 17:57:25 +02:00
|
|
|
let finalString = stringToSlugify
|
2018-07-20 00:36:48 +02:00
|
|
|
.toLowerCase()
|
2018-07-20 17:57:25 +02:00
|
|
|
.replace(/###\s/g, "")
|
|
|
|
.replace(/\s\/\s/g, "-")
|
2018-07-20 00:36:48 +02:00
|
|
|
.replace(/\s/g, "-")
|
2018-07-20 17:57:25 +02:00
|
|
|
.replace(/%/g, "")
|
2018-07-20 00:36:48 +02:00
|
|
|
.replace(/\(/g, "")
|
|
|
|
.replace(/\)/g, "")
|
2018-07-20 17:57:25 +02:00
|
|
|
.replace(/,/g, "");
|
|
|
|
|
|
|
|
if (finalString.match(numberRegex)) finalString = `_${finalString}`;
|
|
|
|
return `#${finalString}`;
|
2018-07-20 00:36:48 +02:00
|
|
|
}
|