trilium/static/js/notecase2html.js

50 lines
1.4 KiB
JavaScript
Raw Normal View History

2017-06-12 04:04:07 +08:00
function notecase2html(note) {
let noteText = note.detail.note_text;
let formatting = note.formatting;
let links = note.links;
let images = note.images;
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 fmt of formatting) {
if (tags[fmt.fmt_tag]) {
noteText = inject(noteText, tags[fmt.fmt_tag], fmt.note_offset + offset);
}
}
offset = 0;
for (let link of links) {
let linkHtml = '<a href="' + link.target_url + '">' + link.lnk_text + '</a>';
noteText = noteText.substr(0, link.note_offset + offset) + noteText.substr(link.note_offset + offset + link.lnk_text.length);
noteText = inject(noteText, linkHtml, link.note_offset + offset);
offset -= link.lnk_text.length;
}
offset = 0;
for (let image of images) {
let type = image.is_png ? "png" : "jpg";
let imgHtml = '<img alt="Embedded Image" src="data:image/' + type + ';base64,' + image.image_data + '" />';
noteText = inject(noteText, imgHtml, image.note_offset + offset);
}
noteText = noteText.replace(/(?:\r\n|\r|\n)/g, '<br />');
2017-08-14 09:42:10 +08:00
noteText = noteText.replace(/ /g, '&nbsp;&nbsp;');
2017-06-12 04:04:07 +08:00
return noteText;
}