Skip to content

Build your personal knowledge base with Trilium Notes

possibility to export single code note as markdown. UI fix of disabled buttons

azivner568c2c997fc5
diff --git a/src/public/javascripts/services/bootstrap.js b/src/public/javascripts/services/bootstrap.js
index e873c4dcc2..e1a779d3e3 100644
--- a/src/public/javascripts/services/bootstrap.js
+++ b/src/public/javascripts/services/bootstrap.js
@@ -103,7 +103,13 @@ if (utils.isElectron()) {
});
}
-$("#export-note-to-markdown-button").click(() => exportService.exportSubtree(noteDetailService.getCurrentNoteId(), 'markdown-single'));
+$("#export-note-to-markdown-button").click(function () {
+ if ($(this).hasClass("disabled")) {
+ return;
+ }
+
+ exportService.exportSubtree(noteDetailService.getCurrentNoteId(), 'markdown-single')
+});
treeService.showTree();
diff --git a/src/public/javascripts/services/entrypoints.js b/src/public/javascripts/services/entrypoints.js
index f62e42135a..d3a4a4e748 100644
--- a/src/public/javascripts/services/entrypoints.js
+++ b/src/public/javascripts/services/entrypoints.js
@@ -25,9 +25,21 @@ function registerEntrypoints() {
$("#jump-to-note-dialog-button").click(jumpToNoteDialog.showDialog);
utils.bindShortcut('ctrl+j', jumpToNoteDialog.showDialog);
- $("#show-note-revisions-button").click(noteRevisionsDialog.showCurrentNoteRevisions);
+ $("#show-note-revisions-button").click(function() {
+ if ($(this).hasClass("disabled")) {
+ return;
+ }
+
+ noteRevisionsDialog.showCurrentNoteRevisions();
+ });
- $("#show-source-button").click(noteSourceDialog.showDialog);
+ $("#show-source-button").click(function() {
+ if ($(this).hasClass("disabled")) {
+ return;
+ }
+
+ noteSourceDialog.showDialog();
+ });
$("#recent-changes-button").click(recentChangesDialog.showDialog);
diff --git a/src/services/export/markdown_single.js b/src/services/export/markdown_single.js
index ceb14b5538..7e878094ca 100644
--- a/src/services/export/markdown_single.js
+++ b/src/services/export/markdown_single.js
@@ -4,8 +4,20 @@ const sanitize = require("sanitize-filename");
const TurndownService = require('turndown');
async function exportSingleMarkdown(note, res) {
- const turndownService = new TurndownService();
- const markdown = turndownService.turndown(note.content);
+ if (note.type !== 'text' && note.type !== 'code') {
+ return [400, `Note type ${note.type} cannot be exported as single markdown file.`];
+ }
+
+ let markdown;
+
+ if (note.type === 'code') {
+ markdown = '```\n' + note.content + "\n```";
+ }
+ else if (note.type === 'text') {
+ const turndownService = new TurndownService();
+ markdown = turndownService.turndown(note.content);
+ }
+
const name = sanitize(note.title);
res.setHeader('Content-Disposition', 'file; filename="' + name + '.md"');
diff --git a/src/views/index.ejs b/src/views/index.ejs
index 4bfb68650f..bf16e394ae 100644
--- a/src/views/index.ejs
+++ b/src/views/index.ejs
@@ -159,7 +159,7 @@
<a class="dropdown-item show-attributes-button"><kbd>Alt+A</kbd> Attributes</a>
<a class="dropdown-item" id="show-source-button" data-bind="css: { disabled: type() != 'text' }">HTML source</a>
<a class="dropdown-item" id="upload-file-button">Upload file</a>
- <a class="dropdown-item" id="export-note-to-markdown-button" data-bind="css: { disabled: type() != 'text' }">Export as markdown</a>
+ <a class="dropdown-item" id="export-note-to-markdown-button" data-bind="css: { disabled: type() != 'text' && type() != 'code' }">Export as markdown</a>
</div>
</div>
</div>