Skip to content

Commit

Permalink
feat[close #19]: add support for color highlighting in codeblocks
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkobrombin committed Feb 14, 2024
1 parent 56ae19b commit 4d7bb19
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
23 changes: 18 additions & 5 deletions src/core/highlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,23 @@ hljs.registerLanguage('bash', bash);
hljs.registerLanguage('yaml', yaml);

export default {
mounted(el: HTMLElement, binding: any) {
const code = el.innerText;
const highlightedCode = hljs.highlight(binding.arg, code).value;
el.innerHTML = highlightedCode;
el.classList.add('hljs');
install(app: any) {
app.mixin({
mounted() {
this.highlightCode();
},
updated() {
this.highlightCode();
},
methods: {
highlightCode() {
requestAnimationFrame(() => {
document.querySelectorAll('.language-yaml, .language-bash, pre code').forEach((block) => {
hljs.highlightElement(block as HTMLElement);
});
});
}
}
});
}
};
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const app = createApp(App);
const pinia = createPinia();
pinia.use(piniaPluginPersistedstate);

app.directive("highlight", highlight);
app.use(highlight);

app.use(router);
app.use(pinia);
Expand Down
2 changes: 1 addition & 1 deletion src/views/ArticleView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</div>
</aside>
<div class="w-full lg:w-3/4 lg:pl-4">
<div class="content prose" v-html="article.Body"></div>
<div class="content prose article-content" v-html="article.Body"></div>
</div>
</div>

Expand Down

0 comments on commit 4d7bb19

Please sign in to comment.