Skip to content

Commit b9be87b

Browse files
committed
feat: allow manually aligning character chats
1 parent 75e196f commit b9be87b

File tree

3 files changed

+28
-30
lines changed

3 files changed

+28
-30
lines changed

src/content/posts/showing-off-blog-features/index.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,18 @@ This is a cool feature!
162162
I agree!
163163
:::
164164

165+
You can also manually specify the alignment (`left` or `right`) for any character chat manually:
166+
167+
```md wrap title="Character chats with specific alignment"
168+
:::unicorn{align="right"}
169+
Over here, to the right!
170+
:::
171+
```
172+
173+
:::unicorn{align="right"}
174+
Over here, to the right!
175+
:::
176+
165177
## GitHub Cards
166178

167179
GitHub overview cards heavily inspired by [Astro Cactus](https://github.com/chrismwilliams/astro-theme-cactus).

src/plugins/remark-character-dialogue.ts

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,36 +33,21 @@ const remarkCharacterDialogue: Plugin<[{ characters: Record<string, string> }],
3333
const characterName = node.name
3434
if (!isCharacterDialogue(characterName)) return
3535

36-
let title: string = characterName
37-
let titleNode: PhrasingContent[] = [{ type: 'text', value: title }]
38-
39-
// Check if there's a custom title
40-
const firstChild = node.children[0]
41-
if (
42-
firstChild?.type === 'paragraph' &&
43-
firstChild.data &&
44-
'directiveLabel' in firstChild.data &&
45-
firstChild.children.length > 0
46-
) {
47-
titleNode = firstChild.children
48-
title = mdastToString(firstChild.children)
49-
// The first paragraph contains a custom title, we can safely remove it.
50-
node.children.splice(0, 1)
51-
}
36+
const align = node.attributes?.align ?? null
37+
const alignClass = align === 'left' || align === 'right' ? ` align-${align}` : ''
5238

5339
// Do not change prefix to AD, ADM, or similar, adblocks will block the content inside.
5440
const admonition = h(
5541
'aside',
5642
{
57-
'aria-label': `Character dialogue: ${title}`,
58-
class: 'character-dialogue',
43+
'aria-label': `Character dialogue: ${characterName}`,
44+
class: 'character-dialogue' + alignClass,
5945
'data-character': characterName,
6046
},
6147
[
62-
// h('p', { class: 'admonition-title', 'aria-hidden': 'true' }, [...titleNode]),
6348
h('img', {
6449
class: 'character-dialogue-image',
65-
alt: title,
50+
alt: characterName,
6651
loading: 'lazy',
6752
src: opts.characters[characterName],
6853
width: 100,

src/styles/global.css

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -391,25 +391,26 @@ article img {
391391
@apply mb-4 sm:mb-0 sm:pb-0;
392392
}
393393

394-
& + aside.character-dialogue {
395-
@apply mt-4 sm:mt-3 sm:pt-0;
394+
& + aside.character-dialogue:nth-of-type(even):not(.align-left),
395+
&.align-right {
396+
@apply flex-row-reverse;
396397

397-
&:nth-of-type(even) {
398-
@apply flex-row-reverse;
399-
400-
.character-dialogue-content:after {
401-
@apply -right-3 -rotate-90;
402-
left: initial;
403-
}
398+
.character-dialogue-content:after {
399+
@apply -right-3 -rotate-90;
400+
left: initial;
404401
}
405402
}
406403

404+
& + aside.character-dialogue {
405+
@apply mt-4 sm:mt-3 sm:pt-0;
406+
}
407+
407408
img.character-dialogue-image {
408409
@apply m-0 border-none shrink-1 w-20 sm:w-25;
409410
}
410411

411412
.character-dialogue-content {
412-
@apply relative shrink-5 sm:bg-foreground/6 px-1 mx-1.5 sm:px-6 sm:mt-1 rounded-xl sm:py-6 max-w-full sm:min-w-1/8;
413+
@apply relative shrink-5 sm:bg-foreground/6 px-1 mx-1.5 sm:px-6 sm:mt-0.5 rounded-xl sm:py-6 max-w-full sm:min-w-1/8;
413414

414415
/* Add pointy part of speech bubble to the bottom left corner */
415416
&:after {

0 commit comments

Comments
 (0)