how it’s built/step 06 of 10

Bend it with hooks

Your URLs, your images, your edits, without forking anything.

Three hooks shape what the SDK gives you, without forking it. They live in createPolis({ hooks }), so every page gets the same answer:

  • transformPost(post) changes a post before any page sees it. These posts start with a # Title line, and this design sets the title itself, so the hook takes the line off.
  • resolveLink(href, post) decides where a link to one of the site's own posts points. The default is the same path on your website (/posts/20260303/x.html). This site prefers /posts/20260303/x, and the same hook is what polis.pub's redirects follow (step 9), so there's one rule for every link.
  • resolveImage(src, post) decides where images load from. The default makes the site's own images absolute on the polis site, so they load on your domain with nothing copied. Swap in a CDN here if you like.
lib/polis.tssource ↗
// The one place this website meets polis.
//
// Everything below reads vdibart.polis.pub's own published files: its index,
// its signed markdown, its profile, its follow list. No API key, no account.
// polisFetch tags each read so the publish webhook (app/polis-webhook) can
// refresh exactly the pages a new post changes.

import { createPolis, type Post } from '@polis-pub/sdk';
import { polisFetch } from '@polis-pub/next';

export const SITE = process.env.POLIS_SITE ?? 'https://vdibart.polis.pub';
export const DISPLAY = process.env.POLIS_DISPLAY ?? 'https://vdibart.com';
export const SITE_HOST = new URL(SITE).host;

/** A post's address here, from its page on the polis site: /posts/<date>/<slug>, no .html. */
export function ourPath(polisPage: string): string | null {
  const m = polisPage.match(/^https:\/\/[^/]+\/posts\/(\d{8})\/([^/?#]+?)(?:\.html)?(?:[?#].*)?$/);
  return m ? `/posts/${m[1]}/${m[2]}` : null;
}

export const polis = createPolis({
  site: SITE,
  display: DISPLAY,
  fetch: polisFetch,
  hooks: {
    // The posts carry their title as a leading "# Title" line; this design
    // sets the title itself, so it comes off the body.
    transformPost(post: Post) {
      const lines = post.markdown.split('\n');
      const first = lines.findIndex((l) => l.trim() !== '');
      if (first >= 0 && lines[first].replace(/^#\s+/, '').trim() === post.title.trim() && lines[first].startsWith('# ')) {
        post.markdown = lines.slice(first + 1).join('\n');
      }
      return post;
    },
    // A link to one of the site's own posts goes to that post HERE.
    resolveLink(href: string) {
      const abs = href.startsWith('/') ? SITE + href : href;
      if (abs.startsWith(SITE + '/posts/')) return DISPLAY + (ourPath(abs) ?? '');
      return abs;
    },
  },
});

/** The route params for a post, from its page on the polis site. */
export function paramsFor(polisPage: string): { date: string; slug: string } | null {
  const p = ourPath(polisPage);
  if (!p) return null;
  const [, , date, slug] = p.split('/');
  return { date, slug };
}

/** The polis site's page for route params (what comments point at). */
export const polisPage = (date: string, slug: string) => `${SITE}/posts/${date}/${slug}.html`;

/** Reading time, at 230 words a minute. */
export function readingTime(markdown: string): number {
  return Math.max(1, Math.round(markdown.split(/\s+/).filter(Boolean).length / 230));
}

/** "2026-03-03T20:10:50Z" → "3 Mar 2026". */
export function formatDate(iso: string): string {
  const d = new Date(iso);
  if (Number.isNaN(d.getTime())) return '';
  return d.toLocaleDateString('en-GB', { day: 'numeric', month: 'short', year: 'numeric', timeZone: 'UTC' });
}

/** The verification page for one of the site's files. */
export const validateURL = (source: string) => `https://validate.polis.pub/?url=${encodeURIComponent(source)}`;
The rule: hooks change what you show, never what the author signed. The signed bytes are still post.raw, and the source link still goes to the original.