Skip to content

Commit

Permalink
Add comments and css code highlight (minor changes)
Browse files Browse the repository at this point in the history
  • Loading branch information
xypnox committed Oct 13, 2024
1 parent 1a97fa9 commit dbcb3c3
Show file tree
Hide file tree
Showing 17 changed files with 314 additions and 84 deletions.
2 changes: 1 addition & 1 deletion astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default defineConfig({
markdown: {
remarkPlugins: [remarkBreaks],
shikiConfig: {
theme: shikiTheme,
theme: 'css-variables',
}
},
vite: {
Expand Down
3 changes: 1 addition & 2 deletions src/components/BlogPostCard.astro
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,9 @@ const dateFormatOption = {
color: var(--heading);
}

.blog-entry:not(:has(.coverImage)) {
:global(.first) .blog-entry:not(:has(.coverImage)) {
h2 {
font-size: var(--font-size-xl);
margin-block-end: 1em;
}
}
.blog-entry .details {
Expand Down
19 changes: 19 additions & 0 deletions src/components/codeembed.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
import { Code } from 'astro:components';
interface Props {
url: string;
lang: string;
}
const codeUrl = async (url: string) => {
const code = await fetch(url).then((res) => res.text());
return code;
};
const { url, lang } = Astro.props;
const code = await codeUrl(url);
---

<Code code={code} lang={lang} theme="css-variables" />

93 changes: 93 additions & 0 deletions src/components/comint/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { createEffect, createResource } from "solid-js";
import styles from './style.module.css';

const instance = 'https://fosstodon.org';
const fetchComments = async (id: string) => {
const url = instance + '/api/v1/statuses/' + id + '/context';

const response = await fetch(url);
return (await response.json()) as {
ancestors: Comment[];
descendants: Comment[];
};
}

interface Comment {
account: {
display_name: string;
avatar_static: string;
url: string;
};
content: string;
created_at: string;
uri: string;
url: string;
replies_count: number;
reblogs_count: number;
favourites_count: number;
}

const Comment = (props: { comment: Comment }) => {
return (
<article class={styles.comment + ' theme-card'}>
<header>
<div>
<img src={props.comment.account.avatar_static} alt="" width="32" height="32" />
<h3>
<a href={props.comment.account.url}>
{props.comment.account.display_name}
</a>
</h3>
</div>
{props.comment.created_at &&
<p>
<a href={props.comment.url}>
{(new Date(props.comment.created_at)).toDateString()}
</a>
</p>
}
</header>
<div>
<div class={styles.ccontent} innerHTML={props.comment.content} />
<div class={styles.meta}>
<a href={props.comment.url}>
{props.comment.replies_count} Replies, {' '}
{props.comment.reblogs_count} Reblogs, {' '}
{props.comment.favourites_count} Favourites
</a>
</div>
</div>
</article>
);
}

export const Commint = (props: { tootId: string }) => {
// console.log('COMMINT');

const [data, { refetch, }] = createResource(async () => fetchComments(props.tootId));

createEffect(() => {
const d = data();
console.log({ d });
});
return (
<div>
{data.loading && <p>Loading...</p>}
{data.error && <p>Error: {data.error}</p>}
{data() && (
<>
<h2>Comments:</h2>
<p>
<a href={instance + '/web/statuses/' + props.tootId}>View on Mastodon</a>
</p>
<br />
<div class={styles.comments}>
{data()!.descendants.map((comment: Comment) => (
<Comment comment={comment} />
))}
</div>
</>
)}
</div>
);
}
55 changes: 55 additions & 0 deletions src/components/comint/style.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
.comments {
display: flex;
flex-direction: column;
gap: 1em;
}

.comment {
padding: 1em;
border-radius: calc(2 * var(--border-radius));

header {
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
> div {
display: flex;
align-items: center;
gap: 0.5em;
}
h3,
p {
margin: 0;
}
img {
width: 2rem;
height: 2rem;
border-radius: 50%;
flex-grow: 0;
flex-shrink: 0;
margin: 0;
}
}

.ccontent {
padding: 0.5em;
p {
margin: 0.5em 0;
}
}

.meta {
display: flex;
justify-content: flex-end;
align-items: center;
gap: 0.5em;
font-size: 0.8em;
color: #666;

a {
text-decoration: none;
border-bottom: none;
}
}
}
2 changes: 1 addition & 1 deletion src/components/images/homeIcon.astro
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const { type } = Astro.props;
padding-inline-start: 1em;
}
.home-icon :global(svg) {
height: auto;
height: 12em;
width: 66%;
}
.home-icon :global(svg .surface-fill) {
Expand Down
42 changes: 42 additions & 0 deletions src/content/blog/comments-via-mastodon.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
title: 'Comments via Mastodon'
date: 2024-10-13
description: "Replaced comments with Mastodon's context API calls."

categories: ["tech"]
tags: ["tech", "software"]

tootId: "113034812167024055"
hideTOC: true
---

import CodeEmbed from '../../components/codeembed.astro';

I have replaced the comment mechanism of this website again.

I looked into [Webmentions](https://indieweb.org/webmention) but the setup was quite complex. I want to add it later.

Now the comments are raw calls to the mastodon context APIs. There is a slight technicality in updating the `tootId` in the post frontmatter, but when set it will fetch and render the comments on client side (so they will be latest).

Old comments are available in the [old repo's issues](https://github.com/xypnox/blag_zola/issues).

Earlier people needed a github account to post comments, now they need a mastodon/fediverse account instead.


Here is the [basic Solid component](https://) I wrote for rendering comments:

<CodeEmbed url="https://raw.githubusercontent.com/xypnox/xypnox.github.io/refs/heads/v2/src/theme.ts" lang="ts" />

And the code embed component (because why not):

<CodeEmbed url="https://raw.githubusercontent.com/xypnox/xypnox.github.io/refs/heads/v2/src/theme.ts" lang="astro" />

Currently apart from the user profile image, the data is rendered in plain text, no icons.

Also new is the css based syntax highlighting from github code blocks as seen above.

With these changes, the site is now entirely customizable in terms of colors and fonts. The code blocks and comments were the last remaining parts that did not use the theme variables.



Hopefully you will find comments below.
1 change: 1 addition & 0 deletions src/content/blog/governmint-wobsite-bad.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ description: 'Why are government websites bad?'
categories: ["tech", "culture"]
tags: ["tech", "culture", "software"]

tootId: "113034812167024055"
hideTOC: true
---

Expand Down
49 changes: 26 additions & 23 deletions src/content/config.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
import { z, defineCollection } from 'astro:content';
import { z, defineCollection } from "astro:content";

const blog = defineCollection({
schema: ({ image }) => z.object({
title: z.string(),
date: z.date(),
description: z.string().optional(),
tags: z.array(z.string()).optional(),
schema: ({ image }) =>
z.object({
title: z.string(),
date: z.date(),
description: z.string().optional(),
tags: z.array(z.string()).optional(),

categories: z.array(z.string()).optional(),
categories: z.array(z.string()).optional(),

// Image to show in blog list and intro image in blog post
coverImage: image().optional(),
coverAlt: z.string().optional(),
socialImage: image().optional(), // will use coverImage or default
hidden: z.boolean().optional(), // will default to false
// Image to show in blog list and intro image in blog post
coverImage: image().optional(),
coverAlt: z.string().optional(),
socialImage: image().optional(), // will use coverImage or default
hidden: z.boolean().optional(), // will default to false

hideTOC: z.boolean().optional(), // will default to false
}),
})
tootId: z.string().optional(), // to show the comments from mastodon toot

const poems = defineCollection({
schema: () => z.object({
title: z.string(),
date: z.date(),
hidden: z.boolean().optional(), // will default to false
}),
})
hideTOC: z.boolean().optional(), // will default to false
}),
});

export const collections = { blog, poems }
const poems = defineCollection({
schema: () =>
z.object({
title: z.string(),
date: z.date(),
hidden: z.boolean().optional(), // will default to false
}),
});

export const collections = { blog, poems };
14 changes: 14 additions & 0 deletions src/layouts/MainLayout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,18 @@ const props = Astro.props;
font-size: var(--font-size-base);
}
}

.astro-code {
--astro-code-color-text: var(--text);
--astro-code-color-background: var(--background);
--astro-code-token-constant: var(--primary-color);
--astro-code-token-string: var(--heading);
--astro-code-token-comment: var(--fadeText);
--astro-code-token-keyword: var(--primary-color);
--astro-code-token-parameter: var(--heading);
--astro-code-token-function: var(--primary-color);
--astro-code-token-string-expression: var(--heading);
--astro-code-token-punctuation: var(--fadeText);
--astro-code-token-link: var(--primary-color);
}
</style>
19 changes: 4 additions & 15 deletions src/pages/blag/posts/[...slug].astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Image } from "astro:assets";
import type { CollectionEntry } from "astro:content";
import Layout from "../../../layouts/MainLayout.astro";
import Toc from "../../../components/Toc.astro";
import { Commint } from "../../../components/comint";
import Tag from "../../../components/nav/Tag.astro";
import { blogStaticPaths } from "../../../lib/dataAstro";
Expand Down Expand Up @@ -98,16 +99,7 @@ console.log({ entryD: entry.data });
<p>Sooner or later, everything ends.</p>
</div>
<h2>Comments</h2>
<script
is:inline
src="https://utteranc.es/client.js"
repo="xypnox/blag_zola"
issue-term="pathname"
label="comments"
theme="photon-dark"
crossorigin="anonymous"
async></script>
{entry.data.tootId && <Commint tootId={entry.data.tootId} client:load />
</div>
{
headings.length > 0 && entry.data.hideTOC !== true && (
Expand Down Expand Up @@ -185,8 +177,8 @@ console.log({ entryD: entry.data });
max-width: var(--layout-content-main);
width: 100%;
margin: 0 auto;
line-height: 1.5;
font-size: 1.2rem;
line-height: 1.6;
font-size: 1.1em;
}

blockquote.hidden-post {
Expand Down Expand Up @@ -252,9 +244,6 @@ console.log({ entryD: entry.data });
max-width: 800px;
position: relative;
}
.content {
font-size: 1rem;
}
}

@media (max-width: 390px) {
Expand Down
4 changes: 2 additions & 2 deletions src/pages/design/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const designSections = [
slug: "https://posters.xypnox.com",
},
{
name: "Experiments",
name: "Blag Posts",
image: "/design-covers/exp.webp",
slug: "https://xypnox.studio",
slug: "/blag/tags/design/",
},
];
---
Expand Down
2 changes: 1 addition & 1 deletion src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import LatestBlog from "../components/latestBlog.astro";
{ title: "agenc", href: "https://agenc.xyp.one", desc: "A system for online services" },
{ title: "Tools", href: "/tools/", desc: "A collection of utilities" },
// { title: "Experiments", href: "https://xypnox.studio", desc: "A place to experiment with frontend" },
{ title: "Repositories", href: "https://github.com/xypnox?tab=repositories", desc: "Repositories and open code" },
{ title: "Repositories", href: "https://github.com/xypnox", desc: "Repositories and open code" },
{ title: "dotfiles", href: "https://github.com/xypnox/dotfiles", desc: "Configuration files for system" },
]}
>
Expand Down
Loading

0 comments on commit dbcb3c3

Please sign in to comment.