Skip to content

Commit

Permalink
Added Turndown Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Offerel committed Feb 6, 2021
1 parent f8c68a2 commit 38f6f1c
Show file tree
Hide file tree
Showing 8 changed files with 1,068 additions and 57 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
### v2.0.2
- Added option button to jump directly to plugin options (thanks to Aleksander Machniak)
- Added function to paste selected text from browser, converted automatically to Markdown with [Turndown](https://github.com/domchristie/turndown)
- Added support for Markdownload [Browser Extension](https://github.com/deathau/markdownload)
- YAML header autofills all supported fields (title, author, date, source and more)

### v2.0.1
- Remove ToC button in edit mode
Expand Down
66 changes: 58 additions & 8 deletions js/notes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* Roundcube Notes Plugin
*
* @version 2.0.1
* @version 2.0.2
* @author Offerel
* @copyright Copyright (c) 2021, Offerel
* @license GNU General Public License, version 3
*/
$(document).ready(function(){
var tagify = new Tagify(document.querySelector('#ntags'), {
var tagify = new Tagify(document.querySelector('#ntags'), {
whitelist:[],
dropdown : {
classname : "color-blue",
Expand Down Expand Up @@ -40,7 +40,9 @@ $(document).ready(function(){
spellChecker: false,
autofocus: true,
status: false,
promptURLs: true,
promptURLs: true,
inputStyle: 'contenteditable',
nativeSpellcheck: true,
//sideBySideFullscreen: false,
renderingConfig: {
codeSyntaxHighlighting: true,
Expand All @@ -65,8 +67,7 @@ $(document).ready(function(){
},
'table', '|',
'preview', 'side-by-side', 'fullscreen', 'guide', '|' ],

});
});

document.querySelectorAll('#filelist li a').forEach(function(note){
note.addEventListener('click',function(){
Expand All @@ -81,7 +82,7 @@ $(document).ready(function(){
if('ttags' in e.data && e.data.ttags == '') tagify.removeAllTags();
if('editor' in e.data && e.data.editor == 'new') {
if(estate.value == 's') {
mde.togglePreview();
mde.togglePreview();
estate.value = 'e';
}
mde.value("");
Expand All @@ -93,6 +94,11 @@ $(document).ready(function(){
document.querySelector('#main_area .editor-toolbar').style.display = 'block';
document.querySelector('.EasyMDEContainer').style = 'display: block';
mde.value('');
editor1.style = 'display: none;'
document.getElementById('author').value = '';
document.getElementById('date').value = '';
document.getElementById('source').value = '';
document.querySelector('#main_area .EasyMDEContainer').addEventListener('paste', pasteParse, false);
} else {
let toolbar = document.createElement('div');
toolbar.id = 'atoolbar';
Expand All @@ -112,7 +118,7 @@ $(document).ready(function(){
}
if('editor' in e.data && e.data.editor == 'edit') {
if(estate.value == 's') {
mde.togglePreview();
mde.togglePreview();
estate.value = 'e';
}

Expand Down Expand Up @@ -175,6 +181,45 @@ $(document).ready(function(){

new rcube_splitter({ id:'notessplitter', p1:'#sidebar', p2:'#main', orientation:'v', relative:true, start:400, min:250, size:12 }).init();

function pasteParse(event) {
const pastedText = event.clipboardData.getData('text');
const pastedHTML = event.clipboardData.getData('text/html');
let textArr = pastedText.split('\n');
if(textArr[0] == '---') {
let cstart = pastedText.indexOf('---',4) + 3;
for(var i = 1; i < 10; i++) {
if(textArr[i] == '---') break;
console.log(textArr[i]);
let yentry = textArr[i].split(':');
if(yentry[0] == 'title') document.getElementById('note_name').value = yentry[1].trim();
if(yentry[0] == 'tags') tagify.addTags(yentry[1]);
if(yentry[0] == 'author') document.getElementById('author').value = yentry[1].trim();
if(yentry[0] == 'date') document.getElementById('date').value = yentry.slice(1).join(':').trim();
if(yentry[0] == 'source') document.getElementById('source').value = yentry.slice(1).join(':').trim();
}
mde.value(pastedText.substr(cstart).trim());
}

if(pastedHTML) {
var options = {
headingStyle: 'atx',
hr: '-',
bulletListMarker: '-',
codeBlockStyle: 'fenced',
fence: '```',
emDelimiter: '*',
strongDelimiter: '**',
linkStyle: 'inlined',
linkReferenceStyle: 'full',
collapseMultipleWhitespaces: true,
preformattedCode: true,
};
var turndownService = new window.TurndownService(options);
turndownService.keep(['kbd', 'ins']);
mde.value(turndownService.turndown(pastedHTML));
}
}

function firstNote() {
showNote(document.getElementById('filelist').firstElementChild.classList[0]);
}
Expand Down Expand Up @@ -238,7 +283,6 @@ $(document).ready(function(){
},
success: function(data){
var note = JSON.parse(data);

if(document.getElementById('bcontent')) document.getElementById('bcontent').remove();
document.querySelector('.EasyMDEContainer').classList.remove('EasyMDEContainerH');
if(document.getElementById('tocdiv')) document.getElementById('tocdiv').remove();
Expand All @@ -251,6 +295,9 @@ $(document).ready(function(){

document.getElementById('headerTitle').innerText = note.notename;
document.getElementById('fname').value = note.filename;
document.getElementById('author').value = note.author;
document.getElementById('date').value = note.date;
document.getElementById('source').value = note.source;

tagify.setReadonly(true);
tagify.removeAllTags();
Expand Down Expand Up @@ -377,6 +424,9 @@ $(document).ready(function(){
ntags: tArr,
editor1: mde.value(),
ftype: fname.substr(extb),
author: document.getElementById('author').value,
date: document.getElementById('date').value,
source: document.getElementById('source').value,
},
success: function(response){
if(response == '') {
Expand Down
4 changes: 2 additions & 2 deletions js/notes.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 38f6f1c

Please sign in to comment.