1- const fs = require ( "fs" ) ;
2- const path = require ( "path" ) ;
1+
32const { EleventyRenderPlugin } = require ( "@11ty/eleventy" ) ;
43const eleventyNavigationPlugin = require ( "@11ty/eleventy-navigation" ) ;
54const Nunjucks = require ( "nunjucks" ) ;
65
7- function findFileInDir ( dir , filename ) {
8- let files = fs . readdirSync ( dir ) ;
9-
10- for ( const file of files ) {
11- let filepath = path . join ( dir , file ) ;
12-
13- if ( fs . statSync ( filepath ) . isDirectory ( ) ) {
14- let result = findFileInDir ( filepath , filename ) ;
15- if ( result ) return result ;
16- } else if ( path . basename ( filepath ) === filename ) {
17- return filepath ;
18- }
19- }
20-
21- return null ;
22- }
23-
24- function getBlocksFromJson ( json ) {
25- let blocks = [ ] ;
26- for ( const key in json ) {
27- if ( json . hasOwnProperty ( key ) ) {
28- const element = json [ key ] ;
29- if ( element [ 'questions' ] ) {
30- blocks . push ( key ) ;
31- }
32- }
33- }
34- return blocks
35- }
36-
37- function findSelfInNavPages ( navPages , url ) {
38- for ( const page of navPages ) {
39- if ( page . key == url ) {
40- return page ;
41- }
42- if ( page . children ) {
43- let result = findSelfInNavPages ( page . children , url ) ;
44- if ( result ) return result ;
45- }
46- }
47-
48- return null ;
49- }
6+ const buildBreadcrumbs = require ( './src/building/breadcrumbs.js' ) ;
7+ const buildChangelog = require ( './src/building/changelog.js' ) ;
8+ const addAsset = require ( './src/building/linking.js' ) ;
9+ const { qka, quizButtons, quizQuestions } = require ( './src/building/quizzing.js' ) ;
5010
5111module . exports = function ( eleventyConfig ) {
5212 eleventyConfig . addPlugin ( EleventyRenderPlugin ) ;
@@ -57,31 +17,18 @@ module.exports = function (eleventyConfig) {
5717 eleventyConfig . addPassthroughCopy ( "**.json" ) ;
5818 eleventyConfig . setDataFileSuffixes ( [ ".data" , "" ] ) ;
5919 eleventyConfig . setQuietMode ( true ) ;
60- eleventyConfig . setServerOptions ( {
61- watch : [ "src/static/css/**" ]
62- } ) ;
63-
64- eleventyConfig . addShortcode ( "addScript" , function ( filename ) {
65- let filepath = findFileInDir ( "./src/static/js" , `${ filename } .js` ) ;
66- if ( filepath ) {
67- return `<script type="module" src="${ filepath . replace ( 'src' , '' ) } "></script>` ;
68- }
20+ eleventyConfig . setServerOptions ( { watch : [ "src/static/css/**" ] } ) ;
6921
70- console . log ( `script file ${ filename } .js not found in /src/static/js/` ) ;
71- return '' ;
72- } ) ;
73-
74- eleventyConfig . addShortcode ( "addStyle" , function ( filename ) {
75- let filepath = findFileInDir ( "./src/static/css" , `${ filename } .sass` ) ;
76- if ( filepath ) {
77- return `<link rel="stylesheet" href="${ filepath . replace ( 'src' , '' ) . replace ( '.sass' , '.css' ) } ">` ;
78- }
7922
80- console . log ( `style file ${ filename } .sass not found in /src/static/css/` ) ;
81- return '' ;
82- } ) ;
23+ eleventyConfig . addShortcode ( "breadcrumbs" , function ( navPages ) { return buildBreadcrumbs ( navPages , this . page ) } ) ;
24+ eleventyConfig . addShortcode ( "changelog" , ( data ) => buildChangelog ( data ) ) ;
25+ eleventyConfig . addShortcode ( "qka" , ( data ) => qka ( data ) ) ;
26+ eleventyConfig . addShortcode ( "quizButtons" , ( json ) => quizButtons ( json ) ) ;
27+ eleventyConfig . addShortcode ( "quizQuestions" , ( json ) => quizQuestions ( json ) ) ;
28+ eleventyConfig . addShortcode ( "addScript" , ( filename ) => addAsset ( "script" , filename ) ) ;
29+ eleventyConfig . addShortcode ( "addStyle" , ( filename ) => addAsset ( "style" , filename ) ) ;
8330
84- eleventyConfig . addShortcode ( "keywords" , function ( ) {
31+ eleventyConfig . addShortcode ( "keywords" , ( ) => {
8532 let json = require ( './package.json' ) ;
8633 if ( ! json . keywords ) {
8734 console . log ( 'keywords not found in package.json' ) ;
@@ -90,183 +37,18 @@ module.exports = function (eleventyConfig) {
9037 return `<meta name="keywords" content="${ json . keywords . join ( ', ' ) } ">` ;
9138 } ) ;
9239
93- eleventyConfig . addShortcode ( "version" , function ( ) {
40+ eleventyConfig . addShortcode ( "version" , ( ) => {
9441 let json = require ( './package.json' ) ;
9542 let version = json . version ;
9643 let channel = json . channel && json . channel !== 'RTW' ? ` (${ json . channel } )` : '' ;
9744 return `<a id="version-tag" href="/changelog/">${ version } ${ channel } </a>` ;
9845 } ) ;
9946
100- // function to get the changelog from the github commit history
101- eleventyConfig . addShortcode ( "changelog" , function ( data ) {
102- let content = '<div id="changelog">'
103-
104- // sort commits by version
105- data . sort ( ( a , b ) => {
106- return a . version . major > b . version . major ? - 1 : 1 ;
107- } )
108-
109- // if the current version isn't in the changelog, add it manually to the data array
110- let currentJson = require ( './package.json' )
111- let currentVersion = currentJson . version . split ( '.' )
112- let currentChannel = currentJson . channel ? ` ${ currentJson . channel } ` : ''
113- if ( ! data . find ( d => d . version . major == currentVersion [ 1 ] && d . version . minor == currentVersion [ 2 ] ) ) {
114- data . unshift ( {
115- "version" : {
116- "major" : currentVersion [ 1 ] ,
117- "minor" : currentVersion [ 2 ]
118- } ,
119- "title" : `${ currentVersion [ 1 ] } .${ currentVersion [ 2 ] } ${ currentChannel } ` ,
120- "date" : "ongoing" ,
121- "message" : "<span class='warning'>changelog for this patch will be built in the next version</span>" ,
122- "hash" : "main"
123- } )
124- }
125-
126- let prevMajor = null
127- data . forEach ( ( d ) => {
128- // if the major version has changed, add a new header
129- if ( prevMajor != d . version . major ) {
130- if ( prevMajor ) content += '<hr>'
131- content += `<h1>v${ d . version . major } </h1>`
132- }
133- prevMajor = d . version . major
134-
135- // add the commit entry
136- content += `<div class="entry hoverborder">
137- <span class="title">${ d . title } </span>`
138- if ( d . message ) content += `<br><span class="message">${ d . message . replace ( / \n / g, '<br>' ) } </span>`
139- content += `<a class="date" href="https://github.com/miermontoto/mier.info/commit/${ d . hash } " target="_blank">${ d . date } </a></div>`
140- } )
141- content += '</div>'
142- return content
143- } )
144-
145- eleventyConfig . addShortcode ( "breadcrumbs" , function ( navPages ) {
146- if ( ! this . page . url ) return '' ; // if permalink is false in frontmatter, don't show breadcrumbs
147-
148- let targetName = this . page . url . replace ( '/' , '' ) . replace ( '/' , '' ) . replace ( '.html' , '' ) . toLowerCase ( ) ;
149- // let targetName = this.page.url.replace('.html', '').split('/');
150- // targetName.shift();
151- // targetName = targetName.join('/');
152- // if (targetName.endsWith('/')) targetName = targetName.slice(0, -1);
153-
154- let currentPage = findSelfInNavPages ( navPages , targetName ) ;
155- if ( ! currentPage ) {
156- console . log ( `unable to produce breadcrumbs for ${ targetName } .` ) ;
157- return '' ;
158- }
159- let html = `<nav aria-label="breadcrumbs" id="breadcrumbs">$ <a href="/" id="indx-btn">/</a>` ;
16047
161-
162- if ( currentPage . parent ) {
163- let parentPage = findSelfInNavPages ( navPages , currentPage . parent ) ;
164- html += `<span class="bc-spacer"> > </span><a class="bc-target" href="${ parentPage . url } ">${ parentPage . key } </a>` ;
165- }
166- html += `<span class="bc-spacer"> > </span><b class="bc-target">${ currentPage . title || currentPage . key } </b>` ;
167- html += `</nav>` ;
168- return html ;
169- } ) ;
170-
171- eleventyConfig . addShortcode ( "top" , function ( ) {
172- return `<span id="top" class="button topbtn">top ↑</span>` ;
173- } ) ;
174-
175- eleventyConfig . addShortcode ( "qka" , function ( data ) {
176- let template = ""
177- data . forEach ( ( d , i ) => {
178- let sources = d . s ? d . s . map ( s => `<li><a href="${ s } ">${ s } </a></li>` ) . join ( ' ' ) : null ;
179- template += `<div class="doubt hoverborder">
180- <span class="question">${ i + 1 } . ${ d . q } </span>` ;
181- if ( d . a ) template += `<span class="answer">${ d . a } </span>`
182- if ( sources ) template += `<span class="sources">sources: <br>${ sources } </span>`
183- if ( d . k && d . d ) template += `<span class="bottom">keywords: <code>${ d . k } </code>, date asked: ${ d . d } </span>`
184- else console . log ( 'warning: question missing keywords or date' )
185- template += `</div>`
186- } )
187- return template
188- } ) ;
189-
190- eleventyConfig . addShortcode ( "quizButtons" , function ( json ) {
191- let blocks = getBlocksFromJson ( json ) ;
192- if ( blocks . length <= 1 ) return '' ;
193-
194- let template = `<div id="block-selection"> <h3>preguntas.</h3> <ul>` ;
195- blocks . forEach ( b => {
196- template += `<li><code>${ b } </code>: ${ json [ b ] [ 'info' ] } </li>` ;
197- } ) ;
198- template += '<li><code>todas</code>: todas las preguntas</li></ul>' ;
199-
200- blocks . forEach ( ( b ) => {
201- template += `<span class="button select-block" id="${ b } ">${ b } </span>`
202- } ) ;
203-
204- template += '<span class="button select-block" id="all">todas</span>' ;
205- template += '</div>' ;
206- return template ;
207- } ) ;
208-
209- eleventyConfig . addShortcode ( "quizQuestions" , function ( json ) {
210- let blocks = getBlocksFromJson ( json ) ;
211- let template = '<div id="questions">' ;
212-
213- blocks . forEach ( ( block ) => {
214- let questions = json [ block ] [ 'questions' ] ;
215-
216- questions . forEach ( ( q ) => {
217- let exam = q . exam == "true" ;
218-
219- template += `
220- <div class="question-block" block="${ block } " exam="${ exam } ">
221- <h2 class="question">
222- ${ q . title }
223- </h2>
224- ` ;
225-
226- if ( q . options ) {
227- template += `<div class="options">` ;
228-
229- q . options . forEach ( ( o , j ) => {
230- template += `<h3 class="option">${ j + 1 } . ${ o } </h3>` ;
231- } ) ;
232-
233- template += `</div>` ;
234- }
235-
236- let shuffle = q . shuffle != "false" ;
237-
238- template += `<div class="answer-block" shuffle="${ shuffle } ">` ;
239- q . answers . forEach ( ( a , j ) => {
240- template += `
241- <span class="button answer${ j == q . correct ? " correct" : "" } ">
242- ${ a }
243- </span> <br>` ;
244- } ) ;
245-
246- template += '</div>' ;
247- if ( exam || ! shuffle ) {
248- template += `<ul class="asterisks">
249- ${ exam ? '<li class="exam">pregunta de examen reciente</li>' : '' }
250- ${ ! shuffle ? '<li class="shuffle">orden de respuestas fijado</li>' : '' }
251- </ul>` ;
252- }
253- template += '</div>' ;
254- } ) ;
255- } ) ;
256-
257- template += `</div>` ;
258- return template ;
259- } ) ;
260-
261- const nunjucksEnvironment = new Nunjucks . Environment (
48+ eleventyConfig . setLibrary ( "njk" , new Nunjucks . Environment (
26249 new Nunjucks . FileSystemLoader ( "./" ) ,
263- {
264- lstripBlocks : true ,
265- trimBlocks : true
266- }
267- ) ;
268-
269- eleventyConfig . setLibrary ( "njk" , nunjucksEnvironment ) ;
50+ { lstripBlocks : true , trimBlocks : true }
51+ ) ) ;
27052
27153 return {
27254 dir : {
0 commit comments