added markdown export (#627)

This commit is contained in:
Philip Molares 2020-10-06 12:43:17 +02:00 committed by GitHub
parent 4984008aff
commit 9f7a106834
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 79 additions and 9 deletions

View file

@ -0,0 +1,48 @@
const testTitle = 'testContent'
const testContent = `---\ntitle: ${testTitle}\n---\nThis is some test content`
describe('Export', () => {
beforeEach(() => {
cy.visit('/n/test')
cy.get('.btn.active.btn-outline-secondary > i.fa-columns')
.should('exist')
cy.get('.CodeMirror textarea')
.type('{ctrl}a', { force: true })
.type('{backspace}')
cy.get('.CodeMirror textarea')
.type(testContent)
})
it('Markdown', () => {
cy.get('#editor-menu-export')
.click()
cy.get('a.dropdown-item > i.fa-file-text')
.click()
cy.get('a[download]')
.then((anchor) => (
new Cypress.Promise((resolve: any, _: any) => {
// Use XHR to get the blob that corresponds to the object URL.
const xhr = new XMLHttpRequest();
xhr.open('GET', anchor.prop('href'), true);
xhr.responseType = 'blob';
// Once loaded, use FileReader to get the string back from the blob.
xhr.onload = () => {
if (xhr.status === 200) {
const blob = xhr.response;
const reader = new FileReader();
reader.onload = () => {
// Once we have a string, resolve the promise to let
// the Cypress chain continue, e.g. to assert on the result.
resolve(reader.result);
};
reader.readAsText(blob);
}
};
xhr.send();
})
))
// Now the regular Cypress assertions should work.
.should('equal', testContent);
})
})

View file

@ -5,5 +5,5 @@ export const download = (data: BlobPart, fileName: string, mimeType: string): vo
helperElement.download = fileName
document.body.appendChild(helperElement)
helperElement.click()
helperElement.remove()
setTimeout(() => helperElement.remove(), 2000)
}

View file

@ -29,7 +29,7 @@ export const DocumentBar: React.FC<DocumentBarProps> = ({ title, noteContent })
</div>
<div className="ml-auto navbar-nav">
<ImportMenu/>
<ExportMenu/>
<ExportMenu title={title} noteContent={noteContent}/>
<EditorMenu noteTitle={title}/>
<ConnectionIndicator/>
</div>

View file

@ -3,8 +3,14 @@ import { Dropdown } from 'react-bootstrap'
import { Trans, useTranslation } from 'react-i18next'
import links from '../../../../links.json'
import { ForkAwesomeIcon } from '../../../common/fork-awesome/fork-awesome-icon'
import { MarkdownExportDropdownItem } from './export/markdown'
const ExportMenu: React.FC = () => {
export interface ExportMenuProps {
title: string
noteContent: string
}
export const ExportMenu: React.FC<ExportMenuProps> = ({ title, noteContent }) => {
useTranslation()
return (
<Dropdown className='small mx-1' alignRight={true}>
@ -34,10 +40,10 @@ const ExportMenu: React.FC = () => {
<Dropdown.Header>
<Trans i18nKey='editor.documentBar.download'/>
</Dropdown.Header>
<Dropdown.Item className='small'>
<ForkAwesomeIcon icon='file-text' className={'mx-2'}/>
Markdown
</Dropdown.Item>
<MarkdownExportDropdownItem
title={title}
noteContent={noteContent}
/>
<Dropdown.Item className='small'>
<ForkAwesomeIcon icon='file-code-o' className={'mx-2'}/>
HTML
@ -61,5 +67,3 @@ const ExportMenu: React.FC = () => {
</Dropdown>
)
}
export { ExportMenu }

View file

@ -0,0 +1,18 @@
import React from 'react'
import { Dropdown } from 'react-bootstrap'
import { download } from '../../../../common/download/download'
import { ForkAwesomeIcon } from '../../../../common/fork-awesome/fork-awesome-icon'
export interface MarkdownExportDropdownItemProps {
title: string
noteContent: string
}
export const MarkdownExportDropdownItem: React.FC<MarkdownExportDropdownItemProps> = ({ title, noteContent }) => {
return (
<Dropdown.Item className='small' onClick={() => download(noteContent, `${title}.md`, 'text/markdown')}>
<ForkAwesomeIcon icon='file-text' className={'mx-2'}/>
Markdown
</Dropdown.Item>
)
}