使用 Starlight 构建个人知识库
介绍
Starlight 是一个基于 Astro 框架构建的全功能文档主题。
安装
官方文档教程:Starlight - 开始使用 - 快速入门。
具体步骤
-
在终端中运行以下命令来创建一个新的 Astro + Starlight 项目:
终端窗口 1npm create astro@latest -- --template starlight终端窗口 1yarn create astro@latest --template starlight终端窗口 1pnpm create astro@latest --template starlight -
根据提示补充信息:
输出 1astro Launch sequence initiated.23dir Where should we create your new project?4./galactic-galaxy5◼ tmpl Using starlight as project template67ts Do you plan to write TypeScript?8Yes910use How strict should TypeScript be?11Strict1213deps Install dependencies?14Yes1516git Initialize a new git repository?17Yes1819✔ Project initialized!20■ Template copied21■ TypeScript customized22■ Dependencies installed23■ Git initialized2411 collapsed lines25next Liftoff confirmed. Explore your project!2627Enter your project directory using cd ./galactic-galaxy28Run pnpm dev to start the dev server. CTRL+C to stop.29Add frameworks like react or tailwind using astro add.3031Stuck? Join us at https://astro.build/chat3233╭─────╮ Houston:34│ ◠ ◡ ◠ Good luck out there, astronaut! 🚀35╰──🍫─╯ -
启动开发服务器:
终端窗口 1npm run dev终端窗口 1yarn run dev终端窗口 1pnpm run dev
个性化配置
配置为中文单语言网站
官方文档教程:Starlight - 国际化 (i18n) - 配置 i18n - 使用root 语言 - 单语言网站
1// @ts-check2import { defineConfig } from 'astro/config';3import starlight from '@astrojs/starlight';4
5// https://astro.build/config6export default defineConfig({7 integrations: [8 starlight({9 ...,10 locales: {11 root: {12 label: '简体中文',13 lang: 'zh-CN',14 },15 },16 ...,17 }),18 ],19});
翻译博客 UI
官方文档关于翻译 UI 的逐步教程:Starlight - 国际化 (i18n) - 翻译 Starlight 的 UI
-
在
src/content/config.ts
中配置i18n
数据集合:src/content/config.ts 1import { defineCollection } from 'astro:content';2import { docsSchema, i18nSchema } from '@astrojs/starlight/schema';34export const collections = {5docs: defineCollection({ schema: docsSchema() }),6i18n: defineCollection({ type: 'data', schema: i18nSchema() }),7}; -
为简体中文在
src/content/i18n/
中创建一个 JSON 翻译文件:文件夹src/
文件夹content/
文件夹i18n/
- zh-CN.json
-
添加翻译:
src/content/i18n/zh-CN.json 1{2"expressiveCode.copyButtonCopied": "已复制!",3"expressiveCode.copyButtonTooltip": "复制到剪贴板",4"expressiveCode.terminalWindowFallbackTitle": "终端窗口"5}
修复代码块选中时的背景颜色与 ins 块背景颜色难以区分的问题
4 collapsed lines
1// @ts-check2import { defineConfig } from 'astro/config';3import starlight from '@astrojs/starlight';4
5// https://astro.build/config6export default defineConfig({7 integrations: [8 starlight({9 ...,10 expressiveCode: {11 useThemedSelectionColors: true,12 styleOverrides: {13 codeSelectionBackground: '#0366d625', // 代码块选中时的背景颜色:半透明天蓝色14 }15 },16 ...,17 }),18 ],19});
安装插件
Starlight Blog
Starlight Blog 文档逐步安装教程:Starlight Blog - Installation。
翻译 Starlight Blog UI
Starlight Blog 文档中扩展的翻译 schema:Starlight Blog - Guides - Internationalization (i18n) - Translate the blog UI
1{2 "expressiveCode.copyButtonCopied": "已复制!",3 "expressiveCode.copyButtonTooltip": "复制到剪贴板",4 "expressiveCode.terminalWindowFallbackTitle": "终端窗口",5 "starlightBlog.authors.count_one": "{{author}} 的 {{count}} 篇文章",6 "starlightBlog.authors.count_other": "{{author}} 的 {{count}} 篇文章",7 "starlightBlog.pagination.prev": "较新的文章",8 "starlightBlog.pagination.next": "更早的文章",9 "starlightBlog.post.lastUpdate": " - 最后更新:{{date}}",10 "starlightBlog.post.draft": "草稿",11 "starlightBlog.post.featured": "精选",12 "starlightBlog.post.tags": "标签:",13 "starlightBlog.rss.imageFallback": "原始图片可在博文中查看。",14 "starlightBlog.sidebar.all": "所有文章",15 "starlightBlog.sidebar.featured": "精选文章",16 "starlightBlog.sidebar.recent": "最近文章",17 "starlightBlog.sidebar.tags": "标签",18 "starlightBlog.sidebar.authors": "作者",19 "starlightBlog.sidebar.rss": "RSS",20 "starlightBlog.tags.count_one": "有标签“{{tag}}” 的 {{count}} 篇文章",21 "starlightBlog.tags.count_other": "有标签“{{tag}}” 的 {{count}} 篇文章"22}
ExpressiveCode 的可选插件两则
书写体验
使用 Prettier 格式化
在 Astro 中使用 Prettier 格式化:Astro Tips - Tips - Setting up Prettier
-
安装
prettier
和prettier-plugin-astro
终端窗口 1npm i -D prettier prettier-plugin-astro终端窗口 1yarn add -D prettier prettier-plugin-astro终端窗口 1pnpm add -D prettier prettier-plugin-astro -
在项目根目录创建
.prettierrc
配置文件:.prettierrc 1{2"printWidth": 100,3"semi": true,4"singleQuote": true,5"tabWidth": 2,6"trailingComma": "es5",7"useTabs": false,8"plugins": ["prettier-plugin-astro"],9"overrides": [10{11"files": ["*.md", "*.mdx"],12"options": {13"printWidth": 8014}15}16]17}