Skip to content

Commit 89cea2d

Browse files
committed
feat(notes): highlight code dark color
1 parent e62ef47 commit 89cea2d

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

apps/codever-ui/src/app/shared/pipe/markdown2html.pipe.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ import * as DOMPurify from 'dompurify';
44
import { marked } from 'marked';
55
import hljs from 'highlight.js';
66

7-
marked.setOptions({
8-
highlight: function (code: string, lang: string) {
9-
if (lang && hljs.getLanguage(lang)) {
10-
return hljs.highlight(code, { language: lang }).value;
11-
}
12-
return hljs.highlightAuto(code).value;
13-
},
14-
});
7+
// Custom renderer to apply highlight.js syntax highlighting to fenced code blocks
8+
// and add the 'hljs' CSS class so the github-dark theme (included in angular.json) is applied
9+
const renderer = new marked.Renderer();
10+
renderer.code = function (code: string, language: string) {
11+
const lang = language && hljs.getLanguage(language) ? language : null;
12+
const highlighted = lang
13+
? hljs.highlight(code, { language: lang }).value
14+
: hljs.highlightAuto(code).value;
15+
return `<pre><code class="hljs${lang ? ' language-' + lang : ''}">${highlighted}</code></pre>`;
16+
};
17+
18+
marked.setOptions({ renderer });
1519

1620
@Pipe({ name: 'md2html' })
1721
export class Markdown2HtmlPipe implements PipeTransform {

0 commit comments

Comments
 (0)