Skip to content

Build your personal knowledge base with Trilium Notes

use GFM extensions for markdown export, closes #638

zadamf6fc24d11d9c
diff --git a/package-lock.json b/package-lock.json
index c4535b2de2..937052b71a 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -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",
diff --git a/package.json b/package.json
index 24ee4807c8..12f7acfae3 100644
--- a/package.json
+++ b/package.json
@@ -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"
diff --git a/src/services/export/md.js b/src/services/export/md.js
new file mode 100644
index 0000000000..8374d368e0
--- /dev/null
+++ b/src/services/export/md.js
@@ -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
+};
\ No newline at end of file
diff --git a/src/services/export/single.js b/src/services/export/single.js
index dce90e1aa2..ebec3d48b0 100644
--- a/src/services/export/single.js
+++ b/src/services/export/single.js
@@ -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'
}
diff --git a/src/services/export/tar.js b/src/services/export/tar.js
index d09e3429b7..803d7e1475 100644
--- a/src/services/export/tar.js
+++ b/src/services/export/tar.js
@@ -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;