2020-01-17 08:52:42 +01:00
const makeItemLink = ( slug ) => ` # ${ slug } ` ;
const findExistingDefinition = ( word , collection ) =>
collection . find ( ( item ) => item . data . title === word ) ;
2019-11-14 09:32:09 +01:00
2020-01-17 08:52:42 +01:00
module . exports = function ( config ) {
2019-11-11 23:51:52 +01:00
// Add a filter using the Config API
2019-11-14 09:32:09 +01:00
config . addFilter ( 'linkTarget' , makeItemLink ) ;
config . addFilter ( 'linkIfExistsInCollection' , ( word , collection ) => {
2020-01-17 08:52:42 +01:00
const existingDefinition = findExistingDefinition ( word , collection ) ;
2019-11-14 09:32:09 +01:00
2019-11-15 20:08:41 +01:00
if ( existingDefinition ) {
2020-01-17 08:52:42 +01:00
return ` <a href=" ${ makeItemLink (
existingDefinition . data . slug
) } " > $ { word } < / a > ` ;
2019-11-14 09:32:09 +01:00
}
2019-11-12 15:08:40 +01:00
2020-01-17 08:52:42 +01:00
return word ;
} ) ;
2019-11-14 09:32:09 +01:00
2019-11-17 19:49:50 +01:00
config . addFilter ( 'linkSubTermIfDefined' , ( subTermData , collection ) => {
2020-01-17 08:52:42 +01:00
const existingDefinition = findExistingDefinition (
subTermData . full _title ,
collection
) ;
2019-11-17 19:49:50 +01:00
if ( existingDefinition ) {
2020-01-17 08:52:42 +01:00
return ` <a href=" ${ makeItemLink (
existingDefinition . data . slug
) } " aria-label=" $ { subTermData . full _title } " > $ { subTermData . text } < / a > ` ;
2019-11-17 19:49:50 +01:00
}
2020-01-17 08:52:42 +01:00
return subTermData . text ;
} ) ;
2019-11-17 19:49:50 +01:00
2019-11-14 09:32:09 +01:00
// just a debug filter to lazily inspect the content of anything in a template
2020-01-17 08:52:42 +01:00
config . addFilter ( 'postInspect' , function ( post ) {
2019-11-12 15:08:40 +01:00
console . log ( post ) ;
2020-01-17 08:52:42 +01:00
} ) ;
2019-11-12 15:08:40 +01:00
2020-01-17 08:52:42 +01:00
config . addPassthroughCopy ( { 'assets/css/' : 'assets/css/' } ) ;
2019-11-11 23:51:52 +01:00
2019-11-17 22:30:46 +01:00
config . addShortcode ( 'definitionFlag' , ( flag ) => {
2019-11-15 23:24:12 +01:00
const cleanText = new Map ( [
2020-01-17 08:52:42 +01:00
[
'avoid' ,
{
class : 'avoid' ,
text : 'Avoid'
}
] ,
[
'better-alternative' ,
{
class : 'better' ,
text : 'Better alternate'
}
] ,
[
'tool' ,
{
class : 'tool' ,
text : ''
}
]
] ) ;
2019-11-15 23:24:12 +01:00
if ( flag ) {
2020-01-17 08:53:48 +01:00
const info = cleanText . get ( flag . level . toLowerCase ( ) ) ;
2019-11-15 23:24:12 +01:00
2020-01-17 08:52:42 +01:00
const sep = flag . text && info . text ? '—' : '' ;
const text = flag . text ? [ info . text , flag . text ] . join ( sep ) : info . text ;
2019-11-15 23:24:12 +01:00
2020-01-17 08:52:42 +01:00
return ` <p class="word__signal word__signal-- ${ info . class } "> ${ text } </p> ` ;
2019-11-15 23:24:12 +01:00
}
2020-01-17 08:52:42 +01:00
return '<p class="word__signal"></p>' ;
2019-11-15 23:24:12 +01:00
} ) ;
2019-11-15 22:19:37 +01:00
// 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
2020-01-17 08:52:42 +01:00
config . addCollection ( 'tableOfContent' , ( collection ) => {
2019-11-15 22:19:37 +01:00
const allItems = collection
2019-11-17 19:49:22 +01:00
. getFilteredByGlob ( './11ty/definitions/*.md' )
2020-01-17 08:52:42 +01:00
. filter ( ( word ) => ! word . data . skip _in _table _of _content )
2019-11-17 19:49:22 +01:00
. sort ( ( a , b ) => {
2020-01-17 08:52:42 +01:00
const { title : firstTitle } = a . data ;
const { title : secondTitle } = b . data ;
const sortA = firstTitle . toLowerCase ( ) . replace ( /^-/ , '' ) ;
const sortB = secondTitle . toLowerCase ( ) . replace ( /^-/ , '' ) ;
2019-11-19 20:07:52 +01:00
2019-11-17 19:49:22 +01:00
// `localeCompare()` is super cool: http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare
2020-01-17 08:52:42 +01:00
return sortA . localeCompare ( sortB ) ;
} ) ;
2019-11-15 22:19:37 +01:00
const split = {
notLetters : {
title : '#' ,
definitions : [ ]
} ,
aToE : {
title : 'A– E' ,
definitions : [ ]
} ,
fToL : {
title : 'F– L' ,
definitions : [ ]
} ,
mToS : {
title : 'M– S' ,
definitions : [ ]
} ,
tToZ : {
title : 'T– Z' ,
definitions : [ ]
}
2020-01-17 08:52:42 +01:00
} ;
2019-11-15 22:19:37 +01:00
2020-01-17 08:52:42 +01:00
allItems . forEach ( ( word ) => {
const { title } = word . data ;
const { notLetters , aToE , fToL , mToS , tToZ } = split ;
const sortableTitle = title . replace ( /^-/ , '' ) ;
2019-11-19 20:07:52 +01:00
2020-01-17 08:52:42 +01:00
if ( /^[a-e]/gim . test ( sortableTitle ) ) {
return aToE . definitions . push ( word ) ;
2019-11-15 22:19:37 +01:00
}
2019-11-19 20:07:52 +01:00
if ( /^[f-l]/i . test ( sortableTitle ) ) {
2020-01-17 08:52:42 +01:00
return fToL . definitions . push ( word ) ;
2019-11-15 22:19:37 +01:00
}
2019-11-19 20:07:52 +01:00
if ( /^[m-s]/i . test ( sortableTitle ) ) {
2020-01-17 08:52:42 +01:00
return mToS . definitions . push ( word ) ;
2019-11-15 22:19:37 +01:00
}
2019-11-19 20:07:52 +01:00
if ( /^[t-z]/i . test ( sortableTitle ) ) {
2020-01-17 08:52:42 +01:00
return tToZ . definitions . push ( word ) ;
2019-11-15 22:19:37 +01:00
}
// no reg ex as the fallback to avoid testing for emojis and numbers
2020-01-17 08:52:42 +01:00
notLetters . definitions . push ( word ) ;
} ) ;
2019-11-15 22:19:37 +01:00
2020-01-17 08:52:42 +01:00
return Object . keys ( split ) . map ( ( key ) => {
const { title , definitions } = split [ key ] ;
2019-11-15 22:19:37 +01:00
2020-01-17 08:52:42 +01:00
return { title , definitions } ;
} ) ;
} ) ;
2019-11-11 23:51:52 +01:00
2020-01-17 08:52:42 +01:00
config . addCollection ( 'definedWords' , ( collection ) => {
2019-11-15 20:09:21 +01:00
return collection
2020-01-17 08:52:42 +01:00
. getFilteredByGlob ( './11ty/definitions/*.md' )
. filter ( ( word ) => word . data . defined )
. sort ( ( a , b ) => {
// `localeCompare()` is super cool: http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare
return a . data . title
. toLowerCase ( )
. localeCompare ( b . data . title . toLowerCase ( ) ) ;
} ) ;
} ) ;
2019-11-12 01:17:21 +01:00
2019-11-17 22:30:20 +01:00
const mdIt = require ( 'markdown-it' ) ( {
html : true
2020-01-17 08:52:42 +01:00
} ) ;
const prism = require ( 'markdown-it-prism' ) ;
const anchor = require ( 'markdown-it-anchor' ) ;
2019-11-17 22:30:20 +01:00
2020-01-17 08:52:42 +01:00
mdIt . use ( prism ) ;
mdIt . use ( anchor ) ;
2019-11-17 22:30:20 +01:00
config . setLibrary ( 'md' , mdIt ) ;
2019-11-11 23:51:52 +01:00
// You can return your Config object (optional).
return {
dir : {
2019-11-12 15:17:44 +01:00
input : '11ty' ,
output : 'dist'
} ,
templateFormats : [ 'njk' , 'md' ] ,
htmlTemplateEngine : 'njk' ,
markdownTemplateEngine : 'njk' ,
passthroughFileCopy : true
2019-11-11 23:51:52 +01:00
} ;
} ;