Skip to content

Commit

Permalink
feat: support export markdown file
Browse files Browse the repository at this point in the history
  • Loading branch information
shuowu committed Nov 27, 2020
1 parent d613f4a commit 782e805
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/common/icons/markdown.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions packages/common/services/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ export const exportJsonFile = async (data, filename, version) => {
await exportFile(blob, filename);
};

export const exportMarkdownFile = async (data, filename) => {
// eslint-disable-next-line no-undef
const blob = new Blob([data], {
type: 'text/markdown;charset=utf-8'
});
await exportFile(blob, filename);
};

export const exportFile = async (blob, filename) => {
const manifest = await browser.runtime.getManifest();
if (typeof browser !== 'undefined' && manifest.browser === 'firefox') {
Expand All @@ -49,5 +57,6 @@ export const exportFile = async (blob, filename) => {
export default {
readAsJson,
exportJsonFile,
exportMarkdownFile,
exportFile
};
5 changes: 2 additions & 3 deletions packages/common/services/integration/evernote/generator.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import EvernoteSDK from 'evernote';
import md5 from 'md5';
import { getBinaryFromBase64 } from '../utils';
import Markdown from '../../markdown';
import { secondsToTime, buildAutoSeekUrl } from '../../../utils';
import { INSTALLATION_URL } from '../../../constants';

Expand Down Expand Up @@ -58,9 +57,9 @@ class Generator {
}
nBody += `<div><span style="padding-right: 20px;"><a href="${escape(
timestampedUrl
)}">${secondsToTime(note.timestamp)}</a></span><p>${Markdown.toText(
)}">${secondsToTime(note.timestamp)}</a></span><p>${
note.content
)}</p></div>`;
}</p></div>`;
});

nBody += '</en-note>';
Expand Down
3 changes: 1 addition & 2 deletions packages/common/services/integration/googledocs/generator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { b64toBlob } from '../utils';
import Markdown from '../../markdown';
import { secondsToTime, buildAutoSeekUrl } from '../../../utils';
import { INSTALLATION_URL } from '../../../constants';

Expand Down Expand Up @@ -217,7 +216,7 @@ class Generator {
}

addNoteContent(note) {
const content = Markdown.toText(note.content);
const content = note.content;
this.requests.push({
insertText: {
text: content + '\n',
Expand Down
3 changes: 1 addition & 2 deletions packages/common/services/integration/onenote/generator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Markdown from '../../markdown';
import { secondsToTime, buildAutoSeekUrl } from '../../../utils';
import { INSTALLATION_URL } from '../../../constants';

Expand Down Expand Up @@ -36,7 +35,7 @@ class Generator {
${secondsToTime(note.timestamp)}
</a>
</span>
<p>${Markdown.toText(note.content)}</p>
<p>${note.content}</p>
</div>
`;
})}
Expand Down
24 changes: 24 additions & 0 deletions packages/common/services/markdown.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import marked from 'marked';
import { secondsToTime, buildAutoSeekUrl } from '../utils';
import { INSTALLATION_URL } from '../constants';

