Skip to content

Commit 857739a

Browse files
committed
add demo 支持
1 parent cad0ac5 commit 857739a

File tree

6 files changed

+147
-44
lines changed

6 files changed

+147
-44
lines changed

src/components/editor/WechatEditor.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { Button } from '@/components/ui/button'
2424
import { Copy } from 'lucide-react'
2525
import { MobileEditor } from './components/MobileEditor'
2626
import { DesktopEditor } from './components/DesktopEditor'
27+
import { getExampleContent } from '@/lib/utils/loadExampleContent'
2728

2829
export default function WechatEditor() {
2930
const { toast } = useToast()
@@ -171,7 +172,8 @@ export default function WechatEditor() {
171172
description: "当前文章未保存,是否继续?",
172173
action: (
173174
<ToastAction altText="继续" onClick={() => {
174-
setValue('# 新文章\n\n开始写作...')
175+
const exampleContent = getExampleContent()
176+
setValue(exampleContent)
175177
setIsDraft(false)
176178
}}>
177179
继续
@@ -182,7 +184,8 @@ export default function WechatEditor() {
182184
return
183185
}
184186

185-
setValue('# 新文章\n\n开始写作...')
187+
const exampleContent = getExampleContent()
188+
setValue(exampleContent)
186189
setIsDraft(false)
187190
}, [isDraft, toast])
188191

@@ -262,6 +265,15 @@ export default function WechatEditor() {
262265
})
263266
} else if (savedContent) {
264267
setValue(savedContent)
268+
} else {
269+
// 如果没有保存的内容或草稿,则加载示例内容
270+
const exampleContent = getExampleContent()
271+
setValue(exampleContent)
272+
toast({
273+
title: "欢迎使用 NeuraPress",
274+
description: "已加载示例内容,您可以开始编辑或查看效果",
275+
duration: 3000,
276+
})
265277
}
266278
}, [toast, handleDiscardDraft])
267279

