2019-10-09 15:51:43 +02:00
const {
JSDOM
} = require ( "jsdom" ) ;
const fs = require ( "fs" ) ;
2019-10-20 15:56:22 +02:00
const converter = require ( "./showdown" ) ( 2 ) ;
2019-10-09 15:51:43 +02:00
2021-03-19 12:02:31 +01:00
const folder = ` ${ _ _dirname } /../ ` ;
2020-04-02 21:54:20 +02:00
console . log ( "Refreshing blog sub-sites..." ) ;
2021-03-19 07:55:18 +01:00
fs . readFile ( ` ${ folder } index.html ` , function ( _ , html ) {
2020-09-23 04:23:40 +02:00
let templateDom = new JSDOM ( html ) ;
2021-03-11 03:21:23 +01:00
// remove all non-blog stuff from the template (which is just our index.html)
2020-09-23 04:23:40 +02:00
let noBlog = templateDom . window . document . getElementsByClassName ( "no-blog" ) ;
while ( noBlog . length > 0 )
noBlog [ 0 ] . parentNode . removeChild ( noBlog [ 0 ] ) ;
2021-03-11 03:21:23 +01:00
// change all relative links to move to the parent directory (since we're in blog/)
let elements = templateDom . window . document . getElementsByTagName ( "*" ) ;
for ( let i = 0 ; i < elements . length ; i ++ ) {
let element = elements [ i ] ;
if ( element . src && element . src . startsWith ( "./" ) )
2021-03-19 07:55:18 +01:00
element . src = ` ../ ${ element . src . substring ( 2 ) } ` ;
2021-03-11 03:21:23 +01:00
if ( element . href && element . href . startsWith ( "./" ) )
2021-03-19 07:55:18 +01:00
element . href = ` ../ ${ element . href . substring ( 2 ) } ` ;
2021-03-11 03:21:23 +01:00
}
2021-03-19 12:02:31 +01:00
const template = templateDom . serialize ( ) ;
2020-09-23 04:23:40 +02:00
2021-03-19 07:55:18 +01:00
fs . readFile ( ` ${ folder } blog/src/posts.json ` , function ( _ , data ) {
2021-03-19 12:02:31 +01:00
const json = JSON . parse ( data . toString ( ) ) ;
2020-04-02 21:54:20 +02:00
for ( let i = 0 ; i < json . length ; i ++ ) {
2021-03-19 12:02:31 +01:00
const post = json [ i ] ;
const index = i ;
const last = getAdjacentPost ( json , index , - 1 ) ;
const next = getAdjacentPost ( json , index , 1 ) ;
2021-03-31 19:24:36 +02:00
let extension = post . book ? "html" : "md" ;
fs . readFile ( ` ${ folder } blog/src/ ${ post . id } . ${ extension } ` , function ( _ , file ) {
2020-04-02 21:54:20 +02:00
let dom = new JSDOM ( template ) ;
2020-09-23 04:23:40 +02:00
let document = dom . window . document ;
2020-04-02 21:54:20 +02:00
2021-03-19 07:55:18 +01:00
document . title += ` - ${ post . name } ` ;
2020-04-02 21:54:20 +02:00
document . querySelector ( 'meta[property="og:title"]' ) . setAttribute ( "content" , post . name ) ;
document . querySelector ( 'meta[name="description"]' ) . setAttribute ( "content" , post . summary ) ;
document . querySelector ( 'meta[property="og:description"]' ) . setAttribute ( "content" , post . summary ) ;
2021-03-19 07:55:18 +01:00
document . getElementById ( "nav-items" ) . innerHTML = /*html*/ `
2021-03-31 17:00:15 +02:00
< a class = "nav-item nav-link" href = "../?cat=${post.cat[0]}#blog" > Back to Main Page < / a >
2021-03-31 02:48:13 +02:00
$ { last ? /*html*/ ` <a class="nav-item nav-link" href="./ ${ last . id } ">Previous ${ post . cat [ 0 ] } Post</a> ` : "" }
$ { next ? /*html*/ ` <a class="nav-item nav-link" href="./ ${ next . id } ">Next ${ post . cat [ 0 ] } Post</a> ` : "" }
2021-03-19 07:55:18 +01:00
` ;
2020-04-02 21:54:20 +02:00
2021-03-31 19:24:36 +02:00
let content ;
if ( post . book ) {
document . head . innerHTML += /*html*/ ` <link rel="stylesheet" href="../style/book.css"> ` ;
// pull the actual content from the crowbook export
let html = new JSDOM ( file ) ;
let page = html . window . document . getElementById ( "page" ) ;
let header = page . getElementsByTagName ( "header" ) ;
while ( header . length > 0 )
header [ 0 ] . parentNode . removeChild ( header [ 0 ] ) ;
content = /*html*/ `
$ { post . prompt ? /*html*/ ` <p class="meta-info"><i>This story was inspired by a Reedsy Prompt and submitted to their competition. As such, it has also been published on <a href=" ${ post . prompt } ">their website</a>.</i></p> ` : "" }
$ { page . outerHTML }
` ;
} else {
content = converter . makeHtml ( file . toString ( ) ) ;
}
2021-03-19 07:55:18 +01:00
document . getElementById ( "main" ) . innerHTML = /*html*/ `
< div class = "list-display rounded" >
< div class = "blog-isolated" >
< h1 > $ { post . name } < / h 1 >
2021-03-31 19:24:36 +02:00
$ { post . archived ? /*html*/ ` <p class="meta-info"><i>This post has been archived.</i></p> ` : "" }
$ { content }
2021-03-19 07:55:18 +01:00
< span class = "text-muted project-status blog-isolated-status" > $ { post . date } < / s p a n >
$ { post . discuss ? /*html*/ ` <a href=" ${ post . discuss } " class="blog-discuss" id="blog-discuss- ${ post . id } ">Discuss this post</a> ` : "" }
< / d i v >
< / d i v >
` ;
2020-04-02 21:54:20 +02:00
2020-09-23 04:23:40 +02:00
let ret = dom . serialize ( ) ;
2021-03-19 07:55:18 +01:00
fs . mkdir ( ` ${ folder } blog ` , function ( ) {
fs . writeFile ( ` ${ folder } blog/ ${ post . id } .html ` , ret , function ( ) { } ) ;
2021-03-11 03:21:23 +01:00
} ) ;
2020-04-02 21:54:20 +02:00
} ) ;
}
2019-10-09 15:51:43 +02:00
} ) ;
2020-05-06 19:54:21 +02:00
} ) ;
function getAdjacentPost ( json , index , move ) {
2021-03-31 02:48:13 +02:00
let cat = json [ index ] . cat [ 0 ] ;
2020-05-06 19:54:21 +02:00
while ( true ) {
index += move ;
let post = json [ index ] ;
if ( ! post )
break ;
2021-03-31 02:55:17 +02:00
// only link to posts with the same main category
if ( post . cat [ 0 ] === cat && ! post . archived )
2020-05-06 19:54:21 +02:00
return post ;
}
}