Skip to content

Commit 7511aef

Browse files
committed
Fix markdown exporting
1 parent e9fe734 commit 7511aef

File tree

4 files changed

+29
-31
lines changed

4 files changed

+29
-31
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@
165165
"styled-components": "^4.3.2",
166166
"unified": "^9.1.0",
167167
"unist-util-visit": "^2.0.3",
168-
"use-force-update": "^1.0.7"
168+
"use-force-update": "^1.0.7",
169+
"yaml": "^1.10.0"
169170
},
170171
"build": {
171172
"appId": "com.boostio.boostnote",

src/components/organisms/NotePageToolbar.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ const NotePageToolbar = ({
171171
selectViewMode('preview')
172172
}, [selectViewMode])
173173

174+
const includeFrontMatter = preferences['markdown.includeFrontMatter']
175+
174176
const openExportContextMenu = useCallback(
175177
(event: React.MouseEvent<HTMLButtonElement>) => {
176178
event.preventDefault()
@@ -183,8 +185,8 @@ const NotePageToolbar = ({
183185
type: 'normal',
184186
label: 'Markdown export',
185187
click: async () =>
186-
await exportNoteAsMarkdownFile(note, pushMessage, {
187-
includeFrontMatter: preferences['markdown.includeFrontMatter'],
188+
await exportNoteAsMarkdownFile(note, {
189+
includeFrontMatter,
188190
}),
189191
},
190192
{
@@ -207,16 +209,15 @@ const NotePageToolbar = ({
207209
preferences,
208210
pushMessage,
209211
{
210-
includeFrontMatter:
211-
preferences['markdown.includeFrontMatter'],
212+
includeFrontMatter,
212213
},
213214
previewStyle
214215
),
215216
},
216217
],
217218
})
218219
},
219-
[note, preferences, previewStyle, pushMessage]
220+
[note, preferences, includeFrontMatter, previewStyle, pushMessage]
220221
)
221222

222223
const routeParams = useRouteParams()

src/lib/exports.ts

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import unified from 'unified'
22
import remarkParse from 'remark-parse'
33
import remarkRehype from 'remark-rehype'
4-
import remarkStringify from 'remark-stringify'
54
import remarkMath from 'remark-math'
65
import rehypeDocument from 'rehype-document'
76
import rehypeRaw from 'rehype-raw'
@@ -20,6 +19,7 @@ import remarkEmoji from 'remark-emoji'
2019
import rehypeReact from 'rehype-react'
2120
import CodeFence from '../components/atoms/markdown/CodeFence'
2221
import { getGlobalCss, selectTheme } from './styled/styleUtil'
22+
import yaml from 'yaml'
2323

2424
const filenamifyNoteTitle = function (noteTitle: string): string {
2525
return filenamify(noteTitle.toLowerCase().replace(/\s+/g, '-'))
@@ -28,11 +28,14 @@ const filenamifyNoteTitle = function (noteTitle: string): string {
2828
const getFrontMatter = (note: NoteDoc): string => {
2929
return [
3030
'---',
31-
`title: "${note.title}"`,
32-
`tags: "${note.tags.join()}"`,
31+
yaml
32+
.stringify({
33+
title: note.title,
34+
tags: note.tags,
35+
})
36+
.trim(),
3337
'---',
3438
'',
35-
'',
3639
].join('\n')
3740
}
3841

@@ -84,29 +87,17 @@ export const exportNoteAsHtmlFile = async (
8487

8588
export const exportNoteAsMarkdownFile = async (
8689
note: NoteDoc,
87-
pushMessage: (context: any) => any,
8890
{ includeFrontMatter }: { includeFrontMatter: boolean }
8991
): Promise<void> => {
90-
await unified()
91-
.use(remarkParse)
92-
.use(remarkStringify)
93-
.process(note.content, (err, file) => {
94-
if (err != null) {
95-
pushMessage({
96-
title: 'Note processing failed',
97-
description: 'Please check markdown syntax and try again later.',
98-
})
99-
return
100-
}
101-
let content = file.toString().trim() + '\n'
102-
content += includeFrontMatter ? getFrontMatter(note) : ''
103-
downloadString(
104-
content,
105-
`${filenamifyNoteTitle(note.title)}.md`,
106-
'text/markdown'
107-
)
108-
return
109-
})
92+
let content = note.content.trim() + '\n'
93+
if (includeFrontMatter) {
94+
content = getFrontMatter(note) + '\n' + content
95+
}
96+
downloadString(
97+
content,
98+
`${filenamifyNoteTitle(note.title)}.md`,
99+
'text/markdown'
100+
)
110101
return
111102
}
112103

0 commit comments

Comments
 (0)