src/example-v2.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,4 @@ function fancyAlert(arg) {
182182
用双波浪号包裹文字(例如 ~~像这样~~)会显示为删除效果。
183183
```
184184
**渲染效果:**
185-
~~像这样~~
186-
187-
188-
**脚注**
189-
[^1]: 我的参考内容。 ↩
185+
~~像这样~~

src/lib/markdown/parser.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { marked } from 'marked'
22
import type { RendererOptions } from './types'
33
import { MarkdownRenderer } from './renderer'
44
import { baseStylesToString } from './styles'
5+
import type { Tokens } from 'marked'
56

67
export class MarkdownParser {
78
private options: RendererOptions
@@ -36,32 +37,6 @@ export class MarkdownParser {
3637
}
3738
})
3839

39-
// 添加脚注支持
40-
const options = this.options // 在闭包中保存 options 引用
41-
marked.use({
42-
extensions: [{
43-
name: 'footnote',
44-
level: 'inline',
45-
start(src: string) {
46-
const match = src.match(/^\[\^([^\]]+)\]/)
47-
return match ? match.index : undefined
48-
},
49-
tokenizer(src: string) {
50-
const match = /^\[\^([^\]]+)\]/.exec(src)
51-
if (match) {
52-
const token = {
53-
type: 'footnote',
54-
raw: match[0],
55-
text: match[1],
56-
tokens: []
57-
}
58-
return token as any
59-
}
60-
return undefined
61-
}
62-
}]
63-
})
64-
6540
// 添加 Mermaid 支持
6641
marked.use({
6742
extensions: [{

src/lib/markdown/renderer.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -366,17 +366,10 @@ export class MarkdownRenderer {
366366

367367
// 添加删除线支持
368368
this.renderer.del = ({ text }: Tokens.Del) => {
369-
const delStyle = (this.options.inline?.del || {})
370-
const styleStr = cssPropertiesToString(delStyle)
369+
const styleOptions = (this.options.inline?.del || {})
370+
const styleStr = cssPropertiesToString(styleOptions)
371371
return `<del${styleStr ? ` style="${styleStr}"` : ''}>${text}</del>`
372372
}
373-
374-
// 添加脚注支持
375-
this.renderer.footnote = (token: any) => {
376-
const footnoteStyle = (this.options.inline?.footnote || {})
377-
const styleStr = cssPropertiesToString(footnoteStyle)
378-
return `<sup${styleStr ? ` style="${styleStr}"` : ''}><a href="#fn-${token.text}">[${token.text}]</a></sup>`
379-
}
380373
}
381374

382375
public getRenderer(): typeof marked.Renderer.prototype {

src/lib/utils/loadExampleContent.ts

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/**
2+
* Utility function to load the example.md content
3+
* This provides a default content for the editor when first opened
4+
*/
5+
6+
// Import the example.md content directly
7+
const exampleContent = `# NeuraPress 简介
8+
Markdown 转微信公众帐号内容神器,能让\`Markdown\`内容,无需作任何调整就能**一键复制**到微信公众号使用,特别针对代码展示做了优化。
9+
10+
11+
GitHub 地址:
12+
13+
[https://github.com/tianyaxiang/neurapress](https://github.com/tianyaxiang/neurapress)
14+
15+
> 使用微信公众号编辑器有一个十分头疼的问题——粘贴出来的代码,格式错乱,而且特别丑。
16+
17+
18+
19+
20+
### Markdown基本语法
21+
#### 标题
22+
支持6种大小的标题,分别对应\`#\`,\`##\`,\`###\`,\`####\`,\`#####\`,\`######\`,和样式文件中的\`h1,...,h6\`如:
23+
# H1
24+
## H2
25+
#### 行内代码
26+
如:\`AppCompatActivity\`类,markdown对行内代码的语法是前后用:\\\`,其中 \\\` 为windows键盘左上角那个,
27+
28+
#### 强调
29+
**我是强调**
30+
#### 斜体
31+
试试*斜体*
32+
#### 强调的斜体
33+
试试***强调的斜体***
34+
#### 删除
35+
试试 ~~删除~~
36+
#### 外链的超链接
37+
试试外链的超链接:[我是外链的超链接](https://www.leti.ltd),markdown对链接的语法为:\`[]()\`,如:\`[我是外链的超链接](https://www.leti.ltd)\`
38+
#### 页内的超链接
39+
试试页内的超链接:[我是页内的超链接](#jump_1),注:你先要在要跳转的到地方放置一个类似:\`<a id="jump_1">任意内容</a>\`的锚点。由\`id="jump_1" \`来匹配。
40+
41+
42+
#### 有序列表
43+
1. 有序列表 1
44+
2. 有序列表 2
45+
3. 有序列表 3
46+
47+
#### 无序列表
48+
- 无序列表 1
49+
- 无序列表 2
50+
- 无序列表 3
51+
52+
#### 引用块
53+
只需要在前面加 \`>\`,如下:
54+
>我是引用块
55+
微信公众号:孟晨
56+
欢迎关注我,一起学习,一起进步,做终身学习者!
57+
58+
#### 分隔线
59+
***
60+
61+
62+
### Markdown扩展语法
63+
64+
#### 表格
65+
| 班级 | 男生 | 女生 |
66+
|-----|-----|------|
67+
| 一(7)班 | 30 | 25 |
68+
| 一(8)班 | 25 | 30 |
69+
70+
71+
### 直接支持html,css
72+
如果你懂html和css,那下面这些效果就不在话下了:
73+
74+
<a href="#jump_1">来个页内跳转</a>,跳转到文未的:\`<a id="jump_1">我是页内跳转到的位置</a>\` ,对应:\`id="jump_1"\`
75+
<span style="color: #5bdaed; ">先给点颜色你看看</span>
76+
<span style="color: #AE87FA; ">再给点颜色你看看</span>
77+
<span style="font-size:1.3em;">试试改变字体大小</span>
78+
79+
<a id="jump_1">我是页内跳转到的位置</a>
80+
81+
### 任务列表
82+
- [x] 已完成任务
83+
- [ ] 未完成任务
84+
- [ ] 未完成任务2
85+
86+
### 数学公式
87+
使用两个美元符号包裹TeX或LaTeX格式的数学公式:
88+
89+
$$
90+
E=mc^2
91+
$$
92+
93+
### 代码块显示效果
94+
注:markdown对代码块的语法是开始和结束行都要添加:\\\`\\\`\\\`,其中 \\\` 为windows键盘左上角那个,如下:
95+
\`\`\`yaml
96+
kind: RoleBinding
97+
apiVersion: rbac.authorization.k8s.io/v1
98+
metadata:
99+
name: haimaxy-sa-rolebinding
100+
namespace: kube-system
101+
subjects:
102+
- kind: ServiceAccount
103+
name: haimaxy-sa
104+
namespace: kube-system
105+
roleRef:
106+
kind: Role
107+
name: haimaxy-sa-role
108+
apiGroup: rbac.authorization.k8s.io
109+
\`\`\`
110+
111+
112+
在页面头部提供了很多中**代码主题**风格,可以根据需要选择合适的即可。
113+
114+
### 用户体验
115+
- 支持自动保存
116+
- 支持多种主题
117+
- 支持快捷键
118+
`;
119+
120+
/**
121+
* Returns the example.md content
122+
*/
123+
export function getExampleContent(): string {
124+
return exampleContent;
125+
}

src/types/marked.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { marked } from 'marked';
22

33
declare module 'marked' {
4-
interface RendererThis {
5-
footnote: (token: any) => string;
4+
interface Renderer {
5+
}
6+
7+
namespace Tokens {
68
}
79
}

0 commit comments

Comments
 (0)