Skip to content

Commit 98e7a64

Browse files
committed
Markdwon adds metadata, document navigation supports Chinese and English switching
1 parent 04fb378 commit 98e7a64

19 files changed

+166
-23
lines changed

darkit/core/web/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
static/docs/*
22
src/routes/docs/**/*.md
33
src/routes/docs/navigation.json
4+
src/routes/docs/navigation_zh.json
45

56
# ---- Svelte ----
67
node_modules

darkit/core/web/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"@xyflow/svelte": "^0.1.23",
5353
"codejar": "^4.2.0",
5454
"echarts": "^5.5.1",
55+
"front-matter": "^4.0.2",
5556
"mode-watcher": "^0.4.1",
5657
"prismjs": "^1.29.0",
5758
"svelte-sonner": "^0.3.28",

darkit/core/web/pnpm-lock.yaml

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

darkit/core/web/src/routes/docs/+layout.svelte

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
<script>
2-
import DocsNavbar from '$lib/components/docs-navbar.svelte';
32
import Navigation from './navigation.json';
3+
import ZhNavigation from './navigation_zh.json';
4+
import { languageTag } from '$lib/paraglide/runtime';
5+
import DocsNavbar from '$lib/components/docs-navbar.svelte';
46
57
let { children } = $props();
68
7-
const docsContents = Navigation.map((item) => {
8-
return {
9-
caption: item.title,
10-
contents: item.children.map((child) => {
11-
return [child.title, child.path];
12-
})
13-
};
14-
});
9+
const navigation = $derived(languageTag() === 'zh' ? ZhNavigation : Navigation);
10+
11+
const docsContents = $derived(
12+
navigation.map((item) => {
13+
return {
14+
caption: item.title,
15+
contents: item.children.map((child) => {
16+
const title = child.metadata.title || child.title;
17+
return [title, child.path];
18+
})
19+
};
20+
})
21+
);
1522
</script>
1623

1724
<DocsNavbar contents={docsContents} />

darkit/core/web/vite-plugin-docs.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import path from 'path';
22
import fs from 'fs/promises';
3+
import fm from 'front-matter';
34
import { watch, type FSWatcher } from 'fs';
45
import type { Plugin } from 'vite';
56

67
export const DOCS_NAVIGATION = 'navigation.json';
8+
export const ZH_DOCS_NAVIGATION = 'navigation_zh.json';
79

810
const ROOT_PATH = path.join(__dirname, '../../../');
911
const ROOT_STATIC_PATH = path.join(ROOT_PATH, 'static/docs');
@@ -18,41 +20,47 @@ type DocsTree =
1820
order: number;
1921
title: string;
2022
path: string;
23+
metadata: Record<string, any>;
2124
};
2225

2326
const isPathIn = (a: string, b: string) => {
2427
const relative = path.relative(b, a);
2528
return !relative.startsWith('..') && !path.isAbsolute(relative);
2629
};
2730

