Skip to content

Commit

Permalink
Fix code highlighing (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
raqbit authored Aug 23, 2020
1 parent c934344 commit 4879c04
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
Empty file modified index.js
100644 → 100755
Empty file.
19 changes: 10 additions & 9 deletions src/renderer/markdown-renderer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import FileRenderer from './file-renderer';
import * as MarkdownIt from 'markdown-it';
import {getLanguage} from 'highlight.js';
import {getLanguage, highlight} from 'highlight.js';
import * as tocPlugin from 'markdown-it-toc-done-right';
import * as anchorPlugin from 'markdown-it-anchor';
import * as umlPlugin from 'markdown-it-textual-uml';
Expand All @@ -13,8 +13,8 @@ export class MarkdownRenderer implements FileRenderer {
constructor() {
this.markdown = new MarkdownIt({
typographer: true,
highlight: (str, lang) => this.highlight(str, lang),
linkify: true,
highlight: MarkdownRenderer.highlight,
html: true,
});
// @ts-ignore
Expand All @@ -23,15 +23,16 @@ export class MarkdownRenderer implements FileRenderer {
this.markdown.use(umlPlugin);
}

private static highlight(str: string, lang: string) {
private highlight(str: string, lang: string) {
let code = '';

if (lang && getLanguage(lang)) {
try {
return MarkdownRenderer.highlight(lang, str).value;
} catch (_) {
}
return ''; // use external default escaping
code = highlight(lang, str, true).value;
} else {
code = this.markdown.utils.escapeHtml(str);
}
return null;

return `<pre class="hljs"><code>${code}</code></pre>`;
}

/**
Expand Down

0 comments on commit 4879c04

Please sign in to comment.