Skip to content

Commit

Permalink
fix: file is null
Browse files Browse the repository at this point in the history
  • Loading branch information
samuele-cozzi committed Mar 9, 2024
1 parent 8140a6a commit d53c34e
Showing 1 changed file with 33 additions and 27 deletions.
60 changes: 33 additions & 27 deletions src/views/marpPreviewView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class MarpPreviewView extends ItemView {
if (this.settings.ThemePath != '') {
const fileContents: string[] = await Promise.all(
this.app.vault.getFiles()
.filter(x => x.parent.path == normalizePath(this.settings.ThemePath))
.filter(x => x.parent?.path == normalizePath(this.settings.ThemePath))
.map((file) => this.app.vault.cachedRead(file))
);

Expand Down Expand Up @@ -130,31 +130,37 @@ export class MarpPreviewView extends ItemView {

async displaySlides(view : MarkdownView) {

this.file = view.file;
const basePath = (new FilePath(this.settings)).getCompleteFileBasePath(view.file);
const markdownText = view.data;

const container = this.containerEl.children[1];
container.empty();


let { html, css } = this.marp.render(markdownText);

// Replace Backgorund Url for images
html = html.replace(/(?!background-image:url\("http)background-image:url\("/g, `background-image:url("${basePath}`);

const htmlFile = `
<!DOCTYPE html>
<html>
<head>
<base href="${basePath}"></base>
<style id="__marp-vscode-style">${css}</style>
</head>
<body>${html}</body>
</html>
`;

container.innerHTML = htmlFile;
this.marpBrowser?.update();
if (view.file != null) {
this.file = view.file;
const basePath = (new FilePath(this.settings)).getCompleteFileBasePath(view.file);
const markdownText = view.data;

const container = this.containerEl.children[1];
container.empty();


let { html, css } = this.marp.render(markdownText);

// Replace Backgorund Url for images
html = html.replace(/(?!background-image:url\(&quot;http)background-image:url\(&quot;/g, `background-image:url(&quot;${basePath}`);

const htmlFile = `
<!DOCTYPE html>
<html>
<head>
<base href="${basePath}"></base>
<style id="__marp-vscode-style">${css}</style>
</head>
<body>${html}</body>
</html>
`;

container.innerHTML = htmlFile;
this.marpBrowser?.update();
}
else
{
console.log("Errore: view.file is null")
}
}
}

0 comments on commit d53c34e

Please sign in to comment.