|
| 1 | +import { NormalizedPath } from "cdm/FolderModel"; |
| 2 | +import { DatabaseView } from "DatabaseView"; |
| 3 | +import { MediaExtensions } from "helpers/Constants"; |
| 4 | +import { getNormalizedPath } from "helpers/VaultManagement"; |
| 5 | +import { MarkdownRenderer, TFile } from "obsidian"; |
| 6 | +import { LOGGER } from "services/Logger"; |
| 7 | + |
| 8 | +export async function renderMarkdown( |
| 9 | + view: DatabaseView, |
| 10 | + markdownString: string, |
| 11 | + domElement: HTMLDivElement |
| 12 | +): Promise<HTMLDivElement> { |
| 13 | + try { |
| 14 | + await MarkdownRenderer.renderMarkdown( |
| 15 | + markdownString, |
| 16 | + domElement, |
| 17 | + view.file.path, |
| 18 | + view |
| 19 | + ); |
| 20 | + |
| 21 | + await handleEmbeds(domElement, view, 5); |
| 22 | + // applyCheckboxIndexes(dom); |
| 23 | + // findUnresolvedLinks(dom, view); |
| 24 | + } catch (e) { |
| 25 | + LOGGER.error(e); |
| 26 | + } |
| 27 | + |
| 28 | + return domElement; |
| 29 | +} |
| 30 | + |
| 31 | +function handleEmbeds(dom: HTMLDivElement, view: DatabaseView, depth: number) { |
| 32 | + return Promise.all( |
| 33 | + dom.findAll(".internal-embed").map(async (el) => { |
| 34 | + const src = el.getAttribute("src"); |
| 35 | + const normalizedPath = getNormalizedPath(src); |
| 36 | + const target = |
| 37 | + typeof src === "string" && |
| 38 | + view.app.metadataCache.getFirstLinkpathDest( |
| 39 | + normalizedPath.root, |
| 40 | + view.file.path |
| 41 | + ); |
| 42 | + |
| 43 | + if (!(target instanceof TFile)) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + if (MediaExtensions.IMAGE.contains(target.extension)) { |
| 48 | + return handleImage(el, target, view); |
| 49 | + } |
| 50 | + |
| 51 | + if (MediaExtensions.AUDIO.contains(target.extension)) { |
| 52 | + return handleAudio(el, target, view); |
| 53 | + } |
| 54 | + |
| 55 | + if (MediaExtensions.VIDEO.contains(target.extension)) { |
| 56 | + return handleVideo(el, target, view); |
| 57 | + } |
| 58 | + |
| 59 | + // if (target.extension === "md") { |
| 60 | + // return await handleMarkdown(el, target, normalizedPath, view, depth); |
| 61 | + // } |
| 62 | + |
| 63 | + //return handleUnknownFile(el, target); |
| 64 | + }) |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +function handleImage(el: HTMLElement, file: TFile, view: DatabaseView) { |
| 69 | + el.empty(); |
| 70 | + |
| 71 | + el.createEl( |
| 72 | + "img", |
| 73 | + { attr: { src: view.app.vault.getResourcePath(file) } }, |
| 74 | + (img) => { |
| 75 | + if (el.hasAttribute("width")) { |
| 76 | + img.setAttribute("width", el.getAttribute("width")); |
| 77 | + } |
| 78 | + |
| 79 | + if (el.hasAttribute("height")) { |
| 80 | + img.setAttribute("height", el.getAttribute("height")); |
| 81 | + } |
| 82 | + |
| 83 | + if (el.hasAttribute("alt")) { |
| 84 | + img.setAttribute("alt", el.getAttribute("alt")); |
| 85 | + } |
| 86 | + } |
| 87 | + ); |
| 88 | + |
| 89 | + el.addClasses(["image-embed", "is-loaded"]); |
| 90 | +} |
| 91 | + |
| 92 | +function handleAudio(el: HTMLElement, file: TFile, view: DatabaseView) { |
| 93 | + el.empty(); |
| 94 | + el.createEl("audio", { |
| 95 | + attr: { controls: "", src: view.app.vault.getResourcePath(file) }, |
| 96 | + }); |
| 97 | + el.addClasses(["media-embed", "is-loaded"]); |
| 98 | +} |
| 99 | + |
| 100 | +function handleVideo(el: HTMLElement, file: TFile, view: DatabaseView) { |
| 101 | + el.empty(); |
| 102 | + |
| 103 | + el.createEl( |
| 104 | + "video", |
| 105 | + { attr: { controls: "", src: view.app.vault.getResourcePath(file) } }, |
| 106 | + (video) => { |
| 107 | + const handleLoad = () => { |
| 108 | + video.removeEventListener("loadedmetadata", handleLoad); |
| 109 | + |
| 110 | + if (video.videoWidth === 0 && video.videoHeight === 0) { |
| 111 | + el.empty(); |
| 112 | + handleAudio(el, file, view); |
| 113 | + } |
| 114 | + }; |
| 115 | + |
| 116 | + video.addEventListener("loadedmetadata", handleLoad); |
| 117 | + } |
| 118 | + ); |
| 119 | + |
| 120 | + el.addClasses(["media-embed", "is-loaded"]); |
| 121 | +} |
0 commit comments