|
| 1 | +const makeItemLink = (slug) => `#${slug}` |
| 2 | +const findExistingDefinition = (word, collection) => collection.find(item => item.data.title === word) |
| 3 | + |
1 | 4 | module.exports = function (config) { |
2 | 5 | // Add a filter using the Config API |
3 | | - config.addFilter('linkTarget', (slug) => `#${slug}`); |
| 6 | + config.addFilter('linkTarget', makeItemLink); |
| 7 | + |
| 8 | + config.addFilter('linkIfExistsInCollection', (word, collection) => { |
| 9 | + const existingDefinition = findExistingDefinition(word, collection) |
| 10 | + |
| 11 | + if (existingDefinition) { |
| 12 | + return `<a href="${makeItemLink(existingDefinition.data.slug)}">${word}</a>` |
| 13 | + } |
| 14 | + |
| 15 | + return word |
| 16 | + }) |
| 17 | + |
| 18 | + config.addFilter('linkSubTermIfDefined', (subTermData, collection) => { |
| 19 | + const existingDefinition = findExistingDefinition(subTermData.full_title, collection) |
| 20 | + |
| 21 | + if (existingDefinition) { |
| 22 | + return `<a href="${makeItemLink(existingDefinition.data.slug)}" aria-label="${subTermData.full_title}">${subTermData.text}</a>` |
| 23 | + } |
| 24 | + |
| 25 | + return subTermData.text |
| 26 | + }) |
4 | 27 |
|
| 28 | + // just a debug filter to lazily inspect the content of anything in a template |
5 | 29 | config.addFilter('postInspect', function (post) { |
6 | 30 | console.log(post); |
7 | | - |
8 | 31 | }) |
9 | 32 |
|
10 | | - config.addPassthroughCopy({'_site/css/': 'assets/css/'}) |
| 33 | + config.addPassthroughCopy({'assets/css/': 'assets/css/'}) |
11 | 34 |
|
12 | | - // Add collections here |
13 | | - config.addCollection('definitions', collection => { |
14 | | - return [ |
15 | | - ...collection |
16 | | - .getFilteredByGlob('./11ty/definitions/*.md') |
17 | | - .sort((a, b) => { |
18 | | - // `toLowerCase()` is just a safety measure, slugs should be lower case anyway |
19 | | - // `localeCompare()` is super cool: http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare |
20 | | - return a.data.slug.toLowerCase().localeCompare(b.data.slug.toLowerCase()) |
21 | | - })] |
| 35 | + config.addShortcode('definitionFlag', (flag) => { |
| 36 | + const cleanText = new Map([ |
| 37 | + ['avoid', { |
| 38 | + class: 'avoid', |
| 39 | + text: 'Avoid' |
| 40 | + }], |
| 41 | + ['better-alternative', { |
| 42 | + class: 'better', |
| 43 | + text: 'Better alternate' |
| 44 | + }], |
| 45 | + ['tool', { |
| 46 | + class: 'tool', |
| 47 | + text: '' |
| 48 | + }] |
| 49 | + ]) |
| 50 | + |
| 51 | + if (flag) { |
| 52 | + const info = cleanText.get(flag.level) |
| 53 | + |
| 54 | + const sep = flag.text && info.text ? '—' : '' |
| 55 | + const text = flag.text ? [info.text, flag.text].join(sep) : info.text |
| 56 | + |
| 57 | + return `<p class="word__signal word__signal--${info.class}">${text}</p>` |
| 58 | + } |
| 59 | + |
| 60 | + return '<p class="word__signal"></p>' |
| 61 | + }); |
| 62 | + |
| 63 | + |
| 64 | + // NOTE (ovlb): this will not be remembered as the best code i’ve written. if anyone seeing this has a better solution then the following to achieve sub groups of the definitions: i am happy to get rid of it |
| 65 | + config.addCollection('tableOfContent', collection => { |
| 66 | + const allItems = collection |
| 67 | + .getFilteredByGlob('./11ty/definitions/*.md') |
| 68 | + .filter(word => !word.data.skip_in_table_of_content) |
| 69 | + .sort((a, b) => { |
| 70 | + const { title: firstTitle } = a.data |
| 71 | + const { title: secondTitle } = b.data |
| 72 | + const sortA = firstTitle.toLowerCase().replace(/^-/, '') |
| 73 | + const sortB = secondTitle.toLowerCase().replace(/^-/, '') |
| 74 | + |
| 75 | + // `localeCompare()` is super cool: http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare |
| 76 | + return sortA.localeCompare(sortB) |
| 77 | + }) |
| 78 | + |
| 79 | + const split = { |
| 80 | + notLetters: { |
| 81 | + title: '#', |
| 82 | + definitions: [] |
| 83 | + }, |
| 84 | + aToE: { |
| 85 | + title: 'A–E', |
| 86 | + definitions: [] |
| 87 | + }, |
| 88 | + fToL: { |
| 89 | + title: 'F–L', |
| 90 | + definitions: [] |
| 91 | + }, |
| 92 | + mToS: { |
| 93 | + title: 'M–S', |
| 94 | + definitions: [] |
| 95 | + }, |
| 96 | + tToZ: { |
| 97 | + title: 'T–Z', |
| 98 | + definitions: [] |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + allItems.forEach(word => { |
| 103 | + const { title } = word.data |
| 104 | + const { notLetters, aToE, fToL, mToS, tToZ } = split |
| 105 | + const sortableTitle = title.replace(/^-/, '') |
| 106 | + |
| 107 | + if (/^[a-e]/gmi.test(sortableTitle)) { |
| 108 | + return aToE.definitions.push(word) |
| 109 | + } |
| 110 | + |
| 111 | + if (/^[f-l]/i.test(sortableTitle)) { |
| 112 | + return fToL.definitions.push(word) |
| 113 | + } |
| 114 | + |
| 115 | + if (/^[m-s]/i.test(sortableTitle)) { |
| 116 | + return mToS.definitions.push(word) |
| 117 | + } |
| 118 | + |
| 119 | + if (/^[t-z]/i.test(sortableTitle)) { |
| 120 | + return tToZ.definitions.push(word) |
| 121 | + } |
| 122 | + |
| 123 | + // no reg ex as the fallback to avoid testing for emojis and numbers |
| 124 | + notLetters.definitions.push(word) |
| 125 | + }) |
| 126 | + |
| 127 | + return Object.keys(split).map(key => { |
| 128 | + const { title, definitions } = split[key] |
| 129 | + |
| 130 | + return { title, definitions } |
| 131 | + }) |
22 | 132 | }) |
23 | 133 |
|
24 | | - config.addCollection('definedDefinitions', collection => { |
25 | | - return [ |
26 | | - ...collection |
| 134 | + config.addCollection('definedWords', collection => { |
| 135 | + return collection |
27 | 136 | .getFilteredByGlob('./11ty/definitions/*.md') |
28 | 137 | .filter(word => word.data.defined) |
29 | 138 | .sort((a, b) => { |
30 | | - // `toLowerCase()` is just a safety measure, slugs should be lower case anyway |
31 | 139 | // `localeCompare()` is super cool: http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare |
32 | | - return a.data.slug.toLowerCase().localeCompare(b.data.slug.toLowerCase()) |
33 | | - })] |
| 140 | + return a.data.title.toLowerCase().localeCompare(b.data.title.toLowerCase()) |
| 141 | + }) |
34 | 142 | }) |
35 | 143 |
|
| 144 | + const mdIt = require('markdown-it')({ |
| 145 | + html: true |
| 146 | + }) |
| 147 | + const prism = require('markdown-it-prism') |
| 148 | + const anchor = require('markdown-it-anchor') |
| 149 | + |
| 150 | + mdIt.use(prism) |
| 151 | + mdIt.use(anchor) |
| 152 | + |
| 153 | + config.setLibrary('md', mdIt); |
| 154 | + |
36 | 155 | // You can return your Config object (optional). |
37 | 156 | return { |
38 | 157 | dir: { |
|
0 commit comments