class Markdown {
static toText(markdownContent) {
Expand All @@ -10,6 +12,28 @@ class Markdown {
static toHTML(markdownContent) {
return marked(markdownContent);
}

static pagesToMarkdown(pages) {
let data = `<!-- ${browser.i18n.getMessage(
'services_template_signature',
`<a href="${INSTALLATION_URL}">YiNote</a>`
)} -->\n\n`;

for (let page of pages) {
const { meta, notes } = page;
data += `<!-- # [${meta.title}](${meta.url}) -->\n\n`;

for (let note of notes) {
data += `<!-- ## [${secondsToTime(note.timestamp)}](${buildAutoSeekUrl(
meta.url,
note.timestamp
)}) -->\n\n`;
data += note.content + '\n\n';
}
}

return data;
}
}

export default Markdown;
11 changes: 5 additions & 6 deletions packages/common/services/pdf/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import jsPDF from 'jspdf';
import { secondsToTime, buildAutoSeekUrl, getFileUrl } from '../../utils';
import StorageService from '../storage';
import Markdown from '../markdown';
import {
WEBSITE_URL,
KEY_APPLY_SEEK_SEC_ON_URL,
Expand Down Expand Up @@ -36,8 +35,8 @@ export default class PDFGenerator {
async getBlobOutput({ url, title, notes }) {
// TODO: pass in options instead of use settings from storage
const settings = await StorageService.getStorage().getSettings();
this.seekSeconds = +settings[KEY_VIDEO_SEEK_SECONDS] || 0;
this.shouldApplySeekSecondsOnUrl = settings[KEY_APPLY_SEEK_SEC_ON_URL];
const seekSeconds = +settings[KEY_VIDEO_SEEK_SECONDS] || 0;
const shouldApplySeekSecondsOnUrl = settings[KEY_APPLY_SEEK_SEC_ON_URL];

this.doc.setFont('msyh');
this.doc.setFontType('normal');
Expand All @@ -62,7 +61,7 @@ export default class PDFGenerator {
this.doc.setFontSize(12);

for (const note of notes) {
let content = Markdown.toText(note.content);
let content = note.content;
content = this.doc.splitTextToSize(content, 180);
if (y + 66 + 6 + 6 * content.length > 300) {
this.doc.addPage();
Expand All @@ -77,8 +76,8 @@ export default class PDFGenerator {
this.doc.textWithLink(secondsToTime(note.timestamp), 20, y, {
url: buildAutoSeekUrl(
url,
this.shouldApplySeekSecondsOnUrl
? note.timestamp - this.seekSeconds
shouldApplySeekSecondsOnUrl
? note.timestamp - seekSeconds
: note.timestamp
)
});
Expand Down
8 changes: 7 additions & 1 deletion packages/extension/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
"options_bookmarks_export_tooltip": {
"message": "Select bookmarks to export"
},
"options_bookmarks_export_format_label": {
"message": "Export format:"
},
"options_page_title": {
"message": "Page Detail"
},
Expand All @@ -135,6 +138,9 @@
"options_page_onenote_tooltip": {
"message": "Send notes to OneNote"
},
"options_page_export_markdown_tooltip": {
"message": "Export as markdown"
},
"options_page_open_tooltip": {
"message": "Open page in new tab"
},
Expand Down Expand Up @@ -241,7 +247,7 @@
"message": "Cancel"
},
"services_template_signature": {
"message": "Generated from $INSTALLATION$",
"message": "Generated by $INSTALLATION$",
"placeholders": {
"installation": {
"content": "$1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import {
storage as StorageService,
file as FileService
} from '@yi-note/common/services';
import Markdown from '@yi-note/common/services/markdown';

const Toolbar = () => {
const { t } = useTranslation('options');
const {
toolbar: { exporting, filtering },
toolbar: { exporting, filtering, exportFormat },
bookmarks
} = useStoreState(state => state.bookmarks);
const {
Expand All @@ -38,7 +39,11 @@ const Toolbar = () => {
return StorageService.getStorage()
.getPagesForExport(ids)
.then(pages => {
FileService.exportJsonFile(pages, 'yi-note.json');
if (exportFormat === 'markdown') {
const data = Markdown.pagesToMarkdown(pages);
return FileService.exportMarkdownFile(data, 'yi-note.md');
}
return FileService.exportJsonFile(pages, 'yi-note.json');
})
.then(() => setExporting(false));
};
Expand Down
57 changes: 52 additions & 5 deletions packages/extension/src/options/containers/Bookmarks/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import React, { useEffect } from 'react';
import { useStoreState, useStoreActions } from 'easy-peasy';
import { useTranslation } from 'react-i18next';
import { useLocation } from 'react-router-dom';
import styled from 'styled-components';
import { Grid, List, ListItem, Chip } from '@material-ui/core';
import {
Grid,
List,
ListItem,
Chip,
FormLabel,
RadioGroup,
FormControlLabel,
Radio
} from '@material-ui/core';
import BookmarkItem from './BookmarkItem';
import NoBookmark from './NoBookmark';

Expand All @@ -11,17 +21,21 @@ const StyledContainer = styled(Grid)`
`;

const Bookmarks = () => {
const { t } = useTranslation('options');
const { search } = useLocation();
const {
bookmarks: {
bookmarks,
tags,
toolbar: { filtering }
toolbar: { filtering, exporting, exportFormat }
}
} = useStoreState(state => state);
const { fetchBookmarks, fetchTags, selectTag } = useStoreActions(
actions => actions.bookmarks
);
const {
fetchBookmarks,
fetchTags,
selectTag,
toolbar: { setExportFormat }
} = useStoreActions(actions => actions.bookmarks);

useEffect(() => {
let tagsFromUrl = [];
Expand All @@ -37,12 +51,45 @@ const Bookmarks = () => {
selectTag(tag);
};

const handleExportFormatChange = e => setExportFormat(e.target.value);

return (
<StyledContainer>
{bookmarks.length === 0 && !filtering ? (
<NoBookmark />
) : (
<Grid container>
{exporting && (
<Grid
item
container
direction="row"
alignItems="center"
spacing={1}
>
<Grid item>
<FormLabel>{t('bookmarks.export.format.label')}</FormLabel>
</Grid>
<Grid item>
<RadioGroup
row
value={exportFormat}
onChange={handleExportFormatChange}
>
<FormControlLabel
value="json"
control={<Radio size="small" />}
label="JSON"
/>
<FormControlLabel
value="markdown"
control={<Radio size="small" />}
label="Markdown (no image)"
/>
</RadioGroup>
</Grid>
</Grid>
)}
{filtering && (
<Grid item container spacing={1}>
{tags.map(({ tag, selected }) => (
Expand Down
16 changes: 14 additions & 2 deletions packages/extension/src/options/containers/Page/Toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import {
import {
EvernoteIcon,
GoogleDocsIcon,
OneNoteIcon
OneNoteIcon,
MarkdownIcon
} from '@yi-note/common/icons';
import {
pdf as PDFService,
file as FileService,
integration as IntegrationService
integration as IntegrationService,
markdown as MarkdownService
} from '@yi-note/common/services';
import { capitalize } from '@yi-note/common/utils';
import { APP_ID } from '@yi-note/common/constants';
Expand Down Expand Up @@ -94,6 +96,11 @@ const Toolbar = () => {
});
};

const handleExportMarkdown = () => {
const data = MarkdownService.pagesToMarkdown([{ meta, notes }]);
return FileService.exportMarkdownFile(data, `yinote_${meta.title}.md`);
};

const handleOpenPage = () => {
window.open(meta.url, '_blank');
};
Expand Down Expand Up @@ -134,6 +141,11 @@ const Toolbar = () => {
<OneNoteIcon fill="#ffffff" />
</IconButton>
</Tooltip>
<Tooltip title={t('page.export.markdown.tooltip')}>
<IconButton color="inherit" onClick={handleExportMarkdown}>
<MarkdownIcon fill="#ffffff" />
</IconButton>
</Tooltip>
<Tooltip title={t('page.open.tooltip')}>
<IconButton color="inherit" onClick={handleOpenPage}>
<OpenInNewIcon />
Expand Down
4 changes: 4 additions & 0 deletions packages/extension/src/options/store/bookmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export default {
filtering: true,
setFiltering: action((state, payload) => {
state.filtering = payload;
}),
exportFormat: 'json',
setExportFormat: action((state, payload) => {
state.exportFormat = payload;
})
},
reset: action(state => {
Expand Down

0 comments on commit 782e805

Please sign in to comment.