From 31ba1789f111d461c162c41a8c7b0bcde2be4ad2 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 23 Jun 2018 13:35:42 -0700 Subject: [PATCH] Improved document download Correctly unescapes documents and downloads latest version --- app/models/Document.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/app/models/Document.js b/app/models/Document.js index 2b895017f..0b9c9cfad 100644 --- a/app/models/Document.js +++ b/app/models/Document.js @@ -6,6 +6,7 @@ import { client } from 'utils/ApiClient'; import stores from 'stores'; import UiStore from 'stores/UiStore'; import parseTitle from '../../shared/utils/parseTitle'; +import unescape from '../../shared/utils/unescape'; import type { User } from 'types'; import BaseModel from './BaseModel'; @@ -270,15 +271,16 @@ class Document extends BaseModel { this.emit('documents.duplicate', this); }; - download() { - const a = window.document.createElement('a'); - a.textContent = 'download'; + download = async () => { + await this.fetch(); + + const blob = new Blob([unescape(this.text)], { type: 'text/markdown' }); + const url = URL.createObjectURL(blob); + const a = document.createElement('a'); + a.href = url; a.download = `${this.title}.md`; - a.href = `data:text/markdown;charset=UTF-8,${encodeURIComponent( - this.text - )}`; a.click(); - } + }; updateData(data: Object = {}, dirty: boolean = false) { if (data.text) {