Skip to content

Commit cb8a479

Browse files
authored
feat: add file name transformer for fenced code blocks (#535)
* feat: add file name transformer for fenced code blocks * chore: update file names in fenced code blocks
1 parent 38ebf6e commit cb8a479

9 files changed

+156
-72
lines changed

astro.config.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import tailwindcss from "@tailwindcss/vite";
33
import sitemap from "@astrojs/sitemap";
44
import remarkToc from "remark-toc";
55
import remarkCollapse from "remark-collapse";
6-
import { SITE } from "./src/config";
76
import {
87
transformerNotationDiff,
98
transformerNotationHighlight,
109
transformerNotationWordHighlight,
1110
} from "@shikijs/transformers";
11+
import { transformerFileName } from "./src/utils/transformers/fileName";
12+
import { SITE } from "./src/config";
1213

1314
// https://astro.build/config
1415
export default defineConfig({
@@ -26,9 +27,10 @@ export default defineConfig({
2627
defaultColor: false,
2728
wrap: false,
2829
transformers: [
29-
transformerNotationDiff({ matchAlgorithm: "v3" }),
30+
transformerFileName(),
3031
transformerNotationHighlight(),
3132
transformerNotationWordHighlight(),
33+
transformerNotationDiff({ matchAlgorithm: "v3" }),
3234
],
3335
},
3436
},

src/data/blog/adding-new-post.md

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
author: Sat Naing
33
pubDatetime: 2022-09-23T15:22:00Z
4-
modDatetime: 2025-03-22T06:25:46.734Z
4+
modDatetime: 2025-06-13T16:52:45.934Z
55
title: Adding new posts in AstroPaper theme
66
slug: adding-new-posts-in-astropaper-theme
77
featured: true
@@ -84,25 +84,23 @@ Title and description (excerpt) are important for search engine optimization (SE
8484

8585
For example, if the blog file name is `adding-new-post.md` and you don't specify the slug in your frontmatter, Astro will automatically create a slug for the blog post using the file name. Thus, the slug will be `adding-new-post`. But if you specify the `slug` in the frontmatter, this will override the default slug. You can read more about this in [Astro Docs](https://docs.astro.build/en/guides/content-collections/#defining-custom-slugs).
8686

87-
If you omit `tags` in a blog post (in other words, if no tag is specified), the default tag `others` will be used as a tag for that post. You can set the default tag in the `/src/content/config.ts` file.
87+
If you omit `tags` in a blog post (in other words, if no tag is specified), the default tag `others` will be used as a tag for that post. You can set the default tag in the `content.config.ts` file.
8888

89-
```ts
90-
// src/content/config.ts
89+
```ts file="src/content.config.ts"
9190
export const blogSchema = z.object({
92-
// ---
91+
// ...
9392
draft: z.boolean().optional(),
9493
// [!code highlight:1]
9594
tags: z.array(z.string()).default(["others"]), // replace "others" with whatever you want
96-
// ---
95+
// ...
9796
});
9897
```
9998

10099
### Sample Frontmatter
101100

102101
Here is the sample frontmatter for a post.
103102

104-
```yaml
105-
# src/content/blog/sample-post.md
103+
```yaml file="src/data/blog/sample-post.md"
106104
---
107105
title: The title of the post
108106
author: your name
@@ -132,7 +130,7 @@ For instance, if you want to place your table of contents just under the intro p
132130
<!-- prettier-ignore-start -->
133131
```md
134132
---
135-
# some frontmatter
133+
# frontmatter
136134
---
137135

138136
Here are some recommendations, tips & ticks for creating new posts in AstroPaper blog theme.
@@ -150,6 +148,45 @@ There's one thing to note about headings. The AstroPaper blog posts use title (t
150148

151149
This rule is not mandatory, but highly recommended for visual, accessibility and SEO purposes.
152150

151+
## Syntax Highlighting
152+
153+
AstroPaper uses [Shiki](https://shiki.style/) as the default syntax highlighting. Starting from AstroPaper v5.4, [@shikijs/transformers](https://shiki.style/packages/transformers) is used to enhance better fenced code blocks. If you don't want to use it, you can simply remove it like this
154+
155+
```bash
156+
pnpm remove @shikijs/transformers
157+
```
158+
159+
```js file="astro.config.ts"
160+
// ...
161+
// [!code --:5]
162+
import {
163+
transformerNotationDiff,
164+
transformerNotationHighlight,
165+
transformerNotationWordHighlight,
166+
} from "@shikijs/transformers";
167+
168+
export default defineConfig({
169+
// ...
170+
markdown: {
171+
remarkPlugins: [remarkToc, [remarkCollapse, { test: "Table of contents" }]],
172+
shikiConfig: {
173+
// For more themes, visit https://shiki.style/themes
174+
themes: { light: "min-light", dark: "night-owl" },
175+
defaultColor: false,
176+
wrap: false,
177+
transformers: [
178+
transformerFileName(),
179+
// [!code --:3]
180+
transformerNotationHighlight(),
181+
transformerNotationWordHighlight(),
182+
transformerNotationDiff({ matchAlgorithm: "v3" }),
183+
],
184+
},
185+
},
186+
// ...
187+
}
188+
```
189+
153190
## Storing Images for Blog Content
154191
155192
Here are two methods for storing images and displaying them inside a markdown file.

src/data/blog/customizing-astropaper-theme-color-schemes.md

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
author: Sat Naing
33
pubDatetime: 2022-09-25T15:20:35Z
4-
modDatetime: 2025-06-09T07:42:54.791Z
4+
modDatetime: 2025-06-13T16:46:34.155Z
55
title: Customizing AstroPaper theme color schemes
66
featured: false
77
draft: false
@@ -19,19 +19,31 @@ This post will explain how you can enable/disable light & dark mode for the webs
1919

2020
## Enable/disable light & dark mode
2121

22-
AstroPaper theme will include light and dark mode by default. In other words, there will be two color schemes\_ one for light mode and another for dark mode. This default behavior can be disabled in SITE configuration object of the `src/config.ts` file.
22+
AstroPaper theme will include light and dark mode by default. In other words, there will be two color schemes\_ one for light mode and another for dark mode. This default behavior can be disabled in `SITE` configuration object.
2323

24-
```js
25-
// file: src/config.ts
24+
```js file="src/config.ts"
2625
export const SITE = {
27-
website: "https://astro-paper.pages.dev/",
26+
website: "https://astro-paper.pages.dev/", // replace this with your deployed domain
2827
author: "Sat Naing",
28+
profile: "https://satnaing.dev/",
2929
desc: "A minimal, responsive and SEO-friendly Astro blog theme.",
3030
title: "AstroPaper",
3131
ogImage: "astropaper-og.jpg",
32-
lightAndDarkMode: true, // true by default
33-
postPerPage: 3,
34-
};
32+
lightAndDarkMode: true, // [!code highlight]
33+
postPerIndex: 4,
34+
postPerPage: 4,
35+
scheduledPostMargin: 15 * 60 * 1000, // 15 minutes
36+
showArchives: true,
37+
showBackButton: true, // show back button in post detail
38+
editPost: {
39+
enabled: true,
40+
text: "Suggest Changes",
41+
url: "https://github.com/satnaing/astro-paper/edit/main/",
42+
},
43+
dynamicOgImage: true,
44+
lang: "en", // html lang code. Set this empty and default will be "en"
45+
timezone: "Asia/Bangkok", // Default global timezone (IANA format) https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
46+
} as const;
3547
```
3648

3749
To disable `light & dark mode` set `SITE.lightAndDarkMode` to `false`.
@@ -40,16 +52,15 @@ To disable `light & dark mode` set `SITE.lightAndDarkMode` to `false`.
4052

4153
By default, if we disable `SITE.lightAndDarkMode`, we will only get system's prefers-color-scheme.
4254

43-
Thus, to choose primary color scheme instead of prefers-color-scheme, we have to set color scheme in the primaryColorScheme variable inside `public/toggle-theme.js`.
55+
Thus, to choose primary color scheme instead of prefers-color-scheme, we have to set color scheme in the `primaryColorScheme` variable inside `toggle-theme.js`.
4456

45-
```js
46-
/* file: public/toggle-theme.js */
47-
const primaryColorScheme = ""; // "light" | "dark"
57+
```js file="public/toggle-theme.js"
58+
const primaryColorScheme = ""; // "light" | "dark" // [!code hl]
4859

4960
// Get theme data from local storage
5061
const currentTheme = localStorage.getItem("theme");
5162

52-
// other codes etc...
63+
// ...
5364
```
5465

5566
The **primaryColorScheme** variable can hold two values\_ `"light"`, `"dark"`. You can leave the empty string (default) if you don't want to specify the primary color scheme.
@@ -58,20 +69,16 @@ The **primaryColorScheme** variable can hold two values\_ `"light"`, `"dark"`. Y
5869
- `"light"` - use light mode as primary color scheme.
5970
- `"dark"` - use dark mode as primary color scheme.
6071

61-
<details><summary>Why 'primaryColorScheme' is not inside config.ts?</summary>
62-
63-
> To avoid color flickering on page reload, we have to place the toggle-switch JavaScript codes as early as possible when the page loads. It solves the problem of flickering, but as a trade-off, we cannot use ESM imports anymore.
64-
65-
[Click here](https://docs.astro.build/en/reference/directives-reference/#isinline) to know more about Astro's `is:inline` script.
66-
72+
<details>
73+
<summary>Why primaryColorScheme' is not inside config.ts?</summary>
74+
To avoid color flickering on page reload, we have to place the toggle-switch JavaScript codes as early as possible when the page loads. It solves the problem of flickering, but as a trade-off, we cannot use ESM imports anymore.
6775
</details>
6876

6977
## Customize color schemes
7078

71-
Both light & dark color schemes of AstroPaper theme can be customized. You can do this in `src/styles/global.css` file.
79+
Both light & dark color schemes of AstroPaper theme can be customized in the `global.css` file.
7280

73-
```css
74-
/* file: src/styles/global.css */
81+
```css file="src/styles/global.css"
7582
@import "tailwindcss";
7683
@import "./typography.css";
7784

@@ -93,7 +100,7 @@ html[data-theme="dark"] {
93100
--muted: #343f60bf;
94101
--border: #ab4b08;
95102
}
96-
/* other styles */
103+
/* ... */
97104
```
98105

99106
In the AstroPaper theme, the `:root` and `html[data-theme="light"]` selectors define the light color scheme, while `html[data-theme="dark"]` defines the dark color scheme.
@@ -112,8 +119,8 @@ Here is the detail explanation of color properties.
112119

113120
Here is an example of changing the light color scheme.
114121

115-
```css
116-
/* other styles */
122+
```css file="src/styles/global.css"
123+
/* ... */
117124
:root,
118125
html[data-theme="light"] {
119126
--background: #f6eee1;
@@ -122,7 +129,7 @@ html[data-theme="light"] {
122129
--muted: #efd8b0;
123130
--border: #dc9891;
124131
}
125-
/* other styles */
132+
/* ... */
126133
```
127134

128135
> Check out some [predefined color schemes](https://astro-paper.pages.dev/posts/predefined-color-schemes/) AstroPaper has already crafted for you.

src/data/blog/dynamic-og-images.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ Dynamic OG image of AstroPaper includes _the blog post title_, _author name_ and
4646

4747
Titles with non-latin characters won't display properly out of the box. To resolve this, we have to replace `fontsConfig` inside `loadGoogleFont.ts` with your preferred font.
4848

49-
```ts
50-
// file: loadGoogleFont.ts
51-
49+
```ts file=src/utils/loadGoogleFont.ts
5250
async function loadGoogleFonts(
5351
text: string
5452
): Promise<
@@ -75,7 +73,7 @@ async function loadGoogleFonts(
7573
style: "normal",
7674
},
7775
];
78-
// other codes
76+
// ...
7977
}
8078
```
8179

src/data/blog/how-to-add-latex-equations-in-blog-posts.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ In this section, you will find instructions on how to add support for LaTeX in y
3232
pnpm install rehype-katex remark-math katex
3333
```
3434

35-
2. Update the Astro configuration (`astro.config.ts`) to use the these plugins:
35+
2. Update the Astro configuration to use the these plugins:
3636

37-
```ts
38-
// other imports
37+
```ts file=astro.config.ts
38+
// ...
3939
import remarkMath from "remark-math";
4040
import rehypeKatex from "rehype-katex";
4141

4242
export default defineConfig({
43-
// other configs
43+
// ...
4444
markdown: {
4545
remarkPlugins: [
4646
remarkMath, // [!code ++]
@@ -54,13 +54,13 @@ In this section, you will find instructions on how to add support for LaTeX in y
5454
wrap: false,
5555
},
5656
},
57-
// other configs
57+
// ...
5858
});
5959
```
6060

61-
3. Import KaTeX CSS in the main layout file `src/layouts/Layout.astro`
61+
3. Import KaTeX CSS in the main layout file
6262

63-
```astro
63+
```astro file=src/layouts/Layout.astro
6464
---
6565
import { SITE } from "@config";
6666
@@ -82,9 +82,9 @@ In this section, you will find instructions on how to add support for LaTeX in y
8282
</body>
8383
```
8484

85-
4. As the last step, add a text-color for `katex` in `src/styles/typography.css`.
85+
4. As the last step, add a text-color for `katex` in `typography.css`.
8686

87-
```css
87+
```css file=src/styles/typography.css
8888
@plugin '@tailwindcss/typography';
8989

9090
@layer base {

src/data/blog/how-to-configure-astropaper-theme.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ The important configurations resides in `src/config.ts` file. Within that file,
2222

2323
During development, it's okay to leave `SITE.website` empty. But in production mode, you should specify your deployed url in `SITE.website` option since this will be used for canonical URL, social card URL etc.. which are important for SEO.
2424

25-
```js
26-
// file: src/config.ts
25+
```js file=src/config.ts
2726
export const SITE = {
2827
website: "https://astro-paper.pages.dev/", // replace this with your deployed domain
2928
author: "Sat Naing",
@@ -73,12 +72,14 @@ Here are SITE configuration options
7372

7473
## Update layout width
7574

76-
The default `max-width` for the entire blog is `768px` (`max-w-3xl`). If you'd like to change it, you can easily update the `max-w-app` utility in your `src/styles/global.css` file:
75+
The default `max-width` for the entire blog is `768px` (`max-w-3xl`). If you'd like to change it, you can easily update the `max-w-app` utility in your `global.css`. For instance:
7776

78-
```css
77+
```css file=src/styles/global.css
7978
@utility max-w-app {
79+
/* [!code --:1] */
80+
@apply max-w-3xl;
81+
/* [!code ++:1] */
8082
@apply max-w-4xl xl:max-w-5xl;
81-
/* eg: max-w-4xl xl:max-w-5xl */
8283
}
8384
```
8485

@@ -101,11 +102,11 @@ This is the easiest option. You just have to update `SITE.title` in `src/config.
101102
You might want to use this option if you want to use an SVG logo.
102103

103104
- First add an SVG inside `src/assets` directory. (eg: `src/assets/dummy-logo.svg`)
104-
- Then import that SVG inside `src/components/Header.astro`
105+
- Then import that SVG inside `Header.astro`
105106

106-
```astro
107+
```astro file=src/components/Header.astro
107108
---
108-
// other imports
109+
// ...
109110
import DummyLogo from "@/assets/dummy-logo.svg";
110111
---
111112
```
@@ -129,11 +130,11 @@ The best part of this approach is that you can customize your SVG styles as need
129130
If your logo is an image but not SVG, you can use Astro's Image component.
130131

131132
- Add your logo inside `src/assets` directory. (eg: `src/assets/dummy-logo.png`)
132-
- Import `Image` and your logo in `src/components/Header.astro`
133+
- Import `Image` and your logo in `Header.astro`
133134

134-
```astro
135+
```astro file=src/components/Header.astro
135136
---
136-
// other imports
137+
// ...
137138
import { Image } from "astro:assets";
138139
import dummyLogo from "@/assets/dummy-logo.png";
139140
---
@@ -155,11 +156,11 @@ With this approach, you can still adjust your image's appearance using CSS class
155156

156157
## Configuring social links
157158

158-
You can configure social links in `SOCIALS` object inside `src/constants.ts`.
159-
160159
![An arrow pointing at social link icons](https://github.com/user-attachments/assets/8b895400-d088-442f-881b-02d2443e00cf)
161160

162-
```ts
161+
You can configure social links in `SOCIALS` object inside `constants.ts`.
162+
163+
```ts file=src/constants.ts
163164
export const SOCIALS = [
164165
{
165166
name: "Github",

0 commit comments

Comments
 (0)