@yanglingfeng/md-web 0.1.0
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/LICENSE +21 -0
- package/README.md +129 -0
- package/dist/MarkdownEditor.d.ts +2 -0
- package/dist/MarkdownPreview.d.ts +6 -0
- package/dist/Toolbar.d.ts +10 -0
- package/dist/editorCommands.d.ts +10 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +1480 -0
- package/dist/index.js.map +1 -0
- package/dist/legacyDiagrams.d.ts +8 -0
- package/dist/remarkPlugins.d.ts +9 -0
- package/dist/style.css +2 -0
- package/dist/types.d.ts +43 -0
- package/dist/useScrollSync.d.ts +3 -0
- package/package.json +106 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ylfeng250
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# @yanglingfeng/md-web
|
|
2
|
+
|
|
3
|
+
接近马克飞象书写体验的 React Markdown 编辑器组件:左边专注写作,右边即时呈现。支持 GFM、公式、流程图、时序图、文内目录、脚注,以及图片粘贴上传。
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
## 安装
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @yanglingfeng/md-web
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
需要 React 18 或以上。样式会随组件入口自动引入;如果宿主打包器没有处理 CSS,再手动加一行:
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import '@yanglingfeng/md-web/style.css'
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## 使用
|
|
20
|
+
|
|
21
|
+
```tsx
|
|
22
|
+
import { useState } from 'react'
|
|
23
|
+
import { MarkdownEditor } from '@yanglingfeng/md-web'
|
|
24
|
+
|
|
25
|
+
export function EditorPage() {
|
|
26
|
+
const [value, setValue] = useState('# 新文档')
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<MarkdownEditor
|
|
30
|
+
value={value}
|
|
31
|
+
onChange={setValue}
|
|
32
|
+
height="100vh"
|
|
33
|
+
onImageUpload={async (file, signal) => {
|
|
34
|
+
const body = new FormData()
|
|
35
|
+
body.append('file', file)
|
|
36
|
+
const response = await fetch('/api/images', {
|
|
37
|
+
method: 'POST',
|
|
38
|
+
body,
|
|
39
|
+
signal,
|
|
40
|
+
})
|
|
41
|
+
const result = await response.json()
|
|
42
|
+
return { url: result.url, alt: file.name }
|
|
43
|
+
}}
|
|
44
|
+
/>
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
本地存储、上传接口都由宿主实现。组件本身不会写 `localStorage`。
|
|
50
|
+
|
|
51
|
+
### 常用属性
|
|
52
|
+
|
|
53
|
+
| 属性 | 说明 |
|
|
54
|
+
| --- | --- |
|
|
55
|
+
| `value` / `defaultValue` / `onChange` | 受控或非受控文档内容 |
|
|
56
|
+
| `mode` / `onModeChange` | `split`、`edit`、`preview` |
|
|
57
|
+
| `theme` | `earthsong`(默认)或 `light` |
|
|
58
|
+
| `height` | 数字像素或 CSS 高度,默认 `100vh` |
|
|
59
|
+
| `onImageUpload` | 粘贴 / 拖放图片时由宿主上传并返回 URL |
|
|
60
|
+
| `onError` | 图表渲染失败、上传失败等错误回调 |
|
|
61
|
+
| `renderStatus` | 自定义底栏:行号、列号、字数、字符数 |
|
|
62
|
+
| `shortcuts` | 覆盖加粗、链接、图片、预览、最大化、帮助等快捷键 |
|
|
63
|
+
| `readOnly` | 只读,禁用工具栏写入与快捷键改写 |
|
|
64
|
+
|
|
65
|
+
也单独导出 `MarkdownPreview`、`runEditorCommand` 以及相关类型。
|
|
66
|
+
|
|
67
|
+
## 功能
|
|
68
|
+
|
|
69
|
+
- CodeMirror 6 编辑内核:撤销重做、行号、自动换行、列表续行、Tab 缩进
|
|
70
|
+
- 工具栏与快捷键:加粗、标题、链接、图片、公式、表格等,支持再次点击取消格式
|
|
71
|
+
- 三种视图:分屏、最大化编辑器、纯预览;分栏可拖拽
|
|
72
|
+
- 预览语法:GFM 任务与表格、KaTeX 公式、代码高亮、`[TOC]`、脚注、`@(笔记本)[标签]`
|
|
73
|
+
- 图表:` ```mermaid `、` ```flow `(flowchart.js)、` ```sequence `(js-sequence-diagrams),默认按自然尺寸展示,可点击放大
|
|
74
|
+
- 目录与同步滚动:右侧迷你工具条打开文档目录,分屏时编辑区与预览双向对齐
|
|
75
|
+
- 安全渲染:禁用原始 HTML、链接协议白名单;图表在无脚本权限的 iframe 沙箱中绘制
|
|
76
|
+
|
|
77
|
+
## 预览语法
|
|
78
|
+
|
|
79
|
+
除了常见 Markdown 与 GFM,还支持马克飞象风格的扩展写法:
|
|
80
|
+
|
|
81
|
+
````md
|
|
82
|
+
@(示例笔记本)[写作|帮助]
|
|
83
|
+
|
|
84
|
+
[TOC]
|
|
85
|
+
|
|
86
|
+
正文里可以加脚注[^1]。
|
|
87
|
+
|
|
88
|
+
```flow
|
|
89
|
+
st=>start: Start
|
|
90
|
+
e=>end
|
|
91
|
+
st->e
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
```sequence
|
|
95
|
+
Alice->Bob: Hello
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
[^1]: 脚注说明。
|
|
99
|
+
````
|
|
100
|
+
|
|
101
|
+
流程图与时序图会先改写成等价的 Mermaid,再在沙箱 iframe 中渲染。图表默认缩小展示,悬停后可放大查看。
|
|
102
|
+
|
|
103
|
+
## 本地开发
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
npm install
|
|
107
|
+
npm run dev
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
质量检查与构建:
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
npm run typecheck
|
|
114
|
+
npm run lint
|
|
115
|
+
npm test
|
|
116
|
+
npm run build # 打包 npm 库到 dist/
|
|
117
|
+
npm run build:demo # 打包演示站到 dist-demo/
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
演示页会把草稿防抖写入 `localStorage`,这只存在于本仓库的 App,不会打进 npm 包。
|
|
121
|
+
|
|
122
|
+
## 发布
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
npm login
|
|
126
|
+
npm publish
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
`prepublishOnly` 会先跑类型检查、测试和库构建。
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { MarkdownEditorProps } from './types';
|
|
2
|
+
export declare function MarkdownEditor({ value, defaultValue, onChange, mode: modeProp, onModeChange, readOnly, height, theme, className, ariaLabel, shortcuts, onImageUpload, onError, renderStatus, }: MarkdownEditorProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { EditorCommand, EditorMode } from './types';
|
|
2
|
+
interface ToolbarProps {
|
|
3
|
+
mode: EditorMode;
|
|
4
|
+
readOnly: boolean;
|
|
5
|
+
onCommand: (command: EditorCommand) => void;
|
|
6
|
+
onModeChange: (mode: EditorMode) => void;
|
|
7
|
+
onDownload: () => void;
|
|
8
|
+
}
|
|
9
|
+
export declare function Toolbar({ mode, readOnly, onCommand, onModeChange, onDownload, }: ToolbarProps): import("react").JSX.Element;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { EditorView } from '@codemirror/view';
|
|
2
|
+
import { EditorCommand } from './types';
|
|
3
|
+
interface Transformation {
|
|
4
|
+
text: string;
|
|
5
|
+
selectionFrom: number;
|
|
6
|
+
selectionTo: number;
|
|
7
|
+
}
|
|
8
|
+
export declare function transformSelection(command: EditorCommand, selectedText: string): Transformation;
|
|
9
|
+
export declare function runEditorCommand(view: EditorView | null, command: EditorCommand): boolean;
|
|
10
|
+
export {};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { MarkdownEditor } from './MarkdownEditor';
|
|
2
|
+
export { MarkdownPreview } from './MarkdownPreview';
|
|
3
|
+
export { runEditorCommand, transformSelection } from './editorCommands';
|
|
4
|
+
export type { EditorCommand, EditorMode, EditorTheme, ImageUploadResult, MarkdownEditorProps, MarkdownEditorShortcuts, TocItem, } from './types';
|