Skip to content

Commit

Permalink
Merge pull request #36 from guicassolato/fix/escape-html
Browse files Browse the repository at this point in the history
fix: escapeHtml invoked on undefined engine const
  • Loading branch information
guicassolato committed Jan 8, 2023
2 parents 0ec53a8 + b72cc3f commit 1a929b7
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions src/engine.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { window } from 'vscode';
import hljs from 'highlight.js';

import * as terminal from './terminal';
Expand Down Expand Up @@ -43,9 +44,14 @@ export class Engine {
if (options?.imageFunc) {
this._engine.renderer.rules.image = options?.imageFunc;
}
const html = this._engine.render(content);
this._engine.renderer.rules.image = this._originalImageFunc;
return html;
try {
const html = this._engine.render(content);
this._engine.renderer.rules.image = this._originalImageFunc;
return html;
} catch (e) {
window.showErrorMessage(`Failed to render markdown content as HTML: ${e}`);
return '';
}
};

parseInline = (text: string, env: object): any[] => {
Expand All @@ -56,22 +62,27 @@ export class Engine {
private runInTerminalTitle = (): string => this.options?.runInTerminalTitle || defaultRunInTerminalTitle;

private renderCodeBlock = (code: string, language: string): string => {
const id = `code-${this._nonce++}`;
const codeAttrs = (language !== "") ? ` class="language-${language}"` : '';

let link = "";
switch (language) {
case 'bash':
case 'sh':
case 'zsh':
const href = `tothom://?p=${terminal.encodeTerminalCommand(code, true)}&id=${id}`;
link = `<a href="${href}" class="tothom-code-action" title="${this.runInTerminalTitle()}">${this.runInTerminalLabel()}</a>`;
break;
default:
break;
}
try {
const id = `code-${this._nonce++}`;
const codeAttrs = (language !== "") ? ` class="language-${language}"` : '';

let link = "";
switch (language) {
case 'bash':
case 'sh':
case 'zsh':
const href = `tothom://?p=${terminal.encodeTerminalCommand(code, true)}&id=${id}`;
link = `<a href="${href}" class="tothom-code-action" title="${this.runInTerminalTitle()}">${this.runInTerminalLabel()}</a>`;
break;
default:
break;
}

return `<pre class="tothom-code hljs" id="${id}"><code${codeAttrs}>${this.syntaxHighlight(code, language)}</code>${link}</pre>`;
return `<pre class="tothom-code hljs" id="${id}"><code${codeAttrs}>${this.syntaxHighlight(code, language)}</code>${link}</pre>`;
} catch (e) {
window.showErrorMessage(`Failed to render code block: ${e}`);
return code;
}
};

private syntaxHighlight = (code: string, language: string): string => {
Expand All @@ -82,7 +93,7 @@ export class Engine {
console.error(err);
}
}
return engine.utils.escapeHtml(code);
return this._engine.utils.escapeHtml(code);
};

private headingOpen = (): RendererRuleFunc => {
Expand Down

0 comments on commit 1a929b7

Please sign in to comment.