This repository has been archived by the owner on Jan 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added export markdown functionality to documents the user is an autho…
…r of
- Loading branch information
1 parent
a827011
commit 9856a42
Showing
10 changed files
with
192 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import {HMBlockNode, toHMBlock} from '@mintter/shared' | ||
import {Button, Tooltip} from '@mintter/ui' | ||
import {Download} from '@tamagui/lucide-icons' | ||
import {useAppContext} from '../app-context' | ||
import {usePublication} from '../models/documents' | ||
import {convertBlocksToMarkdown} from '../utils/blocks-to-markdown' | ||
|
||
export const ExportDocButton = ({ | ||
docId, | ||
version, | ||
}: { | ||
docId: string | undefined | ||
version: string | undefined | ||
}) => { | ||
const pub = usePublication({id: docId, version: version}) | ||
const title = pub.data?.document?.title || 'document' | ||
const {exportDocument} = useAppContext() | ||
return ( | ||
<> | ||
<Tooltip content={'Export Document to Markdown'}> | ||
<Button | ||
size="$2" | ||
theme="blue" | ||
onPress={async () => { | ||
const blocks: HMBlockNode[] | undefined = | ||
pub.data?.document?.children | ||
const editorBlocks = toHMBlock(blocks) | ||
exportDocument(title, await convertBlocksToMarkdown(editorBlocks)) | ||
}} | ||
icon={Download} | ||
> | ||
Export | ||
</Button> | ||
</Tooltip> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import {HMBlock} 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.strike) text = `<del>${text}</del>` | ||
if (styles.underline) text = `<u>${text}</u>` | ||
return text | ||
} | ||
|
||
function convertContentItemToHtml(contentItem) { | ||
let text = contentItem.text || '' | ||
const {styles = {}} = contentItem | ||
|
||
text = applyStyles(text, styles) | ||
|
||
if (contentItem.type === 'link') { | ||
const linkText = applyStyles( | ||
contentItem.content[0].text, | ||
contentItem.content[0].styles || {}, | ||
) | ||
return `<a href="${contentItem.href}">${linkText}</a>` | ||
} else { | ||
return text | ||
} | ||
} | ||
|
||
function convertBlockToHtml(block) { | ||
let childrenHtml = '' | ||
if (block.children) { | ||
const childrenContent = block.children.map(convertBlockToHtml).join('\n') | ||
if (block.props.childrenType === 'ul') { | ||
childrenHtml = `<ul>${childrenContent}</ul>` | ||
} else if (block.props.childrenType === 'ol') { | ||
childrenHtml = `<ol start="${ | ||
block.props.start || 1 | ||
}">${childrenContent}</ol>` | ||
} else { | ||
childrenHtml = childrenContent | ||
} | ||
} | ||
|
||
switch (block.type) { | ||
case 'heading': | ||
return `<h${block.props.level}>${block.content | ||
.map(convertContentItemToHtml) | ||
.join('')}</h${block.props.level}>\n${childrenHtml}` | ||
case 'paragraph': | ||
return `<p>${block.content | ||
.map(convertContentItemToHtml) | ||
.join('')}</p>\n${childrenHtml}` | ||
case 'image': | ||
return `<img src="${block.props.url}" alt="${block.content | ||
.map((contentItem) => contentItem.text) | ||
.join('')}" title="${block.props.name}">\n${childrenHtml}` | ||
case 'codeBlock': | ||
return `<pre><code class="language-${ | ||
block.props.language || 'plaintext' | ||
}">${block.content | ||
.map((contentItem) => contentItem.text) | ||
.join('\n')}</code></pre>\n${childrenHtml}` | ||
case 'video': | ||
return `<p>![${block.props.name}](${block.props.url} "width=${block.props.width}")</p>\n${childrenHtml}` | ||
case 'file': | ||
return `<p>[${block.props.name}](${block.props.url} "size=${block.props.size}")</p>\n${childrenHtml}` | ||
default: | ||
return block.content | ||
? block.content.map(convertContentItemToHtml).join('') + | ||
`\n${childrenHtml}` | ||
: childrenHtml | ||
} | ||
} | ||
|
||
function convertBlocksToHtml(blocks) { | ||
const htmlContent: string = blocks | ||
.map((block) => convertBlockToHtml(block)) | ||
.join('\n\n') | ||
return htmlContent | ||
} | ||
|
||
export async function convertBlocksToMarkdown(blocks: HMBlock[]) { | ||
const markdownFile = await unified() | ||
.use(rehypeParse, {fragment: true}) | ||
.use(rehypeRemark) | ||
.use(remarkGfm) | ||
.use(remarkStringify) | ||
.process(convertBlocksToHtml(blocks)) | ||
return markdownFile.value as string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters