Skip to content

Commit 9253144

Browse files
committed
返回使用 shiki 代码高亮
1 parent 7f51574 commit 9253144

File tree

5 files changed

+92
-332
lines changed

5 files changed

+92
-332
lines changed

astro.config.mjs

Lines changed: 64 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,26 @@ import path from 'node:path';
33
import { fileURLToPath } from 'node:url';
44
import { rehypeHeadingIds } from '@astrojs/markdown-remark';
55
import remarkWikiLink from "@braindb/remark-wiki-link";
6-
import expressiveCode from 'astro-expressive-code';
6+
// import expressiveCode from 'astro-expressive-code';
77
import icon from 'astro-icon';
88
import { defineConfig } from 'astro/config';
99
import fontCarrier from 'font-carrier';
1010
// Others
1111
// import { visualizer } from 'rollup-plugin-visualizer'
12-
import rehypeKatex from 'rehype-katex'
13-
import remarkBreaks from 'remark-breaks'
12+
import rehypeKatex from 'rehype-katex';
13+
import remarkBreaks from 'remark-breaks';
1414
import remarkMath from 'remark-math';
1515

16+
17+
1618
import AstroPureIntegration from './packages/pure/index.ts';
1719
// Local integrations
1820
// Local rehype & remark plugins
1921
import rehypeAutolinkHeadings from './src/plugins/rehype-auto-link-headings.ts';
2022
import { remarkAiNotice } from './src/plugins/remark-ai-notice.mjs';
2123
import { remarkMermaid } from './src/plugins/remark-mermaid';
24+
// Shiki
25+
import { addCopyButton, addLanguage, addTitle, transformerNotationDiff, transformerNotationHighlight, updateStyle } from './src/plugins/shiki-transformers.ts';
2226
import config from './src/site.config.ts';
2327

2428

@@ -27,70 +31,71 @@ const createFontSubsetIntegration = () => {
2731
name: 'font-subset-integration',
2832
hooks: {
2933
'astro:server:start': () => {
30-
const projectRoot = process.cwd();
31-
const fontPath = path.join(projectRoot, 'src', 'assets', 'fonts', 'crjk03w03.ttf');
32-
const outputDir = path.join(projectRoot, 'public', 'fonts');
33-
const outputPath = path.join(outputDir, 'crjk-subset.ttf');
34+
const projectRoot = process.cwd()
35+
const fontPath = path.join(projectRoot, 'src', 'assets', 'fonts', 'crjk03w03.ttf')
36+
const outputDir = path.join(projectRoot, 'public', 'fonts')
37+
const outputPath = path.join(outputDir, 'crjk-subset.ttf')
3438

3539
// To avoid slow startup, only copy the .ttf file in dev mode if it doesn't exist.
3640
// The browser will show 404s for woff/woff2 but will fall back to the ttf.
3741
if (fs.existsSync(outputPath)) {
38-
return;
42+
return
3943
}
4044

4145
if (!fs.existsSync(outputDir)) {
42-
fs.mkdirSync(outputDir, { recursive: true });
46+
fs.mkdirSync(outputDir, { recursive: true })
4347
}
4448

45-
fs.copyFileSync(fontPath, outputPath);
46-
console.log('Development font .ttf copied to public/fonts!');
49+
fs.copyFileSync(fontPath, outputPath)
50+
console.log('Development font .ttf copied to public/fonts!')
4751
},
4852
'astro:build:done': async ({ dir }) => {
49-
const projectRoot = process.cwd();
50-
const contentDir = path.join(projectRoot, 'src', 'content');
51-
const pagesDir = path.join(projectRoot, 'src', 'pages');
52-
const fontPath = path.join(projectRoot, 'src', 'assets', 'fonts', 'crjk03w03.ttf');
53-
const outputDir = fileURLToPath(new URL('./fonts', dir));
54-
const outputPath = path.join(outputDir, 'crjk-subset');
53+
const projectRoot = process.cwd()
54+
const contentDir = path.join(projectRoot, 'src', 'content')
55+
const pagesDir = path.join(projectRoot, 'src', 'pages')
56+
const fontPath = path.join(projectRoot, 'src', 'assets', 'fonts', 'crjk03w03.ttf')
57+
const outputDir = fileURLToPath(new URL('./fonts', dir))
58+
const outputPath = path.join(outputDir, 'crjk-subset')
5559

5660
if (!fs.existsSync(outputDir)) {
57-
fs.mkdirSync(outputDir, { recursive: true });
61+
fs.mkdirSync(outputDir, { recursive: true })
5862
}
5963

6064
function getAllFiles(dirPath, arrayOfFiles) {
61-
const files = fs.readdirSync(dirPath);
62-
arrayOfFiles = arrayOfFiles || [];
65+
const files = fs.readdirSync(dirPath)
66+
arrayOfFiles = arrayOfFiles || []
6367
files.forEach(function (file) {
6468
if (fs.statSync(path.join(dirPath, file)).isDirectory()) {
65-
arrayOfFiles = getAllFiles(path.join(dirPath, file), arrayOfFiles);
69+
arrayOfFiles = getAllFiles(path.join(dirPath, file), arrayOfFiles)
6670
} else {
6771
if (file.endsWith('.md') || file.endsWith('.mdx') || file.endsWith('.astro')) {
68-
arrayOfFiles.push(path.join(dirPath, file));
72+
arrayOfFiles.push(path.join(dirPath, file))
6973
}
7074
}
71-
});
72-
return arrayOfFiles;
75+
})
76+
return arrayOfFiles
7377
}
7478

75-
const contentFiles = getAllFiles(contentDir);
76-
const pagesFiles = getAllFiles(pagesDir);
77-
const allFiles = [...contentFiles, ...pagesFiles];
79+
const contentFiles = getAllFiles(contentDir)
80+
const pagesFiles = getAllFiles(pagesDir)
81+
const allFiles = [...contentFiles, ...pagesFiles]
7882

79-
const noticeTextForSubsetting = 'AI-Assisted Content This article is AI-assisted, and the author has strived for accuracy. Please use with discretion. Posted today Posted 1 day ago Posted {days} days ago 本文由AI辅助生成,作者已尽力确保内容准确,请谨慎参考 发布于今天 发布于 1 天前 发布于 {days} 天前';
80-
let allText = noticeTextForSubsetting;
83+
const noticeTextForSubsetting =
84+
'AI-Assisted Content This article is AI-assisted, and the author has strived for accuracy. Please use with discretion. Posted today Posted 1 day ago Posted {days} days ago 本文由AI辅助生成,作者已尽力确保内容准确,请谨慎参考 发布于今天 发布于 1 天前 发布于 {days} 天前'
85+
let allText = noticeTextForSubsetting
8186
allFiles.forEach((file) => {
82-
allText += fs.readFileSync(file, 'utf-8');
83-
});
87+
allText += fs.readFileSync(file, 'utf-8')
88+
})
8489

