Compare commits

..

No commits in common. "19d63ed0d5009b143e231877d9a02e1062cfc1a0" and "41674ea6a175c8917f8e793506638a81ee50d606" have entirely different histories.

9 changed files with 22 additions and 34 deletions

3
.gitmodules vendored
View File

@ -0,0 +1,3 @@
[submodule "articles"]
path = articles
url = git@gitea.hareworks.net:Hare/blog-articles.git

View File

@ -3,7 +3,6 @@
--color-concept: hsl(39, 100%, 25%, 0.5); --color-concept: hsl(39, 100%, 25%, 0.5);
--color-concept-hsl: 39, 100%, 25%; --color-concept-hsl: 39, 100%, 25%;
--color-outline: #ffffff40; --color-outline: #ffffff40;
width: 100%;
* { * {
color: var(--color-text); color: var(--color-text);
} }
@ -11,15 +10,10 @@
position: relative; position: relative;
width: auto; width: auto;
font-size: 2rem; font-size: 2rem;
margin-bottom: 0.5rem; border-bottom: 1px solid var(--color-outline);
padding-bottom: 0.5rem;
} }
hr { > *:not(h1) {
padding: 0;
margin: 0;
border: none;
border-top: 1px solid var(--color-outline);
}
> *:not(h1, hr) {
margin-left: 2rem; margin-left: 2rem;
} }
h2 { h2 {
@ -99,7 +93,6 @@
font-family: monospace; font-family: monospace;
margin-top: 0.75rem; margin-top: 0.75rem;
margin-bottom: 0.75rem; margin-bottom: 0.75rem;
white-space: pre-wrap;
} }
code { code {
display: inline-block; display: inline-block;

View File

@ -5,10 +5,10 @@
import Footer from '$lib/components/footer.svelte'; import Footer from '$lib/components/footer.svelte';
import * as publish from '$lib/publish'; import * as publish from '$lib/publish';
import FormattedDate from '$lib/components/formatted_date.svelte'; import FormattedDate from '$lib/components/formatted_date.svelte';
import Head from '$lib/components/head.svelte';
import type { Article } from '$lib/article'; import type { Article } from '$lib/article';
export let data: Article; export let data: Article;
const isUpdated = data.updated_at.getTime() != data.released_at.getTime();
</script> </script>
<svelte:head> <svelte:head>
@ -18,7 +18,7 @@
<meta property="og:image" content={data.image} /> <meta property="og:image" content={data.image} />
<meta property="og:type" content="article" /> <meta property="og:type" content="article" />
<meta property="article:published_time" content={data.released_at.toISOString()} /> <meta property="article:published_time" content={data.released_at.toISOString()} />
{#if isUpdated} {#if data.updated_at}
<meta property="article:modified_time" content={data.updated_at.toISOString()} /> <meta property="article:modified_time" content={data.updated_at.toISOString()} />
{/if} {/if}
<meta property="article:author" content="HareWorks" /> <meta property="article:author" content="HareWorks" />
@ -37,7 +37,7 @@
<div class="meta"> <div class="meta">
<span> <span>
released <FormattedDate date={data.released_at} /> released <FormattedDate date={data.released_at} />
{#if isUpdated}<br /> {#if data.updated_at}<br />
updated <FormattedDate date={data.updated_at} /> updated <FormattedDate date={data.updated_at} />
{/if} {/if}
</span> </span>
@ -100,7 +100,7 @@
min-height: 100%; min-height: 100%;
padding: 20px; padding: 20px;
padding-top: 100px; padding-top: 100px;
max-width: 1000px; width: 1000px;
margin: 0 auto; margin: 0 auto;
display: flex; display: flex;
flex-direction: column; flex-direction: column;

View File

@ -1,4 +1,5 @@
import pg from 'pg'; import pg from 'pg';
import { getConnection } from './database/get_connection';
export class Postgres { export class Postgres {
client: pg.PoolClient | null = null; client: pg.PoolClient | null = null;
@ -32,3 +33,7 @@ export class Postgres {
export default async ( export default async (
pool: pg.Pool pool: pg.Pool
) => { return await Postgres.new(pool); }; ) => { return await Postgres.new(pool); };
import { building } from '$app/environment';
import init from '$lib/server/database/init_db';
if (!building) await init(await Postgres.new(getConnection()));

View File

@ -13,7 +13,3 @@ const connectionString = `postgres://${PG_USER}:${PG_PASS}@${PG_HOST}:${PG_PORT}
const pool = new Pool({ connectionString }); const pool = new Pool({ connectionString });
export const getConnection = () => pool; export const getConnection = () => pool;
import init from '$lib/server/database/init_db';
import PG from '$lib/server/database';
await init(await PG(pool));

View File

@ -9,10 +9,6 @@ export default async function init(db: Postgres) {
console.log('Pulling articles from git..'); console.log('Pulling articles from git..');
const stdout = execSync('git -c core.sshCommand="ssh -i ../key -F /dev/null" pull', { cwd: './articles/' }); const stdout = execSync('git -c core.sshCommand="ssh -i ../key -F /dev/null" pull', { cwd: './articles/' });
console.log(stdout.toString()); console.log(stdout.toString());
} else {
console.log('Cloning articles from git..');
const stdout = execSync('git -c core.sshCommand="ssh -i ./key -F /dev/null" clone git@gitea.hareworks.net:Hare/blog-articles.git articles', { cwd: './' });
console.log(stdout.toString());
} }
const schemas = [ const schemas = [
@ -117,19 +113,13 @@ export default async function init(db: Postgres) {
for (const { path, id } of articleFiles) { for (const { path, id } of articleFiles) {
const res = await db.query('select * from article where id = $1', [id]); const res = await db.query('select * from article where id = $1', [id]);
const compiled = await compile(fs.readFileSync(path, 'utf-8'), { const compiled = await compile(fs.readFileSync(path, 'utf-8'));
highlight: {
highlighter: (code: string, lang: string) => {
return `<Codeblock><code class="language-${lang}">${code}</code></Codeblock>`;
}
}
});
const title = compiled.data.fm.title; const title = compiled.data.fm.title;
const category = path.split('/')[3]; const category = path.split('/')[3];
const tags: string[] = compiled.data.fm.tags; const tags: string[] = compiled.data.fm.tags;
const released_at = new Date(compiled.data.fm.released_at); const released_at = new Date(compiled.data.fm.released_at);
const updated_at = (compiled.data.fm.updated_at !== null) ? new Date(compiled.data.fm.updated_at) : released_at; const updated_at = new Date(compiled.data.fm.updated_at);
const image = compiled.data.fm.image; const image = compiled.data.fm.image;
const publish = compiled.data.fm.publish; const publish = compiled.data.fm.publish;
const content = compiled.code const content = compiled.code

View File

@ -7,16 +7,17 @@ import {
} from '$env/static/private' } from '$env/static/private'
import PG from '$lib/server/database'; import PG from '$lib/server/database';
import { getConnection } from '$lib/server/database/get_connection';
// import { building } from '$app/environment'; // import { building } from '$app/environment';
import init from '$lib/server/database/init_db'; import init from '$lib/server/database/init_db';
export const POST: RequestHandler = async ({ url, locals }) => { export const POST: RequestHandler = async ({ url }) => {
const token = url.searchParams.get('token'); const token = url.searchParams.get('token');
console.log(token); console.log(token);
if (token !== TOKEN) { if (token !== TOKEN) {
return error(401, 'Unauthorized'); return error(401, 'Unauthorized');
} }
await init(await PG(locals.db)); await init(await PG(getConnection()));
return new Response(String(token)); return new Response(String(token));
}; };

View File

@ -22,7 +22,7 @@ export const load: PageServerLoad = async ({ params, locals }) => {
return data return data
} catch (e) { } catch (e) {
await db.rollback(); await db.rollback();
error(404, 'Not found'); error(500, (e as Error).message);
} finally { } finally {
await db.release(); await db.release();
} }

View File

@ -22,7 +22,7 @@ export const load: PageServerLoad = async ({ params, locals }) => {
return data return data
} catch (e) { } catch (e) {
await db.rollback(); await db.rollback();
error(404, 'Not found'); error(500, (e as Error).message);
} finally { } finally {
await db.release(); await db.release();
} }