summaryrefslogtreecommitdiff
path: root/web/src/blog.tsx
blob: 738dd7c2967aa20305a18a57cf7f56e790685c84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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;

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 <tr>
    <td>
      { entry.name == "../" &&
      <a href='#' onclick={ () => JSX.goUpDirectory() } class="package-entry-link">
        ../
      </a> }
      { entry.name != "../" && !entry.isdir &&
      <a href={ urlForHref( entry.name, entry.isdir ) } class="package-entry-link">
        {entry.name.replace( /_/g, " " )}
      </a> }
      { entry.name != "../" && entry.isdir &&
      <a href={ urlForHref( entry.name, entry.isdir ) } class="package-entry-link">
        {entry.name}
      </a> }
    </td>
    <td><span class="package-entry-date">{entry.date} {entry.time}</span></td>
  </tr>
}

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( <BlogEntry entry={entry} /> );
  }
}

async function populatePost() {
  try {
    const res = await fetch( "https://networkheaven.net/" + getEndpoint() + ".html" );
    const text = await res.text();
    if( text.startsWith( "<html>" ) || text.startsWith( "<!doctype html" ) ) {
      return populateEntries();
    }

    const titles = text.indexOf( "<title>" );
    const titlee = text.indexOf( "</title>" );

    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 );
    }


    $( "#package-entries" ).find( ".spinner" ).remove();
    $( "#package-entries" ).css( "display", "none" );
    $( "#blog-entry" ).html( text );
    $( "#blog-entry" ).css( "display", "flex" );
    $( "#back-btn" ).css( "display", "block" );
    setTimeout( highlightCode );
  } catch( e ) {
    return populateEntries();
  }
}

export default function Blog() {
  importHlJs();
  entries = [];
  reqErr = 0;
  setTimeout( async () => {
    try {
      entries = await ftpGetEntries( getEndpoint(), getEndpoint() != 'posts/' );
    } catch( e ) {
      reqErr = 1;
    }
  } );

  setTimeout( () => {
    if( getEndpoint() == 'posts/' )
      return populateEntries();

    populatePost();
  } );

  return <Page style="display: flex; flex-direction: column">
    <div class="page-title">
      <h3 style="font-family: JPN24; width: auto" class="gradient">BLOG</h3>
    </div>
    <hr />

    <div id="package-entries">
      <table style="width: 90%;" />
      <Spinner />
    </div>

    <div style="display: flex; flex-direction: column width: 100%; justify-content: center; margin-bottom: 26px">
      <div id="blog-entry" style="display: none">
      </div>
    </div>
    <div style="position: absolute; bottom: 2px; left: calc( 50% - 15px ); display: none" id="back-btn">
      <a href="#" onClick={ () => JSX.goUpDirectory() }>BACK</a>
    </div>

  </Page>
}