2019-10-05 10:55:29 +02:00
|
|
|
import server from "./server.js";
|
2025-01-09 18:36:24 +02:00
|
|
|
import bundleService, { type Bundle } from "./bundle.js";
|
2025-01-13 23:18:10 +02:00
|
|
|
import type FNote from "../entities/fnote.js";
|
2019-10-05 10:55:29 +02:00
|
|
|
|
2024-12-19 22:20:57 +02:00
|
|
|
async function render(note: FNote, $el: JQuery<HTMLElement>) {
|
2025-01-09 18:07:02 +02:00
|
|
|
const relations = note.getRelations("renderNote");
|
|
|
|
|
const renderNoteIds = relations.map((rel) => rel.value).filter((noteId) => noteId);
|
2019-10-05 10:55:29 +02:00
|
|
|
|
|
|
|
|
$el.empty().toggle(renderNoteIds.length > 0);
|
|
|
|
|
|
|
|
|
|
for (const renderNoteId of renderNoteIds) {
|
2024-12-19 22:20:57 +02:00
|
|
|
const bundle = await server.post<Bundle>(`script/bundle/${renderNoteId}`);
|
2019-10-05 10:55:29 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const $scriptContainer = $("<div>");
|
2019-10-05 10:55:29 +02:00
|
|
|
$el.append($scriptContainer);
|
|
|
|
|
|
|
|
|
|
$scriptContainer.append(bundle.html);
|
|
|
|
|
|
2020-02-02 21:16:20 +01:00
|
|
|
// async so that scripts cannot block trilium execution
|
2020-02-25 19:19:10 +01:00
|
|
|
bundleService.executeBundle(bundle, note, $scriptContainer);
|
2019-10-05 10:55:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return renderNoteIds.length > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
render
|
2025-01-09 18:07:02 +02:00
|
|
|
};
|