use GFM extensions for markdown export, closes #638

This commit is contained in:
zadam 2019-09-30 20:56:32 +02:00
parent ed24e32305
commit f6fc24d11d
5 changed files with 31 additions and 9 deletions

9
package-lock.json generated
View File

@ -1549,7 +1549,7 @@
},
"buf-compare": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/buf-compare/-/buf-compare-1.0.1.tgz",
"resolved": "http://registry.npmjs.org/buf-compare/-/buf-compare-1.0.1.tgz",
"integrity": "sha1-/vKNqLgROgoNtEMLC2Rntpcws0o=",
"dev": true
},
@ -4713,7 +4713,7 @@
},
"load-json-file": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-2.0.0.tgz",
"resolved": "http://registry.npmjs.org/load-json-file/-/load-json-file-2.0.0.tgz",
"integrity": "sha1-eUfkIUmvgNaWy/eXvKq8/h/inKg=",
"dev": true,
"requires": {
@ -13130,6 +13130,11 @@
"jsdom": "^11.9.0"
}
},
"turndown-plugin-gfm": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/turndown-plugin-gfm/-/turndown-plugin-gfm-1.0.2.tgz",
"integrity": "sha512-vwz9tfvF7XN/jE0dGoBei3FXWuvll78ohzCZQuOb+ZjWrs3a0XhQVomJEb2Qh4VHTPNRO4GPZh0V7VRbiWwkRg=="
},
"tweetnacl": {
"version": "0.14.5",
"resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-0.14.5.tgz",

View File

@ -73,6 +73,7 @@
"string-similarity": "^3.0.0",
"tar-stream": "2.1.0",
"turndown": "5.0.3",
"turndown-plugin-gfm": "1.0.2",
"unescape": "1.0.1",
"ws": "7.1.2",
"xml2js": "0.4.22"

19
src/services/export/md.js Normal file
View File

@ -0,0 +1,19 @@
"use strict";
const TurndownService = require('turndown');
const turndownPluginGfm = require('turndown-plugin-gfm');
let instance = null;
function toMarkdown(content) {
if (instance === null) {
instance = new TurndownService();
instance.use(turndownPluginGfm.gfm);
}
return instance.turndown(content);
}
module.exports = {
toMarkdown
};

View File

@ -1,9 +1,9 @@
"use strict";
const TurndownService = require('turndown');
const mimeTypes = require('mime-types');
const html = require('html');
const utils = require('../utils');
const mdService = require('./md');
async function exportSingleNote(exportContext, branch, format, res) {
const note = await branch.getNote();
@ -31,8 +31,7 @@ async function exportSingleNote(exportContext, branch, format, res) {
mime = 'text/html';
}
else if (format === 'markdown') {
const turndownService = new TurndownService();
payload = turndownService.turndown(content);
payload = mdService.toMarkdown(content);
extension = 'md';
mime = 'text/x-markdown'
}

View File

@ -5,7 +5,7 @@ const repository = require('../repository');
const tar = require('tar-stream');
const path = require('path');
const mimeTypes = require('mime-types');
const TurndownService = require('turndown');
const mdService = require('./md');
const packageInfo = require('../../../package.json');
const utils = require('../utils');
const protectedSessionService = require('../protected_session');
@ -17,8 +17,6 @@ const sanitize = require("sanitize-filename");
* @param {string} format - 'html' or 'markdown'
*/
async function exportToTar(exportContext, branch, format, res) {
let turndownService = format === 'markdown' ? new TurndownService() : null;
const pack = tar.pack();
const noteIdToMeta = {};
@ -232,7 +230,7 @@ ${content}
return html.prettyPrint(content, {indent_size: 2});
}
else if (noteMeta.format === 'markdown') {
let markdownContent = turndownService.turndown(content);
let markdownContent = mdService.toMarkdown(content);
if (markdownContent.trim().length > 0 && !markdownContent.startsWith("# ")) {
markdownContent = '# ' + title + "\r\n" + markdownContent;