trilium/static/js/notecase2html.js

67 lines
2 KiB
JavaScript
Raw Normal View History

2017-06-12 04:04:07 +08:00
function notecase2html(note) {
2017-09-18 09:17:04 +08:00
if (globalHtmlEnabled) {
return note.detail.note_text;
}
2017-06-12 04:04:07 +08:00
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');
2017-08-30 10:25:58 +08:00
const allTags = note.formatting.concat(note.links).concat(note.images);
allTags.sort(function compare(a, b) {
return a.note_offset - b.note_offset;
});
2017-06-12 04:04:07 +08:00
let offset = 0;
function inject(target, injected, position) {
offset += injected.length;
return noteText.substr(0, position) + injected + noteText.substr(position);
}
2017-08-30 10:25:58 +08:00
for (const el of allTags) {
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') {
2017-08-28 02:39:26 +08:00
let targetUrl;
if (el.target_url) {
targetUrl = el.target_url;
}
else {
targetUrl = "app#" + el.target_note_id;
}
2017-08-30 10:25:58 +08:00
const linkHtml = '<a href="' + targetUrl + '">' + 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') {
2017-08-30 10:25:58 +08:00
const type = el.is_png ? "png" : "jpg";
2017-06-12 04:04:07 +08:00
2017-08-30 10:25:58 +08:00
const 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;
}