trilium/static/js/notecase2html.js

55 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-06-12 04:04:07 +08:00
function notecase2html(note) {
let noteText = note.detail.note_text;
note.formatting.forEach(el => el.type = 'formatting');
note.links.forEach(el => el.type = 'link');
note.images.forEach(el => el.type = 'image');
let all = note.formatting.concat(note.links).concat(note.images);
all.sort(function compare(a, b) {
return a.note_offset - b.note_offset;
});
2017-06-12 04:04:07 +08:00
let offset = 0;
let lastTag = null;
function inject(target, injected, position) {
offset += injected.length;
return noteText.substr(0, position) + injected + noteText.substr(position);
}
for (let el of all) {
if (el.type === 'formatting') {
if (tags[el.fmt_tag]) {
noteText = inject(noteText, tags[el.fmt_tag], el.note_offset + offset);
}
2017-06-12 04:04:07 +08:00
}
else if (el.type === 'link') {
let linkHtml = '<a href="' + el.target_url + '">' + el.lnk_text + '</a>';
2017-06-12 04:04:07 +08:00
noteText = noteText.substr(0, el.note_offset + offset) + noteText.substr(el.note_offset + offset + el.lnk_text.length);
2017-06-12 04:04:07 +08:00
noteText = inject(noteText, linkHtml, el.note_offset + offset);
2017-06-12 04:04:07 +08:00
offset -= el.lnk_text.length;
}
else if (el.type === 'image') {
let type = el.is_png ? "png" : "jpg";
2017-06-12 04:04:07 +08:00
let imgHtml = '<img alt="Embedded Image" src="data:image/' + type + ';base64,' + el.image_data + '" />';
2017-06-12 04:04:07 +08:00
noteText = inject(noteText, imgHtml, el.note_offset + offset);
}
2017-06-12 04:04:07 +08:00
}
noteText = noteText.replace(/(?:\r\n|\r)/g, '\n');
noteText = noteText.replace(/(.+)\n/g, '<p>$1</p>');
noteText = noteText.replace(/\n/g, '<p><br></p>');
2017-06-12 04:04:07 +08:00
2017-08-14 09:42:10 +08:00
noteText = noteText.replace(/ /g, '&nbsp;&nbsp;');
2017-06-12 04:04:07 +08:00
return noteText;
}