From 6525e973c1c4d8532765799b48e181664877a6dc Mon Sep 17 00:00:00 2001 From: jkunczik Date: Sat, 11 Mar 2023 22:15:17 +0100 Subject: [PATCH] Allow embedding only a section of a document. When trying to embed sections, the renderer fails, because the path of the document to embed can't be found. This commit fixes that and only includes the referenced section. (Analog to: jkunczik/obsidian-copy-as-html@8c7542b84493452bd68a9e7dfd2931dbd5dc2a98) --- renderer.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/renderer.ts b/renderer.ts index 41db1c9..49d84a8 100644 --- a/renderer.ts +++ b/renderer.ts @@ -174,8 +174,8 @@ async function postProcessRenderedHTML(plugin: PandocPlugin, inputFile: string, for (let span of Array.from(wrapper.querySelectorAll('span.internal-embed'))) { let src = span.getAttribute('src'); if (src) { - const subfolder = inputFile.substring(adapter.getBasePath().length); // TODO: this is messy - const file = plugin.app.metadataCache.getFirstLinkpathDest(src, subfolder); + let [file_name, heading] = src.split('#'); + const file = plugin.app.metadataCache.getFirstLinkpathDest(file_name, ""); try { if (parentFiles.indexOf(file.path) !== -1) { // We've got an infinite recursion on our hands @@ -183,7 +183,20 @@ async function postProcessRenderedHTML(plugin: PandocPlugin, inputFile: string, // Then our link processing happens afterwards span.outerHTML = `${span.innerHTML}`; } else { - const markdown = await adapter.read(file.path); + let markdown = await adapter.read(file.path); + if (heading) { + heading = heading.split(" ").join("[ \|]+") + // Get the header level + let regex = new RegExp(`(#*) (?:\\[*${heading}\\]*[ ]*\\n)`); + let res = markdown.match(regex) + if (!res || res.length <= 1) break; + const h_level = res[1].length; + // Get everything until a header with the same or lower level appears in the text + regex = new RegExp(`#* (?:\\[*${heading}\\]*[ ]*\\n)((?:.|\\n(?!#{1,${h_level}} ))*)`); + res = markdown.match(regex); + if (!res || res.length <= 1) break; + markdown = res[1]; + } const newParentFiles = [...parentFiles]; newParentFiles.push(inputFile); // TODO: because of this cast, embedded notes won't be able to handle complex plugins (eg DataView)