28-
const createDirectory = async (
31+
const createNavigation = async (
2932
source: string,
3033
root: string | undefined = undefined
3134
): Promise<DocsTree[]> => {
32-
const docsTree = [];
35+
const docsTree: DocsTree[] = [];
3336

3437
const files = await fs.readdir(source, { withFileTypes: true });
3538
for (const file of files) {
3639
//Generate a navigation from an en document
3740
const [order, title] = file.name.split('.');
38-
if (!order || !title) continue; // 跳过不符合“序号.标题”格式的文件或目录
41+
// 跳过不符合“序号.标题”格式的文件或目录
42+
if (!order || !title) continue;
3943

4044
if (file.isDirectory()) {
4145
const root2 = root ? `${root}/${title}` : title;
42-
const childrenTree = await createDirectory(path.join(source, file.name), root2);
46+
const childrenTree = await createNavigation(path.join(source, file.name), root2);
4347
docsTree.push({
4448
order: parseInt(order, 10),
4549
title: title.replace(/-/g, ' '),
4650
children: childrenTree
4751
});
4852
} else if (file.isFile() && file.name.endsWith('.md')) {
53+
const content = await fs.readFile(path.join(source, file.name), 'utf-8');
54+
const { attributes } = fm(content);
55+
4956
const title2 = title.replace('.md', '');
5057
// svelte router path
51-
const path = root ? `/docs/${root}/${title2}` : `/docs/${title2}`;
58+
const urlPath = root ? `/docs/${root}/${title2}` : `/docs/${title2}`;
5259
docsTree.push({
5360
order: parseInt(order, 10),
5461
title: title2.replace(/-/g, ' '),
55-
path: path
62+
path: urlPath,
63+
metadata: attributes as Record<string, any>
5664
});
5765
}
5866
}
@@ -137,7 +145,15 @@ export const docs = ({
137145
const sourcePath = path.join(__dirname, source);
138146
const targetPath = path.join(__dirname, target);
139147
const docStaticPath = path.join(__dirname, staticDir);
140-
const menusJSONPath = path.join(targetPath, DOCS_NAVIGATION);
148+
const navJSONPath = path.join(targetPath, DOCS_NAVIGATION);
149+
const zhNavJSONPath = path.join(targetPath, ZH_DOCS_NAVIGATION);
150+
151+
const i18nWriteNavigationFile = async () => {
152+
const enDocsTree = await createNavigation(path.join(ROOT_PATH, 'docs'));
153+
const zhDocsTree = await createNavigation(path.join(ROOT_PATH, 'docs', 'zh'));
154+
await fs.writeFile(navJSONPath, JSON.stringify(enDocsTree));
155+
await fs.writeFile(zhNavJSONPath, JSON.stringify(zhDocsTree));
156+
};
141157

142158
/** 复制静态文件 */
143159
const copyStaticFiles = async (files: [string, string][]) => {
@@ -175,8 +191,6 @@ export const docs = ({
175191

176192
/** 监听 docs 目录下的文件变化,实时更新 docs 目录 */
177193
const buildDocsPage = (source: string, target: string) => {
178-
const menusJSONPath = path.join(target, DOCS_NAVIGATION);
179-
180194
const events = new Set<string>();
181195
let timer: NodeJS.Timeout | null = null;
182196

@@ -189,8 +203,7 @@ export const docs = ({
189203
events.clear();
190204
console.debug(`Docs page ${Array.from(cache).join(',')} changed: rebuild...`);
191205
// 写入新的文档目录
192-
const docsTree = await createDirectory(source);
193-
await fs.writeFile(menusJSONPath, JSON.stringify(docsTree));
206+
await i18nWriteNavigationFile();
194207
cache.forEach(async (filename) => {
195208
const sourceFile = path.join(source, filename);
196209
if (filename.endsWith('.md')) {
@@ -226,8 +239,7 @@ export const docs = ({
226239
async config(config, { command }) {
227240
isDev = command === 'serve';
228241
// 写入新的文档目录
229-
const docsTree = await createDirectory(sourcePath);
230-
await fs.writeFile(menusJSONPath, JSON.stringify(docsTree));
242+
await i18nWriteNavigationFile();
231243
await removeDocsCache(targetPath);
232244
await copyDocsPage(sourcePath, targetPath);
233245
},

docs/1.Introduction/1.About.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
---
2+
title: About
3+
authors:
4+
- "Yesifan@[email protected]"
5+
updated: "2025-01-13"
6+
---
17
# About
28
DarwinKit is a PyTorch-based NLP toolkit that provides some commonly used NLP models and tools.
39

docs/2.User-guide/1.Installation-guide.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
---
2+
title: Installation Guide
3+
authors:
4+
- "Yesifan@[email protected]"
5+
updated: "2025-01-13"
6+
---
17
# Installation Guide
28

39
We recommend using Anaconda as the Python environment manager.

docs/2.User-guide/2.How-use-web.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
---
2+
title: How Use Web
3+
authors:
4+
- "Yesifan@[email protected]"
5+
updated: "2025-01-13"
6+
---
17
# WEB Tool
28

39
DarwinKit also provides a WEB tool to facilitate users to use DarwinKit more conveniently. The web tool is built into DarwinKit, and users can start the WEB service by running the `darkit start` command.

docs/2.User-guide/3.How-use-cli.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
---
2+
title: How Use CLI
3+
authors:
4+
- "Yesifan@[email protected]"
5+
updated: "2025-01-13"
6+
---
17
# CLI Tool
28

39
`DarwinKit` provides a CLI tool to facilitate easier usage of `DarwinKit`.

docs/2.User-guide/4.How-use-model.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
---
2+
title: How Use Model
3+
authors:
4+
- "Yesifan@[email protected]"
5+
updated: "2025-01-13"
6+
---
17
# Training and using models
28
This tutorial will introduce how to use SpikeGPT to train a text generation model and use the trained model to generate text.
39

0 commit comments

Comments
 (0)