Skip to content

Commit 9d1a9cf

Browse files
committed
OK
1 parent 98c9cd7 commit 9d1a9cf

File tree

4 files changed

+78
-16
lines changed

4 files changed

+78
-16
lines changed

docs/en/guide/docs/assistant.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Assistant
2+
13
The `Assistant` module provides a powerful API that allows users to request structured JSON data from the assistant. This functionality can be used for automation tasks such as extracting bill details, categorizing expenses, or parsing text data.
24

35
## `isAvailable` Variable
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
title: Example
3+
---
4+
5+
```tsx
6+
import { Script } from "scripting";
7+
8+
async function run() {
9+
console.present().then(() => {
10+
Script.exit();
11+
});
12+
13+
console.log("Start to fetch contacts");
14+
try {
15+
const contacts = await Contact.fetchAllContacts();
16+
17+
const first = contacts.at(0);
18+
19+
if (!first) {
20+
console.log("No contacts found");
21+
} else {
22+
console.log("There are " + contacts.length + " contacts");
23+
24+
const name = [first.givenName, first.familyName].join(" ");
25+
26+
console.log("First contact name: " + name);
27+
}
28+
} catch (e) {
29+
console.error(e);
30+
}
31+
}
32+
33+
run();
34+
```

docs/zh/guide/docs/contact/example.mdx

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,36 @@
22
title: 示例
33
---
44

5-
``tsx
5+
```tsx
6+
import { Script } from "scripting"
67

7-
``
8+
async function run() {
9+
console.present().then(() => {
10+
Script.exit()
11+
})
12+
13+
console.log("Start to fetch contacts")
14+
try {
15+
const contacts = await Contact.fetchAllContacts()
16+
17+
const first = contacts.at(0)
18+
19+
if (!first) {
20+
console.log("No contacts found")
21+
} else {
22+
console.log("There are " + contacts.length + " contacts")
23+
24+
const name = [
25+
first.givenName,
26+
first.familyName
27+
].join(" ")
28+
29+
console.log("First contact name: " + name)
30+
}
31+
} catch (e) {
32+
console.error(e)
33+
}
34+
}
35+
36+
run()
37+
```

scripts/generate-example-mdx.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
const fs = require("fs");
22
const path = require("path");
33

4-
// 设置路径
5-
const exampleDir = path.join(__dirname, "docs", "example");
6-
const zhGuideDir = path.join(__dirname, "docs", "zh", "guide", "docs");
7-
const enGuideDir = path.join(__dirname, "docs", "en", "guide", "docs");
4+
// 获取项目根目录
5+
const projectRoot = path.resolve(__dirname, ".."); // 假设脚本在 /scripts 目录下
6+
const exampleDir = path.join(projectRoot, "docs", "example"); // 拼接成 /docs/example
7+
const zhGuideDir = path.join(projectRoot, "docs", "zh", "guide", "docs");
8+
const enGuideDir = path.join(projectRoot, "docs", "en", "guide", "docs");
9+
10+
// 打印路径调试
11+
console.log("Example Directory:", exampleDir);
812

913
// 递归遍历目录
1014
function processDirectory(dirPath, langDir, langTitle) {
11-
// 读取目录中的文件
1215
const files = fs.readdirSync(dirPath);
1316

14-
// 遍历目录中的所有文件
1517
files.forEach((file) => {
1618
const filePath = path.join(dirPath, file);
1719
const outputDir = path.join(langDir, path.relative(exampleDir, dirPath)); // 保持原目录结构
1820

19-
// 如果是文件夹,则递归
2021
if (fs.statSync(filePath).isDirectory()) {
2122
processDirectory(filePath, langDir, langTitle);
2223
} else if (file.endsWith(".tsx")) {
23-
// 如果是TSX文件,则生成MDX
2424
generateMdxFile(filePath, outputDir, langTitle);
2525
}
2626
});
2727
}
2828

29-
// 生成MDX文件的函数
3029
function generateMdxFile(filePath, langDir, langTitle) {
3130
const fileName = path.basename(filePath, ".tsx");
32-
const mdxContent = fs.readFileSync(filePath, "utf-8"); // 读取TSX文件内容
31+
const mdxContent = fs.readFileSync(filePath, "utf-8");
3332

34-
const title = langTitle; // 标题根据语言不同
33+
const title = langTitle;
3534
const mdx = `---
3635
title: ${title}
3736
---
@@ -41,15 +40,12 @@ ${mdxContent}
4140
\`\`\`
4241
`;
4342

44-
// 确保目录存在
4543
if (!fs.existsSync(langDir)) {
4644
fs.mkdirSync(langDir, { recursive: true });
4745
}
4846

49-
// 输出的mdx文件路径
5047
const outputPath = path.join(langDir, fileName + ".mdx");
5148

52-
// 将生成的MDX内容写入文件
5349
fs.writeFileSync(outputPath, mdx);
5450
console.log(`Generated: ${outputPath}`);
5551
}

0 commit comments

Comments
 (0)