import $ from "jquery"; import * as JSX from "./jsx"; import { Page, Spinner } from "./components"; import { FtpEntry, ftpGetEntries } from "./util"; let entries: FtpEntry[] = []; let reqErr = 0; let hljs = null; let hljs_imported = 0; const page_meta = $( "meta[name=description]" ).attr( "content" ); async function highlightCode() { if( !hljs || !hljs_imported ) return setTimeout( highlightCode, 500 ); const elements = $( "code" ); elements.each( ( _, e ) => { hljs.highlightElement( e ); } ); } async function importHlJs() { if( hljs ) return; if( hljs_imported ) return; hljs_imported = 1; hljs = ( await import( 'highlight.js' ) ).default; } function urlForHref( href: string, isdir: boolean ) { const url = new URL( window.location.href ); let path = url.pathname; if( isdir ) { if( path.endsWith( '/' ) ) { return path + href; } return path + "/" + href; } const file = href.slice( 0, href.lastIndexOf( "." ) ); path = path.slice( path.indexOf( '/blog' ) + 5 ); if( path.endsWith( '/' ) ) return "/blog" + path + file; else return "/blog" + path + "/" + file; } function BlogEntry( props: any ) { const entry = props.entry as FtpEntry; return { entry.name == "../" && JSX.goUpDirectory() } class="package-entry-link"> ../ } { entry.name != "../" && !entry.isdir && {entry.name.replace( /_/g, " " )} } { entry.name != "../" && entry.isdir && {entry.name} } {entry.date} {entry.time} } function getEndpoint() { const url = new URL( window.location.href ); let href = url.pathname.split( "/blog" )[1]; if( href[0] == '/' ) { href = href.slice( 1 ); } return "posts/" + href; } function populateEntries() { if( reqErr ) { return JSX.navigate( "/404" ); } if( entries.length <= 0 ) return setTimeout( populateEntries, 100 ); const target = $( "#package-entries" ).find( "table" ); $( "#package-entries" ).find( ".spinner" ).remove(); for( const entry of entries ) { target.append( ); } } function updateMeta( is_post: boolean ) { if( is_post ) { const entry = $( "#blog-entry" ); if( !entry.length ) return; let text = entry[0].innerText; if( text.length > 300 ) text = text.slice( 0, 300 ) + "..."; $( "meta[name='description']" ).attr( "content", text ); } else { $( "meta[name='description']" ).attr( "content", page_meta ); } } async function populatePost() { try { const res = await fetch( "https://networkheaven.net/" + getEndpoint() + ".html" ); const text = await res.text(); if( text.startsWith( "" ) || text.startsWith( "" ); const titlee = text.indexOf( "" ); if( titles != -1 && titlee != -1 ) { let title = text.substring( titles + 7, titlee - 1 ); title = title.replace( /\n/g, "" ); $( ".page-title h3" )[0].innerText = title; $( "title" ).html( title + " - networkheaven.net" ); } $( "#package-entries" ).find( ".spinner" ).remove(); $( "#package-entries" ).css( "display", "none" ); $( "#blog-entry" ).html( text ); $( "#blog-entry" ).css( "display", "flex" ); $( "#back-btn" ).css( "display", "block" ); updateMeta( true ); setTimeout( highlightCode ); } catch( e ) { updateMeta( false ); return populateEntries(); } } export default function Blog() { importHlJs(); entries = []; reqErr = 0; setTimeout( async () => { try { entries = await ftpGetEntries( getEndpoint(), getEndpoint() != 'posts/' ); } catch( e ) { reqErr = 1; } } ); JSX.onPreNavigate( () => { updateMeta( false ); JSX.onPreNavigate( () => {} ); } ); setTimeout( () => { if( getEndpoint() == 'posts/' ) return populateEntries(); populatePost(); } ); return

BLOG