Skip to content

Commit

Permalink
🐛 fix frontmatter detection when embedding local slides
Browse files Browse the repository at this point in the history
  • Loading branch information
ebullient committed Dec 21, 2024
1 parent 987049a commit 0ca4b19
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
15 changes: 5 additions & 10 deletions src/obsidian/obsidianUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class ObsidianUtils implements MediaCollector {
private images = new Set<string>();
private isCollecting = false;

private yamlRegex = /^---.*?---\n(.*?)($|---)/s;
private yamlRegex = /^(---\n[\s\S]*?\n---\n)/;

constructor(app: App, settings: SlidesExtendedSettings) {
this.app = app;
Expand Down Expand Up @@ -229,13 +229,15 @@ export class ObsidianUtils implements MediaCollector {
});

if (header === null) {
if (this.yamlRegex.test(fileContent)) {
return this.yamlRegex.exec(fileContent)[1];
const match = this.yamlRegex.exec(fileContent);
if (match) {
return fileContent.slice(match[0].length);
}
return fileContent;
}
const cache = this.app.metadataCache.getFileCache(tfile);
const resolved = resolveSubpath(cache, header);
console.log("parseFile, cache, resolved", cache, resolved);

if (resolved && resolved.start && resolved.start.line != null) {
if (resolved.end && resolved.end.line != null) {
Expand All @@ -259,12 +261,6 @@ export class ObsidianUtils implements MediaCollector {
}

async fetchRemoteMarkdown(markdown: string): Promise<string> {
const stackTrace = Error().stack;
console.log(
"fetchRemoteMarkdown",
markdown.contains("file://"),
stackTrace,
);
const wikilinkFileRegex = /!\[\[(file:.+?\.md)(\|[^\]]+)?\]\]/gi;
const fileUrlRegex = /(!\[[^\]]*?\]\()(file:.+?\.md(?:#.+?)?)(\))/i;

Expand All @@ -275,7 +271,6 @@ export class ObsidianUtils implements MediaCollector {
const url = new URL(filePath);
return `![${alias}](${url})`;
});
console.log(markdown);

// Replace markdown links with markdown content
if (fileUrlRegex.test(markdown)) {
Expand Down
12 changes: 6 additions & 6 deletions src/obsidian/processors/multipleFileProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ export class MultipleFileProcessor {
private utils: ObsidianUtils;

private regex = /!\[\[(.*?)(\|[^\]]*?)?\]\]/g;
private excalidrawRegex = /(.*\.excalidraw)/i;
private markdownRegex = /!\[.*?\]\((.*?\.md)\)/g;

private excalidrawRegex = /(.*\.excalidraw)/i;

private wikilinkFileRegex = /\[\[(file:.+?)(\|[^\]]+)?\]\]/gi;
private fileUrlRegex = /(\[[^\]]*?\]\()(file:.+?)((?: ".*?")?\))/gi;

Expand Down Expand Up @@ -69,7 +70,6 @@ export class MultipleFileProcessor {
}

const fileName = this.getMarkdownFile(link.replace("%20", " "));

if (fileName === null) {
return matched;
}
Expand All @@ -79,10 +79,10 @@ export class MultipleFileProcessor {
return matched;
}

if (comment.length > 0) {
return this.process(content + comment);
}
return this.process(content);
const result = `\n<!-- begin::[${fileName}] -->
${this.process(content + comment).trim()}
<!-- end::[${fileName}] -->\n`;
return result;
});
}

Expand Down
8 changes: 5 additions & 3 deletions src/reveal/revealPreviewView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,11 @@ export class RevealPreviewView extends ItemView {
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (view?.file.name.includes(filename)) {
const line = this.getTargetLine(url, view.data);
view.editor.setCursor(view.editor.lastLine());
view.editor.setCursor({ line: line, ch: 0 });
// line will be undefined for embedded content
if (line) {
view.editor.setCursor(view.editor.lastLine());
view.editor.setCursor({ line: line, ch: 0 });
}
}
}

Expand Down Expand Up @@ -131,7 +134,6 @@ export class RevealPreviewView extends ItemView {
break;
}
}

if (resultKey) {
const keys = resultKey.split(",");
return [Number.parseInt(keys[0]), Number.parseInt(keys[1])];
Expand Down

0 comments on commit 0ca4b19

Please sign in to comment.