85-
const font = fontCarrier.transfer(fontPath);
86-
font.min(allText);
87-
font.output({ path: outputPath });
90+
const font = fontCarrier.transfer(fontPath)
91+
font.min(allText)
92+
font.output({ path: outputPath })
8893

89-
console.log('Font subset created successfully in dist/fonts!');
90-
},
91-
},
92-
};
93-
};
94+
console.log('Font subset created successfully in dist/fonts!')
95+
}
96+
}
97+
}
98+
}
9499

95100
// https://astro.build/config
96101
export default defineConfig({
@@ -137,14 +142,14 @@ export default defineConfig({
137142
// return slug
138143
// }
139144
// }),
140-
expressiveCode(),
145+
// expressiveCode(),
141146
icon({
142147
include: {
143148
devicon: ['*']
144149
}
145150
}),
146151
AstroPureIntegration(config),
147-
createFontSubsetIntegration(),
152+
createFontSubsetIntegration()
148153
],
149154
// root: './my-project-directory',
150155

@@ -204,8 +209,23 @@ export default defineConfig({
204209
properties: { className: ['anchor'] },
205210
content: { type: 'text', value: '#' }
206211
}
207-
],
208-
]
212+
]
213+
],
214+
// https://docs.astro.build/en/guides/syntax-highlighting/
215+
shikiConfig: {
216+
themes: {
217+
light: 'github-light',
218+
dark: 'github-dark'
219+
},
220+
transformers: [
221+
transformerNotationDiff(),
222+
transformerNotationHighlight(),
223+
updateStyle(),
224+
addTitle(),
225+
addLanguage(),
226+
addCopyButton(2000)
227+
]
228+
}
209229
},
210230
experimental: {
211231
svg: true,
@@ -219,7 +239,7 @@ export default defineConfig({
219239
// })
220240
],
221241
server: {
222-
host: true,
242+
host: true
223243
// https: {
224244
// key: fs.readFileSync('./localhost+2-key.pem'),
225245
// cert: fs.readFileSync('./localhost+2.pem')

packages/pure/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ export default function AstroPureIntegration(opts: UserInputConfig): AstroIntegr
8080
},
8181
markdown: {
8282
remarkPlugins,
83-
rehypePlugins
83+
rehypePlugins,
8484
// rehypePlugins: [rehypeRtlCodeSupport()],
85-
// shikiConfig:
85+
shikiConfig:
8686
// Configure Shiki theme if the user is using the default github-dark theme.
87-
// config.markdown.shikiConfig.theme !== 'github-dark' ? {} : { theme: 'css-variables' }
87+
config.markdown.shikiConfig.theme !== 'github-dark' ? {} : { theme: 'css-variables' }
8888
},
8989
scopedStyleStrategy: 'where',
9090
// If not already configured, default to prefetching all links on hover.

0 commit comments

Comments
 (0)