mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-07-21 06:09:41 +02:00
New tools added + code highlighting (#12)
This commit is contained in:
parent
f845a0d09f
commit
262c1614ed
15 changed files with 1510 additions and 1508 deletions
46
src/frontend/js/classes/codeStyler.js
Normal file
46
src/frontend/js/classes/codeStyler.js
Normal file
|
@ -0,0 +1,46 @@
|
|||
import hljs from "highlight.js/lib/highlight";
|
||||
import javascript from 'highlight.js/lib/languages/javascript';
|
||||
import style from 'highlight.js/styles/github-gist.css';
|
||||
|
||||
/**
|
||||
* @class CodeStyles
|
||||
* @classdesc Provides styling for code blocks
|
||||
*/
|
||||
export default class CodeStyler {
|
||||
/**
|
||||
* @param {string} selector - CSS selector for code blocks
|
||||
* @param {string[]} languages - list of languages to highlight, see hljs.listLanguages()
|
||||
*/
|
||||
constructor({selector, languages = [ 'javascript' ]}) {
|
||||
this.codeBlocksSelector = selector;
|
||||
this.languages = languages;
|
||||
this.langsAvailable = {
|
||||
javascript
|
||||
};
|
||||
|
||||
this.init();
|
||||
}
|
||||
|
||||
/**
|
||||
* Start to highlight
|
||||
*/
|
||||
init() {
|
||||
const codeBlocks = document.querySelectorAll(this.codeBlocksSelector);
|
||||
|
||||
if (!codeBlocks.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.languages.forEach(lang => {
|
||||
hljs.registerLanguage(lang, this.langsAvailable[lang]);
|
||||
});
|
||||
|
||||
hljs.configure({
|
||||
languages: this.languages
|
||||
});
|
||||
|
||||
Array.from(codeBlocks).forEach(block => {
|
||||
hljs.highlightBlock(block);
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue