|
| 1 | + |
| 2 | +let GLOSSARY_PAGES = {}; |
| 3 | +eleventyConfig.on('beforeBuild', () => { |
| 4 | + GLOSSARY_PAGES = {}; |
| 5 | +}); |
| 6 | + |
| 7 | +eleventyConfig.addCollection("glossaire", function (collectionApi) { |
| 8 | + let filteredByGlob = collectionApi.getFilteredByGlob(["glossaire/*.md", "glossaire/*.html"]); |
| 9 | + const sortedGlossaryPages = filteredByGlob |
| 10 | + .sort((a, b) => a.data.title.localeCompare(b.data.title)); |
| 11 | + |
| 12 | + sortedGlossaryPages.forEach(page => { |
| 13 | + |
| 14 | + const {title, description} = page.data |
| 15 | + GLOSSARY_PAGES[page.url] = { |
| 16 | + title: title || "", |
| 17 | + description: description || "", |
| 18 | + id: `glossary-${page.template.fileSlugStr}` |
| 19 | + }; |
| 20 | + }) |
| 21 | + |
| 22 | + return sortedGlossaryPages; |
| 23 | +}); |
| 24 | + |
| 25 | +// - Tooltips pour les liens qui pointent vers une page de glossaire |
| 26 | +// - ajout d'une classe CSS pour signifier qu'il s'agit d'un terme de glossaire |
| 27 | +eleventyConfig.addTransform("glossary-tooltip", function (content, outputPath) { |
| 28 | + if (!outputPath || !outputPath.endsWith(".html")) { |
| 29 | + return content |
| 30 | + } |
| 31 | + // mapping title/description à construire une seule fois (simple cache) |
| 32 | + // TODO: implement mapping lookup using this.collections.glossaire if needed |
| 33 | + const glossaryLinkRegex = /<a\s+([^>]*?)href="(\/glossaire\/[^"]+)"([^>]*?)>([\s\S]*?)<\/a>/g; |
| 34 | + // TODO: replce all occurences |
| 35 | + // add dom content |
| 36 | + const str = "" |
| 37 | + return content.replaceAll(glossaryLinkRegex, (matchedSubstring, pre, href, post, inner) => { |
| 38 | + // placeholder: lookup title/description from a map |
| 39 | + const glossaryPage = GLOSSARY_PAGES[href]; // GLOSS_MAP à construire en haut du fichier |
| 40 | + if (!glossaryPage) return matchedSubstring; |
| 41 | + const payload = encodeURIComponent(JSON.stringify({ |
| 42 | + title: glossaryPage.title, |
| 43 | + desc: glossaryPage.description |
| 44 | + })); |
| 45 | + return `<a ${pre} href="${href}" ${post} data-gloss-tooltip="${payload}">${inner}</a>`; |
| 46 | + }); |
| 47 | +}); |
0 commit comments