hypermedia blog

static site generator

hypermedia blog

src/views/admin/admin.ts


import Mustache from "mustache";
import { BlogPost, getCategories, getPostByURL, getPosts, getTags } from "../../models/blog.js";

async function getFile(file: string) {
  return Bun.file(`src/views/admin/${file}.html`, {
    type: "text/html;charset=utf-8",
  }).text();
}

export async function RenderForm(domain: string) {
  const postHtml = await getFile("main");
  return Mustache.render(postHtml, {
    domain: domain,
    footer: "xero harrison",
    content: "<h1>hi!</h1>",
  });
}

export async function RenderEditForm(post: any, domain: string) {
  const postHtml = await getFile("edit_post");
  const postData = getPostByURL(post);
  return Mustache.render(postHtml, {
    url: postData[0].url,
    date: new Date(postData[0].date * 1000).toLocaleDateString("en-CA", {
      timeZone: "America/New_York",
    }),
    title: postData[0].title,
    subtitle: postData[0].subtitle,
    excerpt: postData[0].excerpt,
    content: postData[0].content,
    tagcloud: () => {
      let tags = "";
      postData[0].meta.tags.forEach((tag) => {
        tags += `<button onclick="untag(event, 'btn${tag.url}')" id="btn${tag.url}" class="tag">${tag.name}<input type="hidden" name="tags" value="${tag.url}"/></button>`;
      });
      return tags;
    },
    tags: () => {
      let tags: string = "";
      const allTags = getTags();
      allTags.forEach((tag) => {
        tags += `<option value="${tag.name}"></option>`;
      });
      return tags;
    },
		pounce: () => {
      let cats: string = "";
      postData[0].meta.cats.forEach((cat) => {
        cats += `<button onclick="untag(event, 'btn${cat.url}')" id="btn${cat.url}" class="tag">${cat.name}<input type="hidden" name="cats" value="${cat.url}"/></button>`;
      });
      return cats;
		},
    cats: () => {
      let cats: string = "";
      const allCats = getCategories();
      allCats.forEach((cat) => {
        cats += `<option value="${cat.name}"></option>`;
      });
      return cats;
    },
    domain: domain,
    footer: "xero harrison",
  });
}

export async function RenderEdit(hx: boolean, domain: string) {
  const blogPosts: BlogPost = getPosts(1000, 0);
  let list: string = "";
  blogPosts.forEach((post: any) => {
    list += `
				<option value="${post.url}"/>`;
  });
  const listHtml = await getFile("edit_select");
  const editList: string = Mustache.render(listHtml, {
    list: list,
  });
  if (hx) {
    return editList;
  } else {
    const editHtml = await getFile("main");
    return Mustache.render(editHtml, {
      domain: domain,
      footer: "xero harrison",
      content: editList,
      ripcache: Date.now(),
    });
  }
}

export async function RenderDelete(hx: boolean, domain: string) {
  const blogPosts: BlogPost = getPosts(1000, 0);
  let list: string = "";
  blogPosts.forEach((post: any) => {
    list += `
				<option value="${post.title}"/>`;
  });
  const listHtml = await getFile("rm_select");
  const rmList: string = Mustache.render(listHtml, {
    list: list,
  });
  if (hx) {
    return rmList;
  } else {
    const rmHtml = await getFile("main");
    return Mustache.render(rmHtml, {
      domain: domain,
      footer: "xero harrison",
      content: rmList,
    });
  }
}

Download

raw zip tar