onenote-cli 0.2.1-win32-x64 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +10 -0
- package/.gitattributes +7 -0
- package/.github/workflows/release.yml +170 -0
- package/.github/workflows/rust-npm-release.yml +109 -0
- package/.husky/pre-commit +1 -0
- package/.releaserc.json +15 -0
- package/.secretlintrc.json +3 -0
- package/CLAUDE.md +42 -0
- package/README.md +267 -0
- package/SKILL.md +77 -0
- package/docs/azure-app-registration.md +80 -0
- package/docs/development-notes.md +84 -0
- package/docs/graph-api-endpoints.md +131 -0
- package/docs/local-search-architecture.md +132 -0
- package/docs/onen0te-cli-analysis.md +130 -0
- package/docs/setup.md +141 -0
- package/docs/zhihu-intro.md +92 -0
- package/package.json +46 -12
- package/rustify/onenote +556 -0
- package/rustify/onenote-cli +556 -0
- package/rustify/package.json +3 -0
- package/src/auth.ts +359 -0
- package/src/cache.ts +1709 -0
- package/src/debug-tags.ts +207 -0
- package/src/export.ts +367 -0
- package/src/graph.ts +1480 -0
- package/src/index.ts +1176 -0
- package/src/markdown.ts +134 -0
- package/src/read-render.ts +621 -0
- package/src/sync-ui.tsx +220 -0
- package/tsconfig.json +16 -0
- package/bin/onenote-cli.exe +0 -0
package/docs/setup.md
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# onenote-cli Authentication Setup Guide
|
|
2
|
+
|
|
3
|
+
This guide walks you through setting up Azure AD authentication for `onenote-cli` to access Microsoft OneNote via the Graph API.
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
- A Microsoft account (personal or organizational)
|
|
8
|
+
- An Azure account with an active subscription — [create one for free](https://azure.microsoft.com/pricing/purchase-options/azure-account)
|
|
9
|
+
- [Bun](https://bun.sh) runtime installed
|
|
10
|
+
|
|
11
|
+
## Step 1: Register an Azure AD Application
|
|
12
|
+
|
|
13
|
+
1. Sign in to the [Microsoft Entra admin center](https://entra.microsoft.com)
|
|
14
|
+
2. Navigate to **Entra ID** > **App registrations** > **New registration**
|
|
15
|
+
3. Fill in the registration form:
|
|
16
|
+
- **Name**: `onenote-cli`
|
|
17
|
+
- **Supported account types**: Select **"Accounts in any organizational directory and personal Microsoft accounts"**
|
|
18
|
+
(This allows both work/school and personal Microsoft accounts)
|
|
19
|
+
- **Redirect URI**: Leave blank for now
|
|
20
|
+
4. Click **Register**
|
|
21
|
+
5. On the Overview page, copy the **Application (client) ID** — you will need this
|
|
22
|
+
|
|
23
|
+
## Step 2: Configure Platform for Device Code Flow
|
|
24
|
+
|
|
25
|
+
1. In your app registration, go to **Authentication** > **Add a platform**
|
|
26
|
+
2. Select **Mobile and desktop applications**
|
|
27
|
+
3. Check the box for `https://login.microsoftonline.com/common/oauth2/nativeclient`
|
|
28
|
+
4. Click **Configure**
|
|
29
|
+
5. Scroll down and set **Allow public client flows** to **Yes**
|
|
30
|
+
6. Click **Save**
|
|
31
|
+
|
|
32
|
+
> Device code flow requires "Allow public client flows" to be enabled. Without this, authentication will fail.
|
|
33
|
+
|
|
34
|
+
## Step 3: Configure API Permissions
|
|
35
|
+
|
|
36
|
+
1. In your app registration, go to **API permissions** > **Add a permission**
|
|
37
|
+
2. Select **Microsoft Graph** > **Delegated permissions**
|
|
38
|
+
3. Search and add the following permissions:
|
|
39
|
+
- `Notes.Read` — Read user OneNote notebooks
|
|
40
|
+
- `Notes.ReadWrite` — Read and write user OneNote notebooks
|
|
41
|
+
- `Notes.Read.All` — Read all notebooks the user can access
|
|
42
|
+
- `Notes.ReadWrite.All` — Read and write all notebooks the user can access
|
|
43
|
+
4. Click **Add permissions**
|
|
44
|
+
|
|
45
|
+
> For organizational accounts, an admin may need to **Grant admin consent** for these permissions.
|
|
46
|
+
|
|
47
|
+
## Step 4: Configure onenote-cli
|
|
48
|
+
|
|
49
|
+
You have two options to provide credentials:
|
|
50
|
+
|
|
51
|
+
### Option A: Environment variables (recommended)
|
|
52
|
+
|
|
53
|
+
Copy `.env.example` to `.env.local` and fill in your client ID:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
cp .env.example .env.local
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Edit `.env.local`:
|
|
60
|
+
|
|
61
|
+
```env
|
|
62
|
+
ONENOTE_CLIENT_ID=your-actual-client-id-here
|
|
63
|
+
ONENOTE_AUTHORITY=https://login.microsoftonline.com/common
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Option B: Config file
|
|
67
|
+
|
|
68
|
+
The CLI also reads from `~/.onenote-cli/config.json`. This file is auto-created on first run:
|
|
69
|
+
|
|
70
|
+
```json
|
|
71
|
+
{
|
|
72
|
+
"clientId": "your-actual-client-id-here",
|
|
73
|
+
"authority": "https://login.microsoftonline.com/common"
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
> Environment variables take priority over the config file.
|
|
78
|
+
|
|
79
|
+
## Step 5: Login
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
bun run src/index.ts login
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
This initiates the **device code flow**:
|
|
86
|
+
|
|
87
|
+
1. The CLI prints a URL and a code
|
|
88
|
+
2. Open the URL in your browser
|
|
89
|
+
3. Enter the code and sign in with your Microsoft account
|
|
90
|
+
4. Grant the requested permissions
|
|
91
|
+
5. Return to the terminal — you should see "Login successful!"
|
|
92
|
+
|
|
93
|
+
Tokens are cached at `~/.onenote-cli/msal-cache.json` so you don't need to login every time.
|
|
94
|
+
|
|
95
|
+
## Step 6: Verify
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
# List your notebooks
|
|
99
|
+
bun run src/index.ts notebooks list
|
|
100
|
+
|
|
101
|
+
# List all sections
|
|
102
|
+
bun run src/index.ts sections list
|
|
103
|
+
|
|
104
|
+
# List pages
|
|
105
|
+
bun run src/index.ts pages list
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Authority URL Reference
|
|
109
|
+
|
|
110
|
+
| Scenario | Authority URL |
|
|
111
|
+
|---|---|
|
|
112
|
+
| Multi-tenant + personal accounts | `https://login.microsoftonline.com/common` |
|
|
113
|
+
| Organizational accounts only (any tenant) | `https://login.microsoftonline.com/organizations` |
|
|
114
|
+
| Personal Microsoft accounts only | `https://login.microsoftonline.com/consumers` |
|
|
115
|
+
| Single tenant only | `https://login.microsoftonline.com/{tenant-id}` |
|
|
116
|
+
|
|
117
|
+
## Logout
|
|
118
|
+
|
|
119
|
+
To clear cached tokens:
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
bun run src/index.ts logout
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Troubleshooting
|
|
126
|
+
|
|
127
|
+
### "AADSTS7000218: The request body must contain the following parameter: 'client_assertion' or 'client_secret'"
|
|
128
|
+
|
|
129
|
+
→ Make sure **Allow public client flows** is set to **Yes** in your app's Authentication settings.
|
|
130
|
+
|
|
131
|
+
### "AADSTS65001: The user or administrator has not consented to use the application"
|
|
132
|
+
|
|
133
|
+
→ An admin needs to grant consent for the API permissions, or the user needs to consent during login.
|
|
134
|
+
|
|
135
|
+
### "AADSTS700016: Application with identifier '...' was not found"
|
|
136
|
+
|
|
137
|
+
→ Double-check that your `ONENOTE_CLIENT_ID` matches the Application (client) ID from Azure portal.
|
|
138
|
+
|
|
139
|
+
### Token expired
|
|
140
|
+
|
|
141
|
+
Tokens are automatically refreshed using the cached refresh token. If refresh fails, run `onenote login` again.
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# onenote-cli: 让你的 OneNote 笔记在 AI 时代存活下来
|
|
2
|
+
|
|
3
|
+
你的 OneNote 笔记本里藏着多少年的记忆?工作笔记、学习资料、读书笔记、灵感碎片……这些内容,AI 能帮你搜索吗?
|
|
4
|
+
|
|
5
|
+
答案是:现在可以了。
|
|
6
|
+
|
|
7
|
+
onenote-cli 是一个用 Bun + TypeScript 构建的命令行工具,通过 Microsoft Graph API 直接操作你的 OneNote 笔记本。最关键的功能:**全文搜索,精确到页面级别,点击 URL 直接跳转到匹配的那一页。**
|
|
8
|
+
|
|
9
|
+
## 为什么需要这个?
|
|
10
|
+
|
|
11
|
+
OneNote 的搜索功能只能在桌面端或网页端使用,无法被 AI 工具调用。当你想让 AI 帮你找一条几年前的笔记时,它做不到——因为 OneNote 没有暴露搜索 API 给第三方。
|
|
12
|
+
|
|
13
|
+
更糟的是,如果你的 OneDrive 里有超过 5000 个 OneNote 项目(笔记本+分区+分区组),微软的 Graph API 会直接返回 403 错误,连列出分区都做不到。
|
|
14
|
+
|
|
15
|
+
onenote-cli 解决了这两个问题。
|
|
16
|
+
|
|
17
|
+
## 它能做什么?
|
|
18
|
+
|
|
19
|
+
### 全文搜索(精确到页面)
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
$ onenote search "项目计划"
|
|
23
|
+
|
|
24
|
+
# (20240315) Q2 项目计划
|
|
25
|
+
Section: Work Notes | Notebook: My Notebook
|
|
26
|
+
...下季度**项目计划**:1. 完成用户认证模块重构 2. 上线新的推荐算法...
|
|
27
|
+
https://onenote.com/...?wd=target(...)
|
|
28
|
+
|
|
29
|
+
3 page-level results found.
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
搜索结果直接给出 OneNote Online 的页面级 URL,点击即可跳转到对应页面。不是打开整个分区,而是精确到那一页。
|
|
33
|
+
|
|
34
|
+
### 笔记本管理
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
$ onenote notebooks list
|
|
38
|
+
$ onenote sections list -n <notebook-id>
|
|
39
|
+
$ onenote pages create -s <section-id> -t "Meeting Notes" -b "<p>内容</p>"
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### 5000 项目限制的解决方案
|
|
43
|
+
|
|
44
|
+
当 Graph API 因为 SharePoint 文档库超过 5000 项而返回 403 时,onenote-cli 会:
|
|
45
|
+
|
|
46
|
+
1. 通过 OneDrive API 直接下载 `.one` 二进制文件
|
|
47
|
+
2. 从二进制中提取页面内容(支持 UTF-8 和 UTF-16LE,中日韩文字都能搜到)
|
|
48
|
+
3. 提取页面 GUID,通过 OneNote API 获取官方页面 URL
|
|
49
|
+
4. 构建本地缓存索引
|
|
50
|
+
|
|
51
|
+
### AI 集成
|
|
52
|
+
|
|
53
|
+
作为 Claude Code Skill 一键安装:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
$ npx skills add snomiao/onenote-cli
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
安装后,AI 可以直接搜索你的 OneNote 笔记,输出 Markdown 格式的结果:
|
|
60
|
+
|
|
61
|
+
```markdown
|
|
62
|
+
[Q2 项目计划](https://onenote.com/...?wd=target(...))
|
|
63
|
+
Work Notes | My Notebook
|
|
64
|
+
...下季度**项目计划**:1. 完成用户认证模块重构...
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## 技术亮点
|
|
68
|
+
|
|
69
|
+
- **设备代码流认证**:支持 SSH / 无头环境
|
|
70
|
+
- **.one 二进制解析**:从 MS-ONESTORE 格式中提取页面文本和 GUID
|
|
71
|
+
- **官方页面 URL**:通过 `/me/onenote/sections/0-{guid}/pages` 端点获取,绕过 OneNote Online 的会话缓存
|
|
72
|
+
- **跨目录运行**:`.env.local` 和缓存从包目录自动加载
|
|
73
|
+
|
|
74
|
+
## 开始使用
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
git clone https://github.com/snomiao/onenote-cli.git
|
|
78
|
+
cd onenote-cli
|
|
79
|
+
bun install
|
|
80
|
+
cp .env.example .env.local # 填入你的 Azure AD Client ID
|
|
81
|
+
bun run src/index.ts auth login
|
|
82
|
+
bun run src/index.ts sync
|
|
83
|
+
bun run src/index.ts search "你想找的内容"
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
详细的 Azure AD 配置步骤见 GitHub 仓库的 [docs/setup.md](https://github.com/snomiao/onenote-cli/blob/main/docs/setup.md)。
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
**GitHub**: https://github.com/snomiao/onenote-cli
|
|
91
|
+
|
|
92
|
+
MIT License | by snomiao
|
package/package.json
CHANGED
|
@@ -1,19 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "onenote-cli",
|
|
3
|
-
"version": "0.2.1
|
|
4
|
-
"description": "
|
|
5
|
-
"license": "MIT",
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "Make your OneNote notebooks survive in the age of AI",
|
|
6
5
|
"repository": {
|
|
7
6
|
"type": "git",
|
|
8
7
|
"url": "git+https://github.com/snomiao/onenote-cli.git"
|
|
9
8
|
},
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
"
|
|
18
|
-
|
|
9
|
+
"type": "module",
|
|
10
|
+
"bin": {
|
|
11
|
+
"onenote-cli": "rustify/onenote-cli",
|
|
12
|
+
"onenote": "rustify/onenote"
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"start": "bun run src/index.ts",
|
|
16
|
+
"lint": "oxlint .",
|
|
17
|
+
"lint:fix": "oxlint --fix .",
|
|
18
|
+
"typecheck": "bunx tsgo --noEmit"
|
|
19
|
+
},
|
|
20
|
+
"lint-staged": {
|
|
21
|
+
"**/*.{ts,tsx,js,jsx,mjs,cjs}": [
|
|
22
|
+
"oxlint --fix"
|
|
23
|
+
],
|
|
24
|
+
"**/*": "secretlint"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@azure/msal-node": "^2.16.0",
|
|
28
|
+
"@types/react": "^19.2.14",
|
|
29
|
+
"clipboardy": "^5.3.1",
|
|
30
|
+
"ink": "^7.0.1",
|
|
31
|
+
"pretty-bytes": "^7.1.0",
|
|
32
|
+
"react": "^19.2.5",
|
|
33
|
+
"yargs": "^17.7.2"
|
|
34
|
+
},
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"rustify": {
|
|
37
|
+
"bins": {
|
|
38
|
+
"onenote": "onenote-cli",
|
|
39
|
+
"onenote-cli": "onenote-cli"
|
|
40
|
+
},
|
|
41
|
+
"extra": [],
|
|
42
|
+
"launcher": 3,
|
|
43
|
+
"rootEnv": "ONENOTE_CLI_PACKAGE_ROOT"
|
|
44
|
+
},
|
|
45
|
+
"optionalDependencies": {
|
|
46
|
+
"onenote-cli-linux-x64": "npm:onenote-cli@0.2.1-linux-x64",
|
|
47
|
+
"onenote-cli-linux-arm64": "npm:onenote-cli@0.2.1-linux-arm64",
|
|
48
|
+
"onenote-cli-darwin-x64": "npm:onenote-cli@0.2.1-darwin-x64",
|
|
49
|
+
"onenote-cli-darwin-arm64": "npm:onenote-cli@0.2.1-darwin-arm64",
|
|
50
|
+
"onenote-cli-win32-x64": "npm:onenote-cli@0.2.1-win32-x64",
|
|
51
|
+
"onenote-cli-win32-arm64": "npm:onenote-cli@0.2.1-win32-arm64"
|
|
52
|
+
}
|
|
19
53
|
}
|