2023-06-15 09:18:46 +00:00
/ * W e l c o m e t o t h e s c r i p t f i l e ! Y o u r 1 s t t i m e h e r e , y o u s h o u l d u p d a t e
2023-07-28 11:59:34 +00:00
the basic info section to include your name and website / social
media link ( if desired ) . Most of the time , you will just come
here to update the posts array . However , you can also edit or
add your own scripts to do whatever you like ! * /
2023-06-15 09:18:46 +00:00
//TABLE OF CONTENTS
2023-07-28 11:59:34 +00:00
// 1. Basic Info
// 2. Posts Array
// 3. Creating HTML Sections to Be Inserted (Header, Footer, etc)
// 4. Inserting the Sections Into our Actual HTML Pages
2023-06-15 09:18:46 +00:00
//-----------------------------
//==[ 1. BASIC INFO ]==
2023-07-28 11:59:34 +00:00
let blogName = "Leilukin's Hub Blog" ;
2023-06-15 09:18:46 +00:00
let authorName = "Leilukin" ;
let authorLink = "https://leilukin.neocities.org/" ; // Enter your website, social media, etc. Some way for people to tell you they like your blog! (Leaving it empty is okay too)
//-----------------------------
//==[ 2. POSTS ARRAY ]==
/ * E a c h t i m e y o u m a k e a n e w p o s t , a d d t h e f i l e p a t h h e r e a t t h e t o p o f p o s t s A r r a y .
2023-07-28 11:59:34 +00:00
This will cause all the right links to appear and work .
NOTE : It ' s important to follow this exact naming convention , because the scripts
below are expecting it ( 'posts/YYYY-MM-DD-Title-of-Your-Post.html' , ) . You can
alter the scripts if you want to use a different naming convention * /
2023-06-15 09:18:46 +00:00
/ * U P D A T E : a s o f v e r s i o n 1 . 3 , y o u m a y o m i t t h e d a t e i f y o u w o u l d l i k e . B u t i f y o u
2023-07-28 11:59:34 +00:00
use a date it must still follow that format . * /
2023-06-15 09:18:46 +00:00
let postsArray = [
2023-10-13 14:46:25 +00:00
[ "posts/2023-10-13-My-Cassette-Beasts-Pombomb-Plushie-Arrived.html" , encodeURI ( ` My Cassette Beasts Pombomb Plushie Arrived! ` ) ] ,
2023-09-10 16:28:25 +00:00
[ "posts/2023-09-11-Happy-1st-Anniversary-to-Leilukins-Hub.html" , encodeURI ( ` Happy 1st Anniversary to Leilukin's Hub ` ) ] ,
2023-07-28 11:59:34 +00:00
[ "posts/2023-07-28-My-First-Birthday-After-the-Launch-of-Leilukins-Hub.html" , encodeURI ( ` My First Birthday After the Launch of Leilukin's Hub ` ) ] ,
[ "posts/2023-05-09-Onboard-the-Dracula-Daily-Hype-Train.html" ] ,
[ "posts/2023-01-28-Leilukins-Hub-Now-Has-a-Blog.html" , encodeURI ( ` Leilukin's Hub Now Has a Blog! ` ) ]
// [ "posts/2020-11-10-Special-Characters-Example.html", encodeURI( 'Spéci@l "Character\'s" Examp|e' ) ],
// [ "posts/2020-11-10-Post-Template.html" ]
2023-06-15 09:18:46 +00:00
] ;
//-----------------------------
//==[ 3. CREATING HTML SECTIONS TO BE INSERTED ]==
let url = window . location . pathname ;
//The date format to look for is 4 digits, hyphen, 2 digits, hyphen, 2 digits, hyphen.
const postDateFormat = /\d{4}\-\d{2}\-\d{2}\-/ ;
//Check if you are in posts (if so, the links will have to go up a directory)
let relativePath = "." ;
if ( url . includes ( "posts/" ) ) {
2023-07-28 11:59:34 +00:00
relativePath = ".." ;
2023-06-15 09:18:46 +00:00
}
//Variable for my main site URL
const mainSiteURL = "/home.html" ;
//Write the Header HTML, a series of list items containing links.
let headerHTML = '<ul> <li><a href="' + relativePath + '/index.html">Blog</a></li>' +
'<li><a href="' + relativePath + '/archive.html">Archive</a></li>' +
'<li><a href="' + mainSiteURL + '">Main Site</a></li> </ul>' ;
//Write the Footer HTML, which has information about the blog.
let footerHTML = "<hr><p>" + blogName + " is written by <a href='" + authorLink + "'>" + authorName + "</a>, built with <a href='https://zonelets.net/' target='_blank'>Zonelets</a>.</p>" ;
//To do the following stuff, we want to know where we are in the posts array (if we're currently on a post page).
let currentIndex = - 1 ;
let currentFilename = url . substring ( url . lastIndexOf ( 'posts/' ) ) ;
//Depending on the web server settings (Or something?), the browser url may or may not have ".html" at the end. If not, we must add it back in to match the posts array. (12-19-2022 fix)
if ( ! currentFilename . endsWith ( ".html" ) ) {
2023-07-28 11:59:34 +00:00
currentFilename += ".html" ;
2023-06-15 09:18:46 +00:00
}
let i ;
for ( i = 0 ; i < postsArray . length ; i ++ ) {
2023-07-28 11:59:34 +00:00
if ( postsArray [ i ] [ 0 ] === currentFilename ) {
currentIndex = i ;
}
2023-06-15 09:18:46 +00:00
}
//Convert the post url to readable post name. E.g. changes "2020-10-10-My-First-Post.html" to "My First Post"
//Or pass along the "special characters" version of the title if one exists
function formatPostTitle ( i ) {
2023-07-28 11:59:34 +00:00
// Check if there is an alternate post title
if ( postsArray [ i ] . length > 1 ) {
//Remember how we had to use encodeURI for special characters up above? Now we use decodeURI to get them back.
return decodeURI ( postsArray [ i ] [ 1 ] ) ;
} else {
//If there is no alternate post title, check if the post uses the date format or not, and return the proper title
2023-06-15 09:18:46 +00:00
if ( postDateFormat . test ( postsArray [ i ] [ 0 ] . slice ( 6 , 17 ) ) ) {
return postsArray [ i ] [ 0 ] . slice ( 17 , - 5 ) . replace ( /-/g , " " ) ;
2023-07-28 11:59:34 +00:00
} else {
return postsArray [ i ] [ 0 ] . slice ( 6 , - 5 ) . replace ( /-/g , " " ) ;
}
2023-06-15 09:18:46 +00:00
}
}
//Get the current post title and date (if we are on a post page)
let currentPostTitle = "" ;
let niceDate = "" ;
if ( currentIndex > - 1 ) {
2023-07-28 11:59:34 +00:00
currentPostTitle = formatPostTitle ( currentIndex ) ;
//Generate the "nice to read" version of date
if ( postDateFormat . test ( postsArray [ currentIndex ] [ 0 ] . slice ( 6 , 17 ) ) ) {
let monthSlice = postsArray [ currentIndex ] [ 0 ] . slice ( 11 , 13 ) ;
let month = "" ;
if ( monthSlice === "01" ) { month = "Jan" ; }
else if ( monthSlice === "02" ) { month = "Feb" ; }
else if ( monthSlice === "03" ) { month = "Mar" ; }
else if ( monthSlice === "04" ) { month = "Apr" ; }
else if ( monthSlice === "05" ) { month = "May" ; }
else if ( monthSlice === "06" ) { month = "Jun" ; }
else if ( monthSlice === "07" ) { month = "Jul" ; }
else if ( monthSlice === "08" ) { month = "Aug" ; }
else if ( monthSlice === "09" ) { month = "Sep" ; }
else if ( monthSlice === "10" ) { month = "Oct" ; }
else if ( monthSlice === "11" ) { month = "Nov" ; }
else if ( monthSlice === "12" ) { month = "Dec" ; }
2023-06-15 09:18:46 +00:00
niceDate = postsArray [ currentIndex ] [ 0 ] . slice ( 14 , 16 ) + " " + month + ", " + postsArray [ currentIndex ] [ 0 ] . slice ( 6 , 10 ) ;
2023-07-28 11:59:34 +00:00
}
2023-06-15 09:18:46 +00:00
}
//Generate the Post List HTML, which will be shown on the "Archive" page.
function formatPostLink ( i ) {
2023-07-28 11:59:34 +00:00
let postTitle _i = "" ;
if ( postsArray [ i ] . length > 1 ) {
postTitle _i = decodeURI ( postsArray [ i ] [ 1 ] ) ;
} else {
2023-06-15 09:18:46 +00:00
if ( postDateFormat . test ( postsArray [ i ] [ 0 ] . slice ( 6 , 17 ) ) ) {
postTitle _i = postsArray [ i ] [ 0 ] . slice ( 17 , - 5 ) . replace ( /-/g , " " ) ;
2023-07-28 11:59:34 +00:00
} else {
postTitle _i = postsArray [ i ] [ 0 ] . slice ( 6 , - 5 ) . replace ( /-/g , " " ) ;
}
}
if ( postDateFormat . test ( postsArray [ i ] [ 0 ] . slice ( 6 , 17 ) ) ) {
return '<li><a href="' + relativePath + '/' + postsArray [ i ] [ 0 ] + '">' + postsArray [ i ] [ 0 ] . slice ( 6 , 16 ) + " \u00BB " + postTitle _i + '</a></li>' ;
2023-06-15 09:18:46 +00:00
} else {
2023-07-28 11:59:34 +00:00
return '<li><a href="' + relativePath + '/' + postsArray [ i ] [ 0 ] + '">' + postTitle _i + '</a></li>' ;
2023-06-15 09:18:46 +00:00
}
}
let postListHTML = "<ul>" ;
for ( let i = 0 ; i < postsArray . length ; i ++ ) {
2023-07-28 11:59:34 +00:00
postListHTML += formatPostLink ( i ) ;
2023-06-15 09:18:46 +00:00
}
postListHTML += "</ul>" ;
//Generate the Recent Post List HTML, which can be shown on the home page (or wherever you want!)
let recentPostsCutoff = 3 ; //Hey YOU! Change this number to set how many recent posts to show before cutting it off with a "more posts" link.
let recentPostListHTML = "<h2>Recent Posts:</h2><ul>" ;
let numberOfRecentPosts = Math . min ( recentPostsCutoff , postsArray . length ) ;
for ( let i = 0 ; i < numberOfRecentPosts ; i ++ ) {
2023-07-28 11:59:34 +00:00
recentPostListHTML += formatPostLink ( i ) ;
2023-06-15 09:18:46 +00:00
}
/ * I f y o u ' v e w r i t t e n m o r e p o s t s t h a n c a n f i t i n t h e R e c e n t P o s t s L i s t ,
2023-07-28 11:59:34 +00:00
then we ' ll add a link to the archive so readers can find the rest of
your wonderful posts and be filled with knowledge . * /
2023-06-15 09:18:46 +00:00
if ( postsArray . length > recentPostsCutoff ) {
2023-07-28 11:59:34 +00:00
recentPostListHTML += '<li class="moreposts"><a href=' + relativePath + '/archive.html>\u00BB more posts</a></li></ul>' ;
2023-06-15 09:18:46 +00:00
} else {
2023-07-28 11:59:34 +00:00
recentPostListHTML += "</ul>" ;
2023-06-15 09:18:46 +00:00
}
//Generate the Next and Previous Post Links HTML
let nextprevHTML = "" ;
let nextlink = "" ;
let prevlink = "" ;
/ * I f y o u ' r e o n t h e n e w e s t b l o g p o s t , t h e r e ' s n o p o i n t t o
a "Next Post" link , right ? And vice versa with the oldest
post ! That ' s what the following code handles . * /
if ( postsArray . length < 2 ) {
2023-07-28 11:59:34 +00:00
nextprevHTML = '<a href="' + relativePath + '/index.html">Blog</a>' ;
2023-06-15 09:18:46 +00:00
} else if ( currentIndex === 0 ) {
2023-07-28 11:59:34 +00:00
prevlink = postsArray [ currentIndex + 1 ] [ 0 ] ;
nextprevHTML = '<a href="' + relativePath + '/index.html">Blog</a> | <a href="' + relativePath + '/' + prevlink + '">Previous Post \u00BB</a>' ;
2023-06-15 09:18:46 +00:00
} else if ( currentIndex === postsArray . length - 1 ) {
2023-07-28 11:59:34 +00:00
nextlink = postsArray [ currentIndex - 1 ] [ 0 ] ;
nextprevHTML = '<a href="' + relativePath + '/' + nextlink + '">\u00AB Next Post</a> | <a href="' + relativePath + '/index.html">Blog</a>' ;
2023-06-15 09:18:46 +00:00
} else if ( 0 < currentIndex && currentIndex < postsArray . length - 1 ) {
2023-07-28 11:59:34 +00:00
nextlink = postsArray [ currentIndex - 1 ] [ 0 ] ;
prevlink = postsArray [ currentIndex + 1 ] [ 0 ] ;
nextprevHTML = '<a href="' + relativePath + '/' + nextlink + '">\u00AB Next Post</a> | <a href="' + relativePath + '/index.html">Blog</a> | <a href="' + relativePath + '/' + prevlink + '">Previous Post \u00BB</a>' ;
2023-06-15 09:18:46 +00:00
}
//-----------------------------
//==[ 4. INSERTING THE SECTIONS INTO OUR ACTUAL HTML PAGES ]==
/ * H e r e w e c h e c k i f e a c h r e l e v a n t d i v e x i s t s . I f s o , w e i n j e c t t h e c o r r e c t H T M L !
2023-07-28 11:59:34 +00:00
NOTE : All of these sections are optional to use on any given page . For example , if there ' s
one particular blog post where we don ' t want the footer to appear ,
we simply don ' t put a < div id = "footer" > on that page . * /
2023-06-15 09:18:46 +00:00
if ( document . getElementById ( "nextprev" ) ) {
2023-07-28 11:59:34 +00:00
document . getElementById ( "nextprev" ) . innerHTML = nextprevHTML ;
2023-06-15 09:18:46 +00:00
}
if ( document . getElementById ( "postlistdiv" ) ) {
2023-07-28 11:59:34 +00:00
document . getElementById ( "postlistdiv" ) . innerHTML = postListHTML ;
2023-06-15 09:18:46 +00:00
}
if ( document . getElementById ( "recentpostlistdiv" ) ) {
2023-07-28 11:59:34 +00:00
document . getElementById ( "recentpostlistdiv" ) . innerHTML = recentPostListHTML ;
2023-06-15 09:18:46 +00:00
}
if ( document . getElementById ( "header" ) ) {
2023-07-28 11:59:34 +00:00
document . getElementById ( "header" ) . innerHTML = headerHTML ;
2023-06-15 09:18:46 +00:00
}
if ( document . getElementById ( "blogTitleH1" ) ) {
2023-07-28 11:59:34 +00:00
document . getElementById ( "blogTitleH1" ) . innerHTML = blogTitle ;
2023-06-15 09:18:46 +00:00
}
if ( document . getElementById ( "postTitleH1" ) ) {
2023-07-28 11:59:34 +00:00
document . getElementById ( "postTitleH1" ) . innerHTML = currentPostTitle ;
2023-06-15 09:18:46 +00:00
}
if ( document . getElementById ( "postDate" ) ) {
2023-07-28 11:59:34 +00:00
document . getElementById ( "postDate" ) . innerHTML = niceDate ;
2023-06-15 09:18:46 +00:00
}
if ( document . getElementById ( "footer" ) ) {
2023-07-28 11:59:34 +00:00
document . getElementById ( "footer" ) . innerHTML = footerHTML ;
2023-06-15 09:18:46 +00:00
}
//Dynamically set the HTML <title> tag from the postTitle variable we created earlier
//The <title> tag content is what shows up on browser tabs
if ( document . title === "Blog Post" ) {
2023-07-28 11:59:34 +00:00
document . title = currentPostTitle + ` | Blog | Leilukin's Hub ` ;
2023-06-15 09:18:46 +00:00
}