Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow embedding only a section of a document. #175

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,29 @@ 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
// We should replace the embed with a wikilink
// Then our link processing happens afterwards
span.outerHTML = `<a href="${file}">${span.innerHTML}</a>`;
} 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)
Expand Down