From 3032a8495174f4f583f52c4e9429b6d4d357dc0c Mon Sep 17 00:00:00 2001 From: navewindre Date: Wed, 12 Nov 2025 06:32:24 +0100 Subject: aa --- web/src/blog.tsx | 156 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 152 insertions(+), 4 deletions(-) (limited to 'web/src/blog.tsx') diff --git a/web/src/blog.tsx b/web/src/blog.tsx index 9d86657..bfef434 100644 --- a/web/src/blog.tsx +++ b/web/src/blog.tsx @@ -1,10 +1,158 @@ import $ from "jquery"; import * as JSX from "./jsx"; -import { Page } from "./components"; -import { AsciiArt } from "./ascii-art"; +import { Page, Spinner } from "./components"; +import { FtpEntry, ftpGetEntries } from "./util"; + +let entries: FtpEntry[] = []; +let reqErr = 0; + +let hljs = null; +let hljs_imported = 0; +async function importHlJs() { + if( hljs ) return; + if( hljs_imported ) return; + + hljs_imported = 1; + hljs = ( await import( 'highlight.js' ) ).default; + + const elements = $( "code" ); + elements.each( ( _, e ) => { + hljs.highlightElement( e ); + } ); +} + +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 && + JSX.navigate( urlForHref( entry.name, entry.isdir ) ) } class="package-entry-link"> + {entry.name.replace( /_/g, " " )} + } + { entry.name != "../" && entry.isdir && + JSX.navigate( urlForHref( entry.name, entry.isdir ) ) } class="package-entry-link"> + {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( ); + } +} + +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 ); + } + + importHlJs(); + + $( "#package-entries" ).find( ".spinner" ).remove(); + $( "#package-entries" ).css( "display", "none" ); + $( "#blog-entry" ).html( text ); + $( "#blog-entry" ).css( "display", "flex" ); + $( "#back-btn" ).css( "display", "block" ); + } catch( e ) { + return populateEntries(); + } +} export default function Blog() { - return - work in progress (: + entries = []; + reqErr = 0; + setTimeout( async () => { + try { + entries = await ftpGetEntries( getEndpoint(), getEndpoint() != 'posts/' ); + } catch( e ) { + reqErr = 1; + } + } ); + + setTimeout( () => { + if( getEndpoint() == 'posts/' ) + return populateEntries(); + + populatePost(); + } ); + + return +
+

BLOG

+
+
+ +
+ + + + +
+ +
+ + } -- cgit v1.2.3