Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
added frontmatter to exported documents. fixed style annotations in e…
Browse files Browse the repository at this point in the history
…xport
  • Loading branch information
iskaktoltay committed Nov 5, 2024
1 parent 99f48e1 commit 8056cba
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
14 changes: 7 additions & 7 deletions frontend/packages/app/components/export-doc-button.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {HMBlockNode, toHMBlock} from '@mintter/shared'
import {HMBlockNode, getDocumentTitle, toHMBlock} from '@mintter/shared'
import {Button, Tooltip, toast} from '@mintter/ui'
import {Download} from '@tamagui/lucide-icons'
import {SizableText, YStack} from 'tamagui'
Expand All @@ -14,7 +14,7 @@ export const ExportDocButton = ({
version: string | undefined
}) => {
const pub = usePublication({id: docId, version: version})
const title = pub.data?.document?.title || 'document'
const title = getDocumentTitle(pub.data?.document)
const {exportDocument, openDirectory} = useAppContext()
return (
<>
Expand All @@ -27,13 +27,13 @@ export const ExportDocButton = ({
pub.data?.document?.children
const editorBlocks = toHMBlock(blocks)

const markdownWithFiles =
await convertBlocksToMarkdown(editorBlocks)
const markdownWithFiles = await convertBlocksToMarkdown(
editorBlocks,
pub.data!.document!,
)

const {markdownContent, mediaFiles} = markdownWithFiles
// Prepend the title as an H1 to the markdown content
const markdownWithTitle = `# ${title}\n\n${markdownContent}`
exportDocument(title, markdownWithTitle, mediaFiles)
exportDocument(title, markdownContent, mediaFiles)
.then((res) => {
const success = (
<>
Expand Down
6 changes: 2 additions & 4 deletions frontend/packages/app/pages/export-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,15 @@ export default function ExportPage() {
const editorBlocks = toHMBlock(blocks)
const {markdownContent, mediaFiles} = await convertBlocksToMarkdown(
editorBlocks,
doc.document!,
docMap,
)
const title = getDocumentTitle(doc.document)

// Prepend the title as an H1 to the markdown content
const markdownWithTitle = `# ${title}\n\n${markdownContent}`

return {
title,
markdown: {
markdownContent: markdownWithTitle,
markdownContent: markdownContent,
mediaFiles,
},
}
Expand Down
28 changes: 17 additions & 11 deletions frontend/packages/app/utils/blocks-to-markdown.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import {HMBlock} from '@mintter/shared'
import {HMBlock, HMDocument} from '@mintter/shared'
import rehypeParse from 'rehype-parse'
import rehypeRemark from 'rehype-remark'
import remarkGfm from 'remark-gfm'
import remarkStringify from 'remark-stringify'
import {unified} from 'unified'

function applyStyles(text, styles) {
if (styles.bold) text = `<strong>${text}</strong>`
if (styles.italic) text = `<em>${text}</em>`
if (styles.bold) text = `<b>${text}</b>`
if (styles.italic) text = `<i>${text}</i>`
if (styles.strike) text = `<del>${text}</del>`
if (styles.underline) text = `<u>${text}</u>`
if (styles.code) text = `<code>${text}</code>`
return text
}

Expand Down Expand Up @@ -137,7 +138,8 @@ async function extractMediaFiles(blocks: HMBlock[]) {
const placeholder = `file-${counter}`
mediaFiles.push({url, filename, placeholder})
counter++
block.props = {...block.props, url: `media/${placeholder}`} // Update the URL to point to the local media folder
// Update the URL to point to the local media folder
block.props = {...block.props, url: `media/${placeholder}`}
}
}
if (block.children) {
Expand All @@ -152,16 +154,17 @@ async function extractMediaFiles(blocks: HMBlock[]) {
return mediaFiles
}

// async function extractLinks(blocks: HMBlock[], blockMap: Map<string, {name: string; path: string}>) {
// const setUrls = async (block) => {
// if ()
// }
// }

export async function convertBlocksToMarkdown(
blocks: HMBlock[],
document: HMDocument,
docMap?: Map<string, {name: string; path: string}>,
) {
const frontMatter = `---
title: ${document.title || ''}
created_at: ${document.createTime}
---
`
const mediaFiles = await extractMediaFiles(blocks)
const markdownFile = await unified()
.use(rehypeParse, {fragment: true})
Expand All @@ -170,7 +173,10 @@ export async function convertBlocksToMarkdown(
.use(remarkGfm)
.use(remarkStringify)
.process(convertBlocksToHtml(blocks, docMap))
const markdownContent = markdownFile.value as string
const markdownContent = (frontMatter +
(document.title
? `# ${document.title}\n\n${markdownFile.value}`
: markdownFile.value)) as string
return {markdownContent, mediaFiles}
}

Expand Down

0 comments on commit 8056cba

Please sign in to comment.