@tnotesjs/core 0.1.14 → 0.1.16
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/README.md +87 -87
- package/dist/{chunk-FVKQPBPP.js → chunk-4MO7F7Q4.js} +2005 -1769
- package/dist/cli/index.js +1 -1
- package/dist/vitepress/config/index.js +212 -4
- package/package.json +1 -1
- package/types/config.ts +61 -61
- package/types/index.ts +11 -11
- package/types/note.ts +33 -33
- package/vitepress/assets/icons/icon__check.svg +3 -3
- package/vitepress/assets/icons/icon__clipboard.svg +7 -7
- package/vitepress/assets/icons/icon__close.svg +1 -1
- package/vitepress/assets/icons/icon__collapse.svg +1 -1
- package/vitepress/assets/icons/icon__confirm.svg +1 -1
- package/vitepress/assets/icons/icon__copy.svg +4 -4
- package/vitepress/assets/icons/icon__focus.svg +1 -1
- package/vitepress/assets/icons/icon__fold.svg +3 -3
- package/vitepress/assets/icons/icon__folder.svg +1 -1
- package/vitepress/assets/icons/icon__fullscreen.svg +1 -1
- package/vitepress/assets/icons/icon__fullscreen_exit.svg +1 -1
- package/vitepress/assets/icons/icon__github.svg +3 -3
- package/vitepress/assets/icons/icon__mindmap.svg +1 -1
- package/vitepress/assets/icons/icon__next.svg +1 -1
- package/vitepress/assets/icons/icon__number_gray.svg +1 -1
- package/vitepress/assets/icons/icon__number_purple.svg +1 -1
- package/vitepress/assets/icons/icon__prev.svg +1 -1
- package/vitepress/assets/icons/icon__restore.svg +1 -1
- package/vitepress/assets/icons/icon__rotate.svg +4 -4
- package/vitepress/assets/icons/icon__search.svg +1 -1
- package/vitepress/assets/icons/icon__setting.svg +1 -0
- package/vitepress/assets/icons/icon__sidebar_collapsed.svg +1 -1
- package/vitepress/assets/icons/icon__sidebar_opened.svg +1 -1
- package/vitepress/assets/icons/icon__totop.svg +5 -5
- package/vitepress/assets/icons/icon__vscode.svg +5 -5
- package/vitepress/assets/icons/icon__zoom_fit.svg +1 -1
- package/vitepress/assets/icons/icon__zoom_in.svg +1 -1
- package/vitepress/assets/icons/icon__zoom_out.svg +1 -1
- package/vitepress/assets/icons/icon__zoom_reset.svg +1 -1
- package/vitepress/assets/icons/index.ts +39 -38
- package/vitepress/components/BilibiliOutsidePlayer/BilibiliOutsidePlayer.vue +20 -20
- package/vitepress/components/CodeBlockFullscreen/CodeBlockFullscreen.vue +373 -373
- package/vitepress/components/CodeBlockFullscreen/index.ts +115 -115
- package/vitepress/components/CodeBlockFullscreen/styles.css +64 -64
- package/vitepress/components/Discussions/Discussions.module.scss +32 -32
- package/vitepress/components/Discussions/Discussions.vue +211 -211
- package/vitepress/components/EnWordList/EnWordList.module.scss +124 -124
- package/vitepress/components/EnWordList/EnWordList.vue +543 -543
- package/vitepress/components/EnWordList/RightClickMenu.module.scss +22 -22
- package/vitepress/components/EnWordList/RightClickMenu.vue +66 -66
- package/vitepress/components/Footprints/Footprints.module.scss +93 -93
- package/vitepress/components/Footprints/Footprints.vue +377 -377
- package/vitepress/components/Layout/AboutModal.module.scss +233 -233
- package/vitepress/components/Layout/AboutModal.vue +105 -105
- package/vitepress/components/Layout/AboutPanel.vue +270 -270
- package/vitepress/components/Layout/ContentCollapse.vue +603 -603
- package/vitepress/components/Layout/CustomSidebar.vue +878 -608
- package/vitepress/components/Layout/DocBeforeControls.vue +149 -149
- package/vitepress/components/Layout/DocFooter.vue +233 -233
- package/vitepress/components/Layout/ImagePreview.module.scss +201 -201
- package/vitepress/components/Layout/ImagePreview.vue +281 -281
- package/vitepress/components/Layout/Layout.module.scss +661 -661
- package/vitepress/components/Layout/Layout.vue +618 -560
- package/vitepress/components/Layout/NavBarSettingsTrigger.vue +59 -0
- package/vitepress/components/Layout/NoteStatus.vue +140 -140
- package/vitepress/components/Layout/SidebarItems.vue +1083 -268
- package/vitepress/components/Layout/SidebarNavBefore.vue +171 -114
- package/vitepress/components/Layout/Swiper.vue +167 -167
- package/vitepress/components/Layout/ToggleFullContent.module.scss +11 -11
- package/vitepress/components/Layout/ToggleFullContent.vue +35 -35
- package/vitepress/components/Layout/ToggleSidebar.module.scss +11 -11
- package/vitepress/components/Layout/ToggleSidebar.vue +36 -36
- package/vitepress/components/Layout/composables/useCollapseControl.ts +88 -88
- package/vitepress/components/Layout/composables/useNoteConfig.ts +119 -119
- package/vitepress/components/Layout/composables/useNoteSave.ts +178 -178
- package/vitepress/components/Layout/composables/useNoteValidation.ts +85 -85
- package/vitepress/components/Layout/composables/useRedirect.ts +113 -113
- package/vitepress/components/Layout/composables/useVSCodeIntegration.ts +85 -85
- package/vitepress/components/Layout/homeReadme.data.ts +124 -124
- package/vitepress/components/MarkMap/MarkMap.module.scss +159 -159
- package/vitepress/components/MarkMap/MarkMap.vue +405 -405
- package/vitepress/components/Mermaid/Mermaid.module.scss +275 -275
- package/vitepress/components/Mermaid/Mermaid.vue +365 -365
- package/vitepress/components/NotesTable/NotesTable.module.scss +77 -77
- package/vitepress/components/NotesTable/NotesTable.vue +98 -98
- package/vitepress/components/NotesTable/README.md +67 -67
- package/vitepress/components/Settings/Settings.module.scss +375 -433
- package/vitepress/components/Settings/Settings.vue +376 -306
- package/vitepress/components/SidebarCard/MindMapView.vue +484 -484
- package/vitepress/components/SidebarCard/NotesTrendChart.vue +108 -108
- package/vitepress/components/SidebarCard/SidebarCard.vue +946 -946
- package/vitepress/components/Tooltip/Tooltip.vue +70 -70
- package/vitepress/components/constants.ts +109 -91
- package/vitepress/components/notesConfig.data.ts +73 -73
- package/vitepress/components/sidebar.data.ts +59 -59
- package/vitepress/components/tnotes-config.data.ts +21 -21
- package/vitepress/components/utils.ts +26 -26
- package/vitepress/config/index.ts +169 -167
- package/vitepress/configs/constants.ts +26 -26
- package/vitepress/configs/head.config.ts +25 -25
- package/vitepress/configs/index.ts +9 -9
- package/vitepress/configs/markdown-it.d.ts +23 -23
- package/vitepress/configs/markdown.config.ts +366 -366
- package/vitepress/configs/theme.config.ts +108 -108
- package/vitepress/plugins/buildProgressPlugin.ts +434 -434
- package/vitepress/plugins/getNoteByConfigIdPlugin.ts +107 -107
- package/vitepress/plugins/renameNotePlugin.ts +68 -68
- package/vitepress/plugins/sidebarStructurePlugin.ts +260 -0
- package/vitepress/plugins/updateConfigPlugin.ts +63 -63
- package/vitepress/theme/index.ts +137 -137
- package/vitepress/theme/styles/base.scss +50 -50
- package/vitepress/theme/styles/components/404.scss +31 -31
- package/vitepress/theme/styles/components/collapse.scss +175 -175
- package/vitepress/theme/styles/components/markmap.scss +101 -101
- package/vitepress/theme/styles/components/swiper.scss +255 -255
- package/vitepress/theme/styles/index.scss +25 -25
- package/vitepress/theme/styles/layout.scss +70 -62
- package/vitepress/theme/styles/utilities.scss +39 -39
- package/vitepress/theme/styles/vitepress-override.scss +25 -25
|
@@ -1,108 +1,108 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* .vitepress/tnotes/vitepress/plugins/getNoteByConfigIdPlugin.ts
|
|
3
|
-
*
|
|
4
|
-
* VitePress 插件 - 根据 configId 查询笔记信息
|
|
5
|
-
*/
|
|
6
|
-
import { NoteIndexCache } from '../../core'
|
|
1
|
+
/**
|
|
2
|
+
* .vitepress/tnotes/vitepress/plugins/getNoteByConfigIdPlugin.ts
|
|
3
|
+
*
|
|
4
|
+
* VitePress 插件 - 根据 configId 查询笔记信息
|
|
5
|
+
*/
|
|
6
|
+
import { NoteIndexCache } from '../../core'
|
|
7
7
|
import { logger } from '../../utils'
|
|
8
|
-
|
|
9
|
-
import type { PluginOption } from 'vite'
|
|
10
|
-
|
|
11
|
-
export function getNoteByConfigIdPlugin(): PluginOption {
|
|
12
|
-
return {
|
|
13
|
-
name: 'tnotes-get-note-by-config-id',
|
|
14
|
-
|
|
15
|
-
configureServer(server) {
|
|
16
|
-
// 添加中间件处理查询请求
|
|
17
|
-
server.middlewares.use(async (req, res, next) => {
|
|
18
|
-
if (
|
|
19
|
-
req.url?.startsWith('/__tnotes_get_note?') &&
|
|
20
|
-
req.method === 'GET'
|
|
21
|
-
) {
|
|
22
|
-
try {
|
|
23
|
-
// 解析查询参数
|
|
24
|
-
const url = new URL(req.url, `http://${req.headers.host}`)
|
|
25
|
-
const configId = url.searchParams.get('configId')
|
|
26
|
-
|
|
27
|
-
if (!configId) {
|
|
28
|
-
res.statusCode = 400
|
|
29
|
-
res.setHeader('Content-Type', 'application/json')
|
|
30
|
-
res.end(
|
|
31
|
-
JSON.stringify({
|
|
32
|
-
success: false,
|
|
33
|
-
error: 'Missing configId parameter',
|
|
34
|
-
}),
|
|
35
|
-
)
|
|
36
|
-
return
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// 确保笔记索引缓存已初始化
|
|
40
|
-
const noteIndexCache = NoteIndexCache.getInstance()
|
|
41
|
-
if (!noteIndexCache.isInitialized()) {
|
|
42
|
-
res.statusCode = 503
|
|
43
|
-
res.setHeader('Content-Type', 'application/json')
|
|
44
|
-
res.end(
|
|
45
|
-
JSON.stringify({
|
|
46
|
-
success: false,
|
|
47
|
-
error: 'Service not initialized',
|
|
48
|
-
}),
|
|
49
|
-
)
|
|
50
|
-
return
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// 从索引缓存中查询笔记
|
|
54
|
-
const noteItem = noteIndexCache.getByConfigId(configId)
|
|
55
|
-
|
|
56
|
-
if (!noteItem) {
|
|
57
|
-
// 笔记不存在或已被删除
|
|
58
|
-
res.statusCode = 200
|
|
59
|
-
res.setHeader('Content-Type', 'application/json')
|
|
60
|
-
res.end(
|
|
61
|
-
JSON.stringify({
|
|
62
|
-
success: true,
|
|
63
|
-
found: false,
|
|
64
|
-
data: null,
|
|
65
|
-
}),
|
|
66
|
-
)
|
|
67
|
-
return
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// 返回笔记信息
|
|
71
|
-
res.statusCode = 200
|
|
72
|
-
res.setHeader('Content-Type', 'application/json')
|
|
73
|
-
res.end(
|
|
74
|
-
JSON.stringify({
|
|
75
|
-
success: true,
|
|
76
|
-
found: true,
|
|
77
|
-
data: {
|
|
78
|
-
noteIndex: noteItem.noteIndex,
|
|
79
|
-
folderName: noteItem.folderName,
|
|
80
|
-
// 构建笔记的完整 URL(包含 README)
|
|
81
|
-
url: `/notes/${encodeURIComponent(
|
|
82
|
-
noteItem.folderName,
|
|
83
|
-
)}/README`,
|
|
84
|
-
},
|
|
85
|
-
}),
|
|
86
|
-
)
|
|
87
|
-
|
|
88
|
-
logger.debug(
|
|
89
|
-
`查询笔记: configId=${configId}, noteIndex=${noteItem.noteIndex}`,
|
|
90
|
-
)
|
|
91
|
-
} catch (error) {
|
|
92
|
-
logger.error('查询笔记失败:', error)
|
|
93
|
-
res.statusCode = 500
|
|
94
|
-
res.setHeader('Content-Type', 'application/json')
|
|
95
|
-
res.end(
|
|
96
|
-
JSON.stringify({
|
|
97
|
-
success: false,
|
|
98
|
-
error: error instanceof Error ? error.message : String(error),
|
|
99
|
-
}),
|
|
100
|
-
)
|
|
101
|
-
}
|
|
102
|
-
} else {
|
|
103
|
-
next()
|
|
104
|
-
}
|
|
105
|
-
})
|
|
106
|
-
},
|
|
107
|
-
}
|
|
108
|
-
}
|
|
8
|
+
|
|
9
|
+
import type { PluginOption } from 'vite'
|
|
10
|
+
|
|
11
|
+
export function getNoteByConfigIdPlugin(): PluginOption {
|
|
12
|
+
return {
|
|
13
|
+
name: 'tnotes-get-note-by-config-id',
|
|
14
|
+
|
|
15
|
+
configureServer(server) {
|
|
16
|
+
// 添加中间件处理查询请求
|
|
17
|
+
server.middlewares.use(async (req, res, next) => {
|
|
18
|
+
if (
|
|
19
|
+
req.url?.startsWith('/__tnotes_get_note?') &&
|
|
20
|
+
req.method === 'GET'
|
|
21
|
+
) {
|
|
22
|
+
try {
|
|
23
|
+
// 解析查询参数
|
|
24
|
+
const url = new URL(req.url, `http://${req.headers.host}`)
|
|
25
|
+
const configId = url.searchParams.get('configId')
|
|
26
|
+
|
|
27
|
+
if (!configId) {
|
|
28
|
+
res.statusCode = 400
|
|
29
|
+
res.setHeader('Content-Type', 'application/json')
|
|
30
|
+
res.end(
|
|
31
|
+
JSON.stringify({
|
|
32
|
+
success: false,
|
|
33
|
+
error: 'Missing configId parameter',
|
|
34
|
+
}),
|
|
35
|
+
)
|
|
36
|
+
return
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// 确保笔记索引缓存已初始化
|
|
40
|
+
const noteIndexCache = NoteIndexCache.getInstance()
|
|
41
|
+
if (!noteIndexCache.isInitialized()) {
|
|
42
|
+
res.statusCode = 503
|
|
43
|
+
res.setHeader('Content-Type', 'application/json')
|
|
44
|
+
res.end(
|
|
45
|
+
JSON.stringify({
|
|
46
|
+
success: false,
|
|
47
|
+
error: 'Service not initialized',
|
|
48
|
+
}),
|
|
49
|
+
)
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// 从索引缓存中查询笔记
|
|
54
|
+
const noteItem = noteIndexCache.getByConfigId(configId)
|
|
55
|
+
|
|
56
|
+
if (!noteItem) {
|
|
57
|
+
// 笔记不存在或已被删除
|
|
58
|
+
res.statusCode = 200
|
|
59
|
+
res.setHeader('Content-Type', 'application/json')
|
|
60
|
+
res.end(
|
|
61
|
+
JSON.stringify({
|
|
62
|
+
success: true,
|
|
63
|
+
found: false,
|
|
64
|
+
data: null,
|
|
65
|
+
}),
|
|
66
|
+
)
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// 返回笔记信息
|
|
71
|
+
res.statusCode = 200
|
|
72
|
+
res.setHeader('Content-Type', 'application/json')
|
|
73
|
+
res.end(
|
|
74
|
+
JSON.stringify({
|
|
75
|
+
success: true,
|
|
76
|
+
found: true,
|
|
77
|
+
data: {
|
|
78
|
+
noteIndex: noteItem.noteIndex,
|
|
79
|
+
folderName: noteItem.folderName,
|
|
80
|
+
// 构建笔记的完整 URL(包含 README)
|
|
81
|
+
url: `/notes/${encodeURIComponent(
|
|
82
|
+
noteItem.folderName,
|
|
83
|
+
)}/README`,
|
|
84
|
+
},
|
|
85
|
+
}),
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
logger.debug(
|
|
89
|
+
`查询笔记: configId=${configId}, noteIndex=${noteItem.noteIndex}`,
|
|
90
|
+
)
|
|
91
|
+
} catch (error) {
|
|
92
|
+
logger.error('查询笔记失败:', error)
|
|
93
|
+
res.statusCode = 500
|
|
94
|
+
res.setHeader('Content-Type', 'application/json')
|
|
95
|
+
res.end(
|
|
96
|
+
JSON.stringify({
|
|
97
|
+
success: false,
|
|
98
|
+
error: error instanceof Error ? error.message : String(error),
|
|
99
|
+
}),
|
|
100
|
+
)
|
|
101
|
+
}
|
|
102
|
+
} else {
|
|
103
|
+
next()
|
|
104
|
+
}
|
|
105
|
+
})
|
|
106
|
+
},
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -1,68 +1,68 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* .vitepress/tnotes/vitepress/plugins/renameNotePlugin.ts
|
|
3
|
-
*
|
|
4
|
-
* Vite 插件 - 处理笔记重命名请求
|
|
5
|
-
*/
|
|
6
|
-
import { RenameNoteCommand } from '../../commands/note/RenameNoteCommand'
|
|
7
|
-
|
|
8
|
-
import type { PluginOption } from 'vite'
|
|
9
|
-
|
|
10
|
-
export function renameNotePlugin(): PluginOption {
|
|
11
|
-
const renameCommand = new RenameNoteCommand()
|
|
12
|
-
|
|
13
|
-
return {
|
|
14
|
-
name: 'tnotes-rename-note',
|
|
15
|
-
configureServer(server) {
|
|
16
|
-
server.middlewares.use(async (req, res, next) => {
|
|
17
|
-
// 只处理 POST 请求到 /__tnotes_rename_note
|
|
18
|
-
if (req.url === '/__tnotes_rename_note' && req.method === 'POST') {
|
|
19
|
-
let body = ''
|
|
20
|
-
|
|
21
|
-
req.on('data', (chunk) => {
|
|
22
|
-
body += chunk.toString()
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
req.on('end', async () => {
|
|
26
|
-
try {
|
|
27
|
-
const { noteIndex, newTitle } = JSON.parse(body)
|
|
28
|
-
|
|
29
|
-
if (!noteIndex || !newTitle) {
|
|
30
|
-
res.statusCode = 400
|
|
31
|
-
res.end('Missing noteIndex or newTitle')
|
|
32
|
-
return
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// 执行重命名
|
|
36
|
-
const startTime = Date.now()
|
|
37
|
-
await renameCommand.renameNote({ noteIndex, newTitle })
|
|
38
|
-
const duration = Date.now() - startTime
|
|
39
|
-
|
|
40
|
-
const newFolderName = `${noteIndex}. ${newTitle.trim()}`
|
|
41
|
-
const newUrl = `/notes/${encodeURIComponent(
|
|
42
|
-
newFolderName,
|
|
43
|
-
)}/README`
|
|
44
|
-
|
|
45
|
-
res.statusCode = 200
|
|
46
|
-
res.setHeader('Content-Type', 'application/json')
|
|
47
|
-
res.end(
|
|
48
|
-
JSON.stringify({
|
|
49
|
-
success: true,
|
|
50
|
-
duration,
|
|
51
|
-
newTitle,
|
|
52
|
-
newFolderName,
|
|
53
|
-
newUrl,
|
|
54
|
-
message: '重命名完成',
|
|
55
|
-
})
|
|
56
|
-
)
|
|
57
|
-
} catch (error) {
|
|
58
|
-
res.statusCode = 500
|
|
59
|
-
res.end(error instanceof Error ? error.message : 'Rename failed')
|
|
60
|
-
}
|
|
61
|
-
})
|
|
62
|
-
} else {
|
|
63
|
-
next()
|
|
64
|
-
}
|
|
65
|
-
})
|
|
66
|
-
},
|
|
67
|
-
}
|
|
68
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* .vitepress/tnotes/vitepress/plugins/renameNotePlugin.ts
|
|
3
|
+
*
|
|
4
|
+
* Vite 插件 - 处理笔记重命名请求
|
|
5
|
+
*/
|
|
6
|
+
import { RenameNoteCommand } from '../../commands/note/RenameNoteCommand'
|
|
7
|
+
|
|
8
|
+
import type { PluginOption } from 'vite'
|
|
9
|
+
|
|
10
|
+
export function renameNotePlugin(): PluginOption {
|
|
11
|
+
const renameCommand = new RenameNoteCommand()
|
|
12
|
+
|
|
13
|
+
return {
|
|
14
|
+
name: 'tnotes-rename-note',
|
|
15
|
+
configureServer(server) {
|
|
16
|
+
server.middlewares.use(async (req, res, next) => {
|
|
17
|
+
// 只处理 POST 请求到 /__tnotes_rename_note
|
|
18
|
+
if (req.url === '/__tnotes_rename_note' && req.method === 'POST') {
|
|
19
|
+
let body = ''
|
|
20
|
+
|
|
21
|
+
req.on('data', (chunk) => {
|
|
22
|
+
body += chunk.toString()
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
req.on('end', async () => {
|
|
26
|
+
try {
|
|
27
|
+
const { noteIndex, newTitle } = JSON.parse(body)
|
|
28
|
+
|
|
29
|
+
if (!noteIndex || !newTitle) {
|
|
30
|
+
res.statusCode = 400
|
|
31
|
+
res.end('Missing noteIndex or newTitle')
|
|
32
|
+
return
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// 执行重命名
|
|
36
|
+
const startTime = Date.now()
|
|
37
|
+
await renameCommand.renameNote({ noteIndex, newTitle })
|
|
38
|
+
const duration = Date.now() - startTime
|
|
39
|
+
|
|
40
|
+
const newFolderName = `${noteIndex}. ${newTitle.trim()}`
|
|
41
|
+
const newUrl = `/notes/${encodeURIComponent(
|
|
42
|
+
newFolderName,
|
|
43
|
+
)}/README`
|
|
44
|
+
|
|
45
|
+
res.statusCode = 200
|
|
46
|
+
res.setHeader('Content-Type', 'application/json')
|
|
47
|
+
res.end(
|
|
48
|
+
JSON.stringify({
|
|
49
|
+
success: true,
|
|
50
|
+
duration,
|
|
51
|
+
newTitle,
|
|
52
|
+
newFolderName,
|
|
53
|
+
newUrl,
|
|
54
|
+
message: '重命名完成',
|
|
55
|
+
})
|
|
56
|
+
)
|
|
57
|
+
} catch (error) {
|
|
58
|
+
res.statusCode = 500
|
|
59
|
+
res.end(error instanceof Error ? error.message : 'Rename failed')
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
} else {
|
|
63
|
+
next()
|
|
64
|
+
}
|
|
65
|
+
})
|
|
66
|
+
},
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* .vitepress/tnotes/vitepress/plugins/sidebarStructurePlugin.ts
|
|
3
|
+
*
|
|
4
|
+
* VitePress dev middleware - 处理语雀风格侧边栏的目录结构操作。
|
|
5
|
+
*/
|
|
6
|
+
import { FileWatcherService, NoteService, ReadmeService } from '../../services'
|
|
7
|
+
|
|
8
|
+
import type { PluginOption } from 'vite'
|
|
9
|
+
import type { IncomingMessage, ServerResponse } from 'http'
|
|
10
|
+
import type { NoteInfo } from '../../types'
|
|
11
|
+
|
|
12
|
+
interface JsonResponse {
|
|
13
|
+
success: boolean
|
|
14
|
+
message?: string
|
|
15
|
+
sidebarChanged?: boolean
|
|
16
|
+
redirectUrl?: string
|
|
17
|
+
createdNotes?: Array<{ index: string; dirName: string; link: string }>
|
|
18
|
+
deletedNoteIndexes?: string[]
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async function readJsonBody(req: IncomingMessage): Promise<any> {
|
|
22
|
+
let body = ''
|
|
23
|
+
|
|
24
|
+
await new Promise<void>((resolve, reject) => {
|
|
25
|
+
req.on('data', (chunk) => {
|
|
26
|
+
body += chunk.toString()
|
|
27
|
+
})
|
|
28
|
+
req.on('end', resolve)
|
|
29
|
+
req.on('error', reject)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
return body ? JSON.parse(body) : {}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function sendJson(
|
|
36
|
+
res: ServerResponse,
|
|
37
|
+
statusCode: number,
|
|
38
|
+
payload: JsonResponse,
|
|
39
|
+
): void {
|
|
40
|
+
res.statusCode = statusCode
|
|
41
|
+
res.setHeader('Content-Type', 'application/json')
|
|
42
|
+
res.end(JSON.stringify(payload))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function normalizePathname(url?: string): string {
|
|
46
|
+
return (url || '').split('?')[0]
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function normalizeGroupPath(value: unknown): string[] {
|
|
50
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
51
|
+
throw new Error('Missing groupPath')
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return value.map((item) => String(item))
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function normalizeCount(value: unknown): number {
|
|
58
|
+
const count = Number(value ?? 1)
|
|
59
|
+
if (!Number.isInteger(count) || count < 1 || count > 100) {
|
|
60
|
+
throw new Error('count must be an integer between 1 and 100')
|
|
61
|
+
}
|
|
62
|
+
return count
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function getCreatedNotePayload(notes: NoteInfo[]) {
|
|
66
|
+
return notes.map((note) => ({
|
|
67
|
+
index: note.index,
|
|
68
|
+
dirName: note.dirName,
|
|
69
|
+
link: `/notes/${encodeURIComponent(note.dirName)}/README`,
|
|
70
|
+
}))
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async function withSuspendedWatcher<T>(task: () => Promise<T>): Promise<T> {
|
|
74
|
+
const watcher = FileWatcherService.getInstance()
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
watcher?.suspend()
|
|
78
|
+
return await task()
|
|
79
|
+
} finally {
|
|
80
|
+
watcher?.unsuspend()
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function sidebarStructurePlugin(): PluginOption {
|
|
85
|
+
const noteService = NoteService.getInstance()
|
|
86
|
+
const readmeService = ReadmeService.getInstance()
|
|
87
|
+
|
|
88
|
+
async function createNotes(count: number) {
|
|
89
|
+
const notes = []
|
|
90
|
+
const usedIndexes = new Set<number>()
|
|
91
|
+
|
|
92
|
+
for (const note of noteService.getAllNotes()) {
|
|
93
|
+
const index = parseInt(note.index, 10)
|
|
94
|
+
if (!isNaN(index)) {
|
|
95
|
+
usedIndexes.add(index)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
for (let i = 0; i < count; i++) {
|
|
100
|
+
const note = await noteService.createNote({
|
|
101
|
+
title: 'new',
|
|
102
|
+
usedIndexes,
|
|
103
|
+
})
|
|
104
|
+
const index = parseInt(note.index, 10)
|
|
105
|
+
if (!isNaN(index)) {
|
|
106
|
+
usedIndexes.add(index)
|
|
107
|
+
}
|
|
108
|
+
notes.push(note)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return notes
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
name: 'tnotes-sidebar-structure',
|
|
116
|
+
configureServer(server) {
|
|
117
|
+
server.middlewares.use(async (req, res, next) => {
|
|
118
|
+
const pathname = normalizePathname(req.url)
|
|
119
|
+
const handledPaths = new Set([
|
|
120
|
+
'/__tnotes_sidebar_rename_group',
|
|
121
|
+
'/__tnotes_sidebar_delete_group',
|
|
122
|
+
'/__tnotes_sidebar_create_note',
|
|
123
|
+
'/__tnotes_sidebar_create_notes',
|
|
124
|
+
'/__tnotes_sidebar_delete_note',
|
|
125
|
+
'/__tnotes_sidebar_reorder',
|
|
126
|
+
])
|
|
127
|
+
|
|
128
|
+
if (!handledPaths.has(pathname)) {
|
|
129
|
+
next()
|
|
130
|
+
return
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (req.method !== 'POST') {
|
|
134
|
+
sendJson(res, 405, { success: false, message: 'Method Not Allowed' })
|
|
135
|
+
return
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
try {
|
|
139
|
+
const data = await readJsonBody(req)
|
|
140
|
+
|
|
141
|
+
const result = await withSuspendedWatcher(async () => {
|
|
142
|
+
if (pathname === '/__tnotes_sidebar_rename_group') {
|
|
143
|
+
const groupPath = normalizeGroupPath(data.groupPath)
|
|
144
|
+
const newTitle = String(data.newTitle || '')
|
|
145
|
+
await readmeService.renameGroupInReadme(groupPath, newTitle)
|
|
146
|
+
await readmeService.refreshHomeReadmeAndSidebar()
|
|
147
|
+
return {
|
|
148
|
+
success: true,
|
|
149
|
+
sidebarChanged: true,
|
|
150
|
+
message: '目录已重命名',
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (pathname === '/__tnotes_sidebar_delete_group') {
|
|
155
|
+
const groupPath = normalizeGroupPath(data.groupPath)
|
|
156
|
+
const noteIndexes = await readmeService.deleteGroupFromReadme(
|
|
157
|
+
groupPath,
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
for (const noteIndex of noteIndexes) {
|
|
161
|
+
await noteService.deleteNote(noteIndex)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
await readmeService.refreshHomeReadmeAndSidebar()
|
|
165
|
+
return {
|
|
166
|
+
success: true,
|
|
167
|
+
sidebarChanged: true,
|
|
168
|
+
redirectUrl: '/',
|
|
169
|
+
deletedNoteIndexes: noteIndexes,
|
|
170
|
+
message: '目录已删除',
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (pathname === '/__tnotes_sidebar_delete_note') {
|
|
175
|
+
const noteIndex = String(data.noteIndex || '')
|
|
176
|
+
if (!noteIndex) throw new Error('Missing noteIndex')
|
|
177
|
+
|
|
178
|
+
await readmeService.deleteNoteFromReadme(noteIndex)
|
|
179
|
+
await noteService.deleteNote(noteIndex)
|
|
180
|
+
await readmeService.refreshHomeReadmeAndSidebar()
|
|
181
|
+
|
|
182
|
+
return {
|
|
183
|
+
success: true,
|
|
184
|
+
sidebarChanged: true,
|
|
185
|
+
redirectUrl: '/',
|
|
186
|
+
deletedNoteIndexes: [noteIndex],
|
|
187
|
+
message: '笔记已删除',
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (pathname === '/__tnotes_sidebar_reorder') {
|
|
192
|
+
if (data.dragType === 'note') {
|
|
193
|
+
const noteIndex = String(data.noteIndex || '')
|
|
194
|
+
if (!noteIndex) throw new Error('Missing noteIndex')
|
|
195
|
+
|
|
196
|
+
if (data.targetType === 'group') {
|
|
197
|
+
await readmeService.moveNoteInReadme(noteIndex, {
|
|
198
|
+
targetGroupPath: normalizeGroupPath(data.targetGroupPath),
|
|
199
|
+
})
|
|
200
|
+
} else {
|
|
201
|
+
await readmeService.moveNoteInReadme(noteIndex, {
|
|
202
|
+
targetNoteIndex: String(data.targetNoteIndex || ''),
|
|
203
|
+
placement: data.placement === 'after' ? 'after' : 'before',
|
|
204
|
+
})
|
|
205
|
+
}
|
|
206
|
+
} else if (data.dragType === 'group') {
|
|
207
|
+
await readmeService.moveGroupInReadme(
|
|
208
|
+
normalizeGroupPath(data.groupPath),
|
|
209
|
+
normalizeGroupPath(data.targetGroupPath),
|
|
210
|
+
data.placement === 'after' ? 'after' : 'before',
|
|
211
|
+
)
|
|
212
|
+
} else {
|
|
213
|
+
throw new Error('Missing dragType')
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
await readmeService.refreshHomeReadmeAndSidebar()
|
|
217
|
+
return {
|
|
218
|
+
success: true,
|
|
219
|
+
sidebarChanged: true,
|
|
220
|
+
message: '排序已更新',
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const count = normalizeCount(data.count)
|
|
225
|
+
const notes = await createNotes(count)
|
|
226
|
+
|
|
227
|
+
if (data.targetNoteIndex) {
|
|
228
|
+
const placement = data.placement === 'after' ? 'after' : 'before'
|
|
229
|
+
await readmeService.insertNotesAroundNote(
|
|
230
|
+
String(data.targetNoteIndex),
|
|
231
|
+
notes,
|
|
232
|
+
placement,
|
|
233
|
+
)
|
|
234
|
+
} else {
|
|
235
|
+
const groupPath = normalizeGroupPath(data.groupPath)
|
|
236
|
+
await readmeService.insertNotesAtGroupStart(groupPath, notes)
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
await readmeService.refreshHomeReadmeAndSidebar()
|
|
240
|
+
|
|
241
|
+
return {
|
|
242
|
+
success: true,
|
|
243
|
+
sidebarChanged: true,
|
|
244
|
+
createdNotes: getCreatedNotePayload(notes),
|
|
245
|
+
message: '笔记已创建',
|
|
246
|
+
}
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
sendJson(res, 200, result)
|
|
250
|
+
} catch (error) {
|
|
251
|
+
console.error('侧边栏结构操作失败:', error)
|
|
252
|
+
sendJson(res, 500, {
|
|
253
|
+
success: false,
|
|
254
|
+
message: error instanceof Error ? error.message : String(error),
|
|
255
|
+
})
|
|
256
|
+
}
|
|
257
|
+
})
|
|
258
|
+
},
|
|
259
|
+
}
|
|
260
|
+
}
|