diff --git a/.gitmodules b/.gitmodules index db0425c..e69de29 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +0,0 @@ -[submodule "articles"] - path = articles - url = git@gitea.hareworks.net:Hare/blog-articles.git diff --git a/src/lib/blog.scss b/src/lib/blog.scss index 14b18eb..4c6cfcc 100644 --- a/src/lib/blog.scss +++ b/src/lib/blog.scss @@ -3,6 +3,7 @@ --color-concept: hsl(39, 100%, 25%, 0.5); --color-concept-hsl: 39, 100%, 25%; --color-outline: #ffffff40; + width: 100%; * { color: var(--color-text); } @@ -10,10 +11,15 @@ position: relative; width: auto; font-size: 2rem; - border-bottom: 1px solid var(--color-outline); - padding-bottom: 0.5rem; + margin-bottom: 0.5rem; } - > *:not(h1) { + hr { + padding: 0; + margin: 0; + border: none; + border-top: 1px solid var(--color-outline); + } + > *:not(h1, hr) { margin-left: 2rem; } h2 { @@ -93,6 +99,7 @@ font-family: monospace; margin-top: 0.75rem; margin-bottom: 0.75rem; + white-space: pre-wrap; } code { display: inline-block; diff --git a/src/lib/page/article_page.svelte b/src/lib/page/article_page.svelte index 109f79e..204ace7 100644 --- a/src/lib/page/article_page.svelte +++ b/src/lib/page/article_page.svelte @@ -5,10 +5,10 @@ import Footer from '$lib/components/footer.svelte'; import * as publish from '$lib/publish'; import FormattedDate from '$lib/components/formatted_date.svelte'; - import Head from '$lib/components/head.svelte'; import type { Article } from '$lib/article'; export let data: Article; + const isUpdated = data.updated_at.getTime() != data.released_at.getTime(); @@ -18,7 +18,7 @@ - {#if data.updated_at} + {#if isUpdated} {/if} @@ -37,7 +37,7 @@
released - {#if data.updated_at}
+ {#if isUpdated}
updated {/if}
@@ -100,7 +100,7 @@ min-height: 100%; padding: 20px; padding-top: 100px; - width: 1000px; + max-width: 1000px; margin: 0 auto; display: flex; flex-direction: column; diff --git a/src/lib/server/database.ts b/src/lib/server/database.ts index 189b72f..cc4d196 100644 --- a/src/lib/server/database.ts +++ b/src/lib/server/database.ts @@ -1,5 +1,4 @@ import pg from 'pg'; -import { getConnection } from './database/get_connection'; export class Postgres { client: pg.PoolClient | null = null; @@ -33,7 +32,3 @@ export class Postgres { export default async ( pool: pg.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())); diff --git a/src/lib/server/database/get_connection.ts b/src/lib/server/database/get_connection.ts index 8e65981..b8eee44 100644 --- a/src/lib/server/database/get_connection.ts +++ b/src/lib/server/database/get_connection.ts @@ -13,3 +13,7 @@ const connectionString = `postgres://${PG_USER}:${PG_PASS}@${PG_HOST}:${PG_PORT} const pool = new Pool({ connectionString }); export const getConnection = () => pool; + +import init from '$lib/server/database/init_db'; +import PG from '$lib/server/database'; +await init(await PG(pool)); \ No newline at end of file diff --git a/src/lib/server/database/init_db.ts b/src/lib/server/database/init_db.ts index bcb5202..a5d3185 100644 --- a/src/lib/server/database/init_db.ts +++ b/src/lib/server/database/init_db.ts @@ -9,6 +9,10 @@ export default async function init(db: Postgres) { console.log('Pulling articles from git..'); const stdout = execSync('git -c core.sshCommand="ssh -i ../key -F /dev/null" pull', { cwd: './articles/' }); 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 = [ @@ -113,13 +117,19 @@ export default async function init(db: Postgres) { for (const { path, id } of articleFiles) { 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 `${code}`; + } + } + }); const title = compiled.data.fm.title; const category = path.split('/')[3]; const tags: string[] = compiled.data.fm.tags; const released_at = new Date(compiled.data.fm.released_at); - const updated_at = new Date(compiled.data.fm.updated_at); + const updated_at = (compiled.data.fm.updated_at !== null) ? new Date(compiled.data.fm.updated_at) : released_at; const image = compiled.data.fm.image; const publish = compiled.data.fm.publish; const content = compiled.code diff --git a/src/routes/api/fetch_articles/+server.ts b/src/routes/api/fetch_articles/+server.ts index 2520687..7264e6c 100644 --- a/src/routes/api/fetch_articles/+server.ts +++ b/src/routes/api/fetch_articles/+server.ts @@ -7,17 +7,16 @@ import { } from '$env/static/private' import PG from '$lib/server/database'; -import { getConnection } from '$lib/server/database/get_connection'; // import { building } from '$app/environment'; import init from '$lib/server/database/init_db'; -export const POST: RequestHandler = async ({ url }) => { +export const POST: RequestHandler = async ({ url, locals }) => { const token = url.searchParams.get('token'); console.log(token); if (token !== TOKEN) { return error(401, 'Unauthorized'); } - await init(await PG(getConnection())); + await init(await PG(locals.db)); return new Response(String(token)); }; diff --git a/src/routes/article/[category]/[id]/+page.server.ts b/src/routes/article/[category]/[id]/+page.server.ts index f7e4fee..54410e4 100644 --- a/src/routes/article/[category]/[id]/+page.server.ts +++ b/src/routes/article/[category]/[id]/+page.server.ts @@ -22,7 +22,7 @@ export const load: PageServerLoad = async ({ params, locals }) => { return data } catch (e) { await db.rollback(); - error(500, (e as Error).message); + error(404, 'Not found'); } finally { await db.release(); } diff --git a/src/routes/article/[category]/[series]/[id]/+page.server.ts b/src/routes/article/[category]/[series]/[id]/+page.server.ts index 75af536..522fd97 100644 --- a/src/routes/article/[category]/[series]/[id]/+page.server.ts +++ b/src/routes/article/[category]/[series]/[id]/+page.server.ts @@ -22,7 +22,7 @@ export const load: PageServerLoad = async ({ params, locals }) => { return data } catch (e) { await db.rollback(); - error(500, (e as Error).message); + error(404, 'Not found'); } finally { await db.